1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of Solr Client Symfony package. |
7
|
|
|
* |
8
|
|
|
* (c) ingatlan.com Zrt. <[email protected]> |
9
|
|
|
* |
10
|
|
|
* This source file is subject to the MIT license that is bundled |
11
|
|
|
* with this source code in the file LICENSE. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace iCom\SolrClient\Query; |
15
|
|
|
|
16
|
|
|
final class TermsQuery |
17
|
|
|
{ |
18
|
|
|
private $params = [ |
19
|
|
|
'field' => null, |
20
|
|
|
'separator' => ',', |
21
|
|
|
'method' => null, |
22
|
|
|
'values' => null, |
23
|
|
|
]; |
24
|
|
|
|
25
|
|
|
public function __construct(string $field, array $values) |
26
|
|
|
{ |
27
|
|
|
$this->params['field'] = $field; |
28
|
|
|
$this->params['values'] = $values; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
public function __toString(): string |
32
|
|
|
{ |
33
|
|
|
return sprintf('{!terms f=%s%s%s}%s', |
34
|
|
|
$this->params['field'], |
35
|
|
|
null === $this->params['method'] ? '' : ' method='.$this->params['method'], |
36
|
|
|
',' === $this->params['separator'] ? '' : sprintf(' separator="%s"', $this->params['separator']), |
37
|
|
|
\implode($this->params['separator'], $this->params['values']) |
38
|
|
|
); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public static function create(string $field, array $values): self |
42
|
|
|
{ |
43
|
|
|
return new self($field, $values); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function separator(string $separator): self |
47
|
|
|
{ |
48
|
|
|
$collapse = clone $this; |
49
|
|
|
$collapse->params['separator'] = $separator; |
50
|
|
|
|
51
|
|
|
return $collapse; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function method(string $method): self |
55
|
|
|
{ |
56
|
|
|
if (!\in_array($method, $available = ['termsFilter', 'booleanQuery', 'automaton', 'docValuesTermsFilter'], true)) { |
57
|
|
|
throw new \InvalidArgumentException(sprintf('Available methods: %s!', implode(',', $available))); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
$collapse = clone $this; |
61
|
|
|
$collapse->params['method'] = $method; |
62
|
|
|
|
63
|
|
|
return $collapse; |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|