1 | <?php |
||
24 | class Repository |
||
25 | { |
||
26 | /** |
||
27 | * @var Manager |
||
28 | */ |
||
29 | private $manager; |
||
30 | |||
31 | /** |
||
32 | * @var string Fully qualified class name |
||
33 | */ |
||
34 | private $className; |
||
35 | |||
36 | /** |
||
37 | * @var string Elasticsearch type name |
||
38 | */ |
||
39 | private $type; |
||
40 | |||
41 | /** |
||
42 | * Constructor. |
||
43 | * |
||
44 | * @param Manager $manager |
||
45 | * @param string $className |
||
46 | */ |
||
47 | public function __construct($manager, $className) |
||
48 | { |
||
49 | if (!is_string($className)) { |
||
50 | throw new \InvalidArgumentException('Class name must be a string.'); |
||
51 | } |
||
52 | |||
53 | if (!class_exists($className)) { |
||
54 | throw new \InvalidArgumentException( |
||
55 | sprintf('Cannot create repository for non-existing class "%s".', $className) |
||
56 | ); |
||
57 | } |
||
58 | |||
59 | $this->manager = $manager; |
||
60 | $this->className = $className; |
||
61 | $this->type = $this->resolveType($className); |
||
62 | } |
||
63 | |||
64 | /** |
||
65 | * Returns elasticsearch manager used in the repository. |
||
66 | * |
||
67 | * @return Manager |
||
68 | */ |
||
69 | public function getManager() |
||
73 | |||
74 | /** |
||
75 | * @return array |
||
76 | */ |
||
77 | public function getType() |
||
78 | { |
||
79 | return $this->type; |
||
80 | } |
||
81 | |||
82 | /** |
||
83 | * Returns a single document data by ID or null if document is not found. |
||
84 | * |
||
85 | * @param string $id Document ID to find |
||
86 | * @param string $routing Custom routing for the document |
||
87 | * |
||
88 | * @return object |
||
89 | */ |
||
90 | public function find($id, $routing = null) |
||
91 | { |
||
92 | return $this->manager->find($this->type, $id, $routing); |
||
93 | } |
||
94 | |||
95 | /** |
||
96 | * Returns documents by a set of ids |
||
97 | * |
||
98 | * @param array $ids |
||
99 | * |
||
100 | * @return DocumentIterator The objects. |
||
101 | */ |
||
102 | public function findByIds(array $ids) |
||
130 | |||
131 | /** |
||
132 | * Finds documents by a set of criteria. |
||
133 | * |
||
134 | * @param array $criteria Example: ['group' => ['best', 'worst'], 'job' => 'medic']. |
||
135 | * @param array|null $orderBy Example: ['name' => 'ASC', 'surname' => 'DESC']. |
||
136 | * @param int|null $limit Example: 5. |
||
137 | * @param int|null $offset Example: 30. |
||
138 | * |
||
139 | * @return array|DocumentIterator The objects. |
||
140 | */ |
||
141 | public function findBy( |
||
142 | array $criteria, |
||
143 | array $orderBy = [], |
||
144 | $limit = null, |
||
145 | $offset = null |
||
146 | ) { |
||
147 | $search = $this->createSearch(); |
||
148 | |||
149 | if ($limit !== null) { |
||
150 | $search->setSize($limit); |
||
151 | } |
||
152 | if ($offset !== null) { |
||
153 | $search->setFrom($offset); |
||
154 | } |
||
155 | |||
156 | foreach ($criteria as $field => $value) { |
||
157 | $search->addQuery( |
||
158 | new QueryStringQuery(is_array($value) ? implode(' OR ', $value) : $value, ['default_field' => $field]) |
||
159 | ); |
||
160 | } |
||
161 | |||
162 | foreach ($orderBy as $field => $direction) { |
||
163 | $search->addSort(new FieldSort($field, $direction)); |
||
164 | } |
||
165 | |||
166 | return $this->execute($search); |
||
167 | } |
||
168 | |||
169 | /** |
||
170 | * Finds a single document by a set of criteria. |
||
171 | * |
||
172 | * @param array $criteria Example: ['group' => ['best', 'worst'], 'job' => 'medic']. |
||
173 | * @param array|null $orderBy Example: ['name' => 'ASC', 'surname' => 'DESC']. |
||
174 | * |
||
175 | * @return object|null The object. |
||
176 | */ |
||
177 | public function findOneBy(array $criteria, array $orderBy = []) |
||
178 | { |
||
179 | $result = $this->findBy($criteria, $orderBy, null, null); |
||
180 | |||
181 | return $result->first(); |
||
182 | } |
||
183 | |||
184 | /** |
||
185 | * Returns search instance. |
||
186 | * |
||
187 | * @return Search |
||
188 | */ |
||
189 | public function createSearch() |
||
193 | |||
194 | /** |
||
195 | * Executes given search. |
||
196 | * |
||
197 | * @param Search $search |
||
198 | * @param string $resultsType |
||
199 | * |
||
200 | * @return DocumentIterator|RawIterator|array |
||
201 | * |
||
202 | * @throws \Exception |
||
203 | */ |
||
204 | public function execute(Search $search, $resultsType = Result::RESULTS_OBJECT) |
||
208 | |||
209 | /** |
||
210 | * Counts documents by given search. |
||
211 | * |
||
212 | * @param Search $search |
||
213 | * @param array $params |
||
214 | * @param bool $returnRaw If set true returns raw response gotten from client. |
||
215 | * |
||
216 | * @return int|array |
||
217 | */ |
||
218 | public function count(Search $search, array $params = [], $returnRaw = false) |
||
239 | |||
240 | /** |
||
241 | * Removes a single document data by ID. |
||
242 | * |
||
243 | * @param string $id Document ID to remove |
||
244 | * @param string $routing Custom routing for the document |
||
245 | * |
||
246 | * @return array |
||
247 | * |
||
248 | * @throws \LogicException |
||
249 | */ |
||
250 | public function remove($id, $routing = null) |
||
266 | |||
267 | /** |
||
268 | * Partial document update. |
||
269 | * |
||
270 | * @param string $id Document id to update. |
||
271 | * @param array $fields Fields array to update. |
||
272 | * @param string $script Groovy script to update fields. |
||
273 | * @param array $params Additional parameters to pass to the client. |
||
274 | * |
||
275 | * @return array |
||
276 | */ |
||
277 | public function update($id, array $fields = [], $script = null, array $params = []) |
||
298 | |||
299 | /** |
||
300 | * Resolves elasticsearch type by class name. |
||
301 | * |
||
302 | * @param string $className |
||
303 | * |
||
304 | * @return array |
||
305 | */ |
||
306 | private function resolveType($className) |
||
310 | |||
311 | /** |
||
312 | * Returns fully qualified class name. |
||
313 | * |
||
314 | * @return string |
||
315 | */ |
||
316 | public function getClassName() |
||
320 | } |
||
321 |
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: