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\Helper; |
15
|
|
|
|
16
|
|
|
use iCom\SolrClient\Query\QueryHelper; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @psalm-immutable |
20
|
|
|
*/ |
21
|
|
|
final class Collapse implements QueryHelper |
22
|
|
|
{ |
23
|
|
|
use Caching; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var string |
27
|
|
|
*/ |
28
|
|
|
private $field; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var ?string |
32
|
|
|
*/ |
33
|
|
|
private $min; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var ?string |
37
|
|
|
*/ |
38
|
|
|
private $max; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var ?string |
42
|
|
|
*/ |
43
|
|
|
private $sort; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @var ?string |
47
|
|
|
*/ |
48
|
|
|
private $nullPolicy; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @var ?string |
52
|
|
|
*/ |
53
|
|
|
private $hint; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @var ?int |
57
|
|
|
*/ |
58
|
|
|
private $size; |
59
|
|
|
|
60
|
|
|
public function __construct(string $field) |
61
|
|
|
{ |
62
|
|
|
$this->field = $field; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public static function create(string $field): self |
66
|
|
|
{ |
67
|
|
|
return new self($field); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function min(string $expression): self |
71
|
|
|
{ |
72
|
|
|
$this->assertSingleSort(); |
73
|
|
|
|
74
|
|
|
$collapse = clone $this; |
75
|
|
|
$collapse->min = $expression; |
76
|
|
|
|
77
|
|
|
return $collapse; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
public function max(string $expression): self |
81
|
|
|
{ |
82
|
|
|
$this->assertSingleSort(); |
83
|
|
|
|
84
|
|
|
$collapse = clone $this; |
85
|
|
|
$collapse->max = $expression; |
86
|
|
|
|
87
|
|
|
return $collapse; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @param array<string> $sort |
92
|
|
|
*/ |
93
|
|
|
public function sort(array $sort): self |
94
|
|
|
{ |
95
|
|
|
$this->assertSingleSort(); |
96
|
|
|
|
97
|
|
|
$collapse = clone $this; |
98
|
|
|
$collapse->sort = sprintf("'%s'", implode(',', $sort)); |
99
|
|
|
|
100
|
|
|
return $collapse; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
public function nullPolicy(string $nullPolicy): self |
104
|
|
|
{ |
105
|
|
|
if (!\in_array($nullPolicy, $available = ['ignore', 'expand', 'collapse'], true)) { |
106
|
|
|
throw new \InvalidArgumentException(sprintf('Available null policies: %s!', implode(',', $available))); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
$collapse = clone $this; |
110
|
|
|
$collapse->nullPolicy = $nullPolicy; |
111
|
|
|
|
112
|
|
|
return $collapse; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
public function hint(): self |
116
|
|
|
{ |
117
|
|
|
$collapse = clone $this; |
118
|
|
|
$collapse->hint = 'top_fc'; |
119
|
|
|
|
120
|
|
|
return $collapse; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
public function size(int $size): self |
124
|
|
|
{ |
125
|
|
|
$collapse = clone $this; |
126
|
|
|
$collapse->size = $size; |
127
|
|
|
|
128
|
|
|
return $collapse; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
public function toString(): string |
132
|
|
|
{ |
133
|
|
|
$params = [ |
134
|
|
|
'field' => $this->field, |
135
|
|
|
'min' => $this->min, |
136
|
|
|
'max' => $this->max, |
137
|
|
|
'sort' => $this->sort, |
138
|
|
|
'nullPolicy' => $this->nullPolicy, |
139
|
|
|
'hint' => $this->hint, |
140
|
|
|
'size' => $this->size, |
141
|
|
|
] + $this->getCacheFields(); |
142
|
|
|
|
143
|
|
|
return sprintf('{!collapse %s}', urldecode(http_build_query($params, '', ' '))); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
private function assertSingleSort(): void |
147
|
|
|
{ |
148
|
|
|
if (null !== $this->min || null !== $this->max || null !== $this->sort) { |
149
|
|
|
throw new \RuntimeException('Multiple sort is not allowed.'); |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
|