|
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
|
|
|
use iCom\SolrClient\JsonHelper; |
|
17
|
|
|
use iCom\SolrClient\JsonQuery; |
|
18
|
|
|
|
|
19
|
|
|
final class SelectQuery implements JsonQuery |
|
20
|
|
|
{ |
|
21
|
|
|
use JsonHelper; |
|
22
|
|
|
|
|
23
|
|
|
// to keep key order consistent |
|
24
|
|
|
private $types = [ |
|
25
|
|
|
'query' => 'string', |
|
26
|
|
|
'filter' => 'array', |
|
27
|
|
|
'fields' => 'array', |
|
28
|
|
|
'facet' => 'array', |
|
29
|
|
|
'sort' => 'string', |
|
30
|
|
|
'offset' => 'integer', |
|
31
|
|
|
'limit' => 'integer', |
|
32
|
|
|
'params' => 'array', |
|
33
|
|
|
]; |
|
34
|
|
|
private $body; |
|
35
|
|
|
|
|
36
|
|
|
public function __construct(array $body = []) |
|
37
|
|
|
{ |
|
38
|
|
|
if ($invalid = array_diff_key($body, $this->types)) { |
|
39
|
|
|
throw new \InvalidArgumentException(sprintf('Invalid keys "%s" found. Valid keys are "%s".', implode(', ', array_keys($invalid)), implode(', ', array_keys($this->types)))); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
foreach ($body as $key => $value) { |
|
43
|
|
|
if ($this->types[$key] !== $type = \gettype($value)) { |
|
44
|
|
|
throw new \InvalidArgumentException(sprintf('Type of field "%s" should be "%s", "%s" given.', $key, $this->types[$key], $type)); |
|
45
|
|
|
} |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
$this->body = array_replace(array_fill_keys(array_keys($this->types), null), $body); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
public static function create(array $body = []): self |
|
52
|
|
|
{ |
|
53
|
|
|
return new self($body); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
public function query(string $query): self |
|
57
|
|
|
{ |
|
58
|
|
|
$q = clone $this; |
|
59
|
|
|
$q->body['query'] = $query; |
|
60
|
|
|
|
|
61
|
|
|
return $q; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
public function filter(array $filter): self |
|
65
|
|
|
{ |
|
66
|
|
|
$q = clone $this; |
|
67
|
|
|
$q->body['filter'] = $filter; |
|
68
|
|
|
|
|
69
|
|
|
return $q; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
public function withFilter(string $filter): self |
|
73
|
|
|
{ |
|
74
|
|
|
$q = clone $this; |
|
75
|
|
|
$q->body['filter'][] = $filter; |
|
76
|
|
|
|
|
77
|
|
|
return $q; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
public function fields(array $fields): self |
|
81
|
|
|
{ |
|
82
|
|
|
$q = clone $this; |
|
83
|
|
|
$q->body['fields'] = $fields; |
|
84
|
|
|
|
|
85
|
|
|
return $q; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
public function sort(string $sort): self |
|
89
|
|
|
{ |
|
90
|
|
|
$q = clone $this; |
|
91
|
|
|
$q->body['sort'] = $sort; |
|
92
|
|
|
|
|
93
|
|
|
return $q; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
public function facet(array $facet): self |
|
97
|
|
|
{ |
|
98
|
|
|
$q = clone $this; |
|
99
|
|
|
$q->body['facet'] = $facet; |
|
100
|
|
|
|
|
101
|
|
|
return $q; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
public function params(array $params): self |
|
105
|
|
|
{ |
|
106
|
|
|
$q = clone $this; |
|
107
|
|
|
$q->body['params'] = $params; |
|
108
|
|
|
|
|
109
|
|
|
return $q; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
public function offset(int $offset): self |
|
113
|
|
|
{ |
|
114
|
|
|
$q = clone $this; |
|
115
|
|
|
$q->body['offset'] = $offset; |
|
116
|
|
|
|
|
117
|
|
|
return $q; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
public function limit(int $limit): self |
|
121
|
|
|
{ |
|
122
|
|
|
$q = clone $this; |
|
123
|
|
|
$q->body['limit'] = $limit; |
|
124
|
|
|
|
|
125
|
|
|
return $q; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
public function toJson(): string |
|
129
|
|
|
{ |
|
130
|
|
|
return self::jsonEncode(array_filter($this->body), \JSON_UNESCAPED_UNICODE); |
|
131
|
|
|
} |
|
132
|
|
|
} |
|
133
|
|
|
|