Skip to content

Commit e2d8935

Browse files
committed
Merge remote-tracking branch 'remotes/origin/6.x' into 7.x
# Conflicts: # composer.json
2 parents eb6040e + baace95 commit e2d8935

File tree

3 files changed

+134
-2
lines changed

3 files changed

+134
-2
lines changed

Diff for: composer.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
}
1212
],
1313
"require": {
14-
"php": "^7.1",
15-
"symfony/serializer": "^3.0|^4.0|^5.0",
14+
"php": "^7.0",
15+
"symfony/serializer": "^3.0|^4.0",
1616
"paragonie/random_compat": "*"
1717
},
1818
"require-dev": {

Diff for: src/Query/TermLevel/TermsSetQuery.php

+85
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the ONGR package.
5+
*
6+
* (c) NFQ Technologies UAB <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace ONGR\ElasticsearchDSL\Query\TermLevel;
13+
14+
use ONGR\ElasticsearchDSL\BuilderInterface;
15+
use ONGR\ElasticsearchDSL\ParametersTrait;
16+
17+
/**
18+
* Represents Elasticsearch "terms_set" query.
19+
*
20+
* @link https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-terms-set-query.html
21+
*/
22+
class TermsSetQuery implements BuilderInterface
23+
{
24+
use ParametersTrait;
25+
26+
const MINIMUM_SHOULD_MATCH_TYPE_FIELD = 'minimum_should_match_field';
27+
const MINIMUM_SHOULD_MATCH_TYPE_SCRIPT = 'minimum_should_match_script';
28+
29+
/**
30+
* @var string
31+
*/
32+
private $field;
33+
34+
/**
35+
* @var array
36+
*/
37+
private $terms;
38+
39+
/**
40+
* Constructor.
41+
*
42+
* @param string $field Field name
43+
* @param array $terms An array of terms
44+
* @param array $parameters Parameters
45+
*/
46+
public function __construct($field, $terms, array $parameters)
47+
{
48+
$this->field = $field;
49+
$this->terms = $terms;
50+
$this->validateParameters($parameters);
51+
$this->setParameters($parameters);
52+
}
53+
54+
/**
55+
* {@inheritdoc}
56+
*/
57+
public function getType()
58+
{
59+
return 'terms_set';
60+
}
61+
62+
/**
63+
* {@inheritdoc}
64+
*/
65+
public function toArray()
66+
{
67+
$query = [
68+
'terms' => $this->terms,
69+
];
70+
71+
return [$this->getType() => [
72+
$this->field => $this->processArray($query),
73+
]];
74+
}
75+
76+
private function validateParameters(array $parameters)
77+
{
78+
if (!isset($parameters[self::MINIMUM_SHOULD_MATCH_TYPE_FIELD]) &&
79+
!isset($parameters[self::MINIMUM_SHOULD_MATCH_TYPE_SCRIPT])
80+
) {
81+
$message = "Either minimum_should_match_field or minimum_should_match_script must be set.";
82+
throw new \InvalidArgumentException($message);
83+
}
84+
}
85+
}

Diff for: tests/Unit/Query/TermLevel/TermsSetQueryTest.php

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the ONGR package.
5+
*
6+
* (c) NFQ Technologies UAB <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace ONGR\ElasticsearchDSL\Tests\Unit\Query\TermLevel;
13+
14+
use ONGR\ElasticsearchDSL\Query\TermLevel\TermsSetQuery;
15+
16+
class TermsSetQueryTest extends \PHPUnit\Framework\TestCase
17+
{
18+
/**
19+
* Tests toArray().
20+
*/
21+
public function testToArray()
22+
{
23+
$terms = ['php', 'c++', 'java'];
24+
$parameters = ['minimum_should_match_field' => 'required_matches'];
25+
$query = new TermsSetQuery('programming_languages', $terms, $parameters);
26+
$expected = [
27+
'terms_set' => [
28+
'programming_languages' => [
29+
'terms' => ['php', 'c++', 'java'],
30+
'minimum_should_match_field' => 'required_matches',
31+
]
32+
],
33+
];
34+
35+
$this->assertEquals($expected, $query->toArray());
36+
}
37+
38+
public function testItThrowsAaExceptionWhenMinimumShouldMatchFieldOrMinimumShouldMatchScriptIsNotGiven()
39+
{
40+
$message = "Either minimum_should_match_field or minimum_should_match_script must be set.";
41+
$this->expectException(\InvalidArgumentException::class);
42+
$this->expectExceptionMessage($message);
43+
44+
$terms = ['php', 'c++', 'java'];
45+
new TermsSetQuery('programming_languages', $terms, []);
46+
}
47+
}

0 commit comments

Comments
 (0)