diff --git a/composer.json b/composer.json index d226ea04919fd5e977c7e78d8e31f105ddf8c0db..1cc93d8addeb3a515e3473996ec710f1e48306fa 100644 --- a/composer.json +++ b/composer.json @@ -11,15 +11,14 @@ } ], "require": { - "php": "^7.1", + "php": "^7.0", "symfony/serializer": "^3.0|^4.0", "paragonie/random_compat": "*" }, "require-dev": { "elasticsearch/elasticsearch": "^7.0", "phpunit/phpunit": "~6.0", - "squizlabs/php_codesniffer": "^3.0", - "php-coveralls/php-coveralls": "^2.1" + "squizlabs/php_codesniffer": "^3.0" }, "suggest": { "elasticsearch/elasticsearch": "This library is for elasticsearch/elasticsearch client to enhance it with DSL functionality." diff --git a/src/ParametersTrait.php b/src/ParametersTrait.php index 5305f77637a9c827e997c83934656881cd83758c..013b4b6d1f6708d6257602896ab7511683fb97ef 100644 --- a/src/ParametersTrait.php +++ b/src/ParametersTrait.php @@ -50,7 +50,7 @@ trait ParametersTrait * * @param string $name * - * @return array|string|int|bool|\stdClass + * @return array|string|int|float|bool|\stdClass */ public function getParameter($name) { @@ -69,7 +69,7 @@ trait ParametersTrait /** * @param string $name - * @param array|string|int|bool|\stdClass $value + * @param array|string|int|float|bool|\stdClass $value */ public function addParameter($name, $value) { diff --git a/src/Query/TermLevel/TermsSetQuery.php b/src/Query/TermLevel/TermsSetQuery.php new file mode 100644 index 0000000000000000000000000000000000000000..6f9ec80d7fd03717f88690c35d688777436ae15b --- /dev/null +++ b/src/Query/TermLevel/TermsSetQuery.php @@ -0,0 +1,85 @@ +<?php + +/* + * This file is part of the ONGR package. + * + * (c) NFQ Technologies UAB <info@nfq.com> + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace ONGR\ElasticsearchDSL\Query\TermLevel; + +use ONGR\ElasticsearchDSL\BuilderInterface; +use ONGR\ElasticsearchDSL\ParametersTrait; + +/** + * Represents Elasticsearch "terms_set" query. + * + * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-terms-set-query.html + */ +class TermsSetQuery implements BuilderInterface +{ + use ParametersTrait; + + const MINIMUM_SHOULD_MATCH_TYPE_FIELD = 'minimum_should_match_field'; + const MINIMUM_SHOULD_MATCH_TYPE_SCRIPT = 'minimum_should_match_script'; + + /** + * @var string + */ + private $field; + + /** + * @var array + */ + private $terms; + + /** + * Constructor. + * + * @param string $field Field name + * @param array $terms An array of terms + * @param array $parameters Parameters + */ + public function __construct($field, $terms, array $parameters) + { + $this->field = $field; + $this->terms = $terms; + $this->validateParameters($parameters); + $this->setParameters($parameters); + } + + /** + * {@inheritdoc} + */ + public function getType() + { + return 'terms_set'; + } + + /** + * {@inheritdoc} + */ + public function toArray() + { + $query = [ + 'terms' => $this->terms, + ]; + + return [$this->getType() => [ + $this->field => $this->processArray($query), + ]]; + } + + private function validateParameters(array $parameters) + { + if (!isset($parameters[self::MINIMUM_SHOULD_MATCH_TYPE_FIELD]) && + !isset($parameters[self::MINIMUM_SHOULD_MATCH_TYPE_SCRIPT]) + ) { + $message = "Either minimum_should_match_field or minimum_should_match_script must be set."; + throw new \InvalidArgumentException($message); + } + } +} diff --git a/src/Search.php b/src/Search.php index 404c68a2f2df77cea8a76e5f35902747ed602e57..89726af4fd459dfb6f76028368fa8a00848b90aa 100644 --- a/src/Search.php +++ b/src/Search.php @@ -458,7 +458,7 @@ class Search } /** - * @return int + * @return null|int */ public function getFrom() { @@ -466,7 +466,7 @@ class Search } /** - * @param int $from + * @param null|int $from * * @return $this */ @@ -498,7 +498,7 @@ class Search } /** - * @return int + * @return null|int */ public function getSize() { @@ -506,7 +506,7 @@ class Search } /** - * @param int $size + * @param null|int $size * * @return $this */ diff --git a/tests/Unit/Query/TermLevel/TermsSetQueryTest.php b/tests/Unit/Query/TermLevel/TermsSetQueryTest.php new file mode 100644 index 0000000000000000000000000000000000000000..c51fa90456a47e5fff3e531bfa5a5c28fc20e487 --- /dev/null +++ b/tests/Unit/Query/TermLevel/TermsSetQueryTest.php @@ -0,0 +1,47 @@ +<?php + +/* + * This file is part of the ONGR package. + * + * (c) NFQ Technologies UAB <info@nfq.com> + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace ONGR\ElasticsearchDSL\Tests\Unit\Query\TermLevel; + +use ONGR\ElasticsearchDSL\Query\TermLevel\TermsSetQuery; + +class TermsSetQueryTest extends \PHPUnit\Framework\TestCase +{ + /** + * Tests toArray(). + */ + public function testToArray() + { + $terms = ['php', 'c++', 'java']; + $parameters = ['minimum_should_match_field' => 'required_matches']; + $query = new TermsSetQuery('programming_languages', $terms, $parameters); + $expected = [ + 'terms_set' => [ + 'programming_languages' => [ + 'terms' => ['php', 'c++', 'java'], + 'minimum_should_match_field' => 'required_matches', + ] + ], + ]; + + $this->assertEquals($expected, $query->toArray()); + } + + public function testItThrowsAaExceptionWhenMinimumShouldMatchFieldOrMinimumShouldMatchScriptIsNotGiven() + { + $message = "Either minimum_should_match_field or minimum_should_match_script must be set."; + $this->expectException(\InvalidArgumentException::class); + $this->expectExceptionMessage($message); + + $terms = ['php', 'c++', 'java']; + new TermsSetQuery('programming_languages', $terms, []); + } +}