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() |
||
70 | { |
||
71 | return $this->manager; |
||
72 | } |
||
73 | |||
74 | /** |
||
75 | * @return array |
||
76 | */ |
||
77 | public function getType() |
||
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 | * |
||
87 | * @return object |
||
88 | */ |
||
89 | public function find($id) |
||
93 | |||
94 | /** |
||
95 | * Returns documents by a set of ids |
||
96 | * |
||
97 | * @param array $ids |
||
98 | * |
||
99 | * @return DocumentIterator The objects. |
||
100 | */ |
||
101 | public function findByIds(array $ids) |
||
129 | |||
130 | /** |
||
131 | * Finds documents by a set of criteria. |
||
132 | * |
||
133 | * @param array $criteria Example: ['group' => ['best', 'worst'], 'job' => 'medic']. |
||
134 | * @param array|null $orderBy Example: ['name' => 'ASC', 'surname' => 'DESC']. |
||
135 | * @param int|null $limit Example: 5. |
||
136 | * @param int|null $offset Example: 30. |
||
137 | * |
||
138 | * @return array|DocumentIterator The objects. |
||
139 | */ |
||
140 | public function findBy( |
||
167 | |||
168 | /** |
||
169 | * Finds a single document by a set of criteria. |
||
170 | * |
||
171 | * @param array $criteria Example: ['group' => ['best', 'worst'], 'job' => 'medic']. |
||
172 | * @param array|null $orderBy Example: ['name' => 'ASC', 'surname' => 'DESC']. |
||
173 | * |
||
174 | * @return object|null The object. |
||
175 | */ |
||
176 | public function findOneBy(array $criteria, array $orderBy = []) |
||
182 | |||
183 | /** |
||
184 | * Returns search instance. |
||
185 | * |
||
186 | * @return Search |
||
187 | */ |
||
188 | public function createSearch() |
||
192 | |||
193 | /** |
||
194 | * Executes given search. |
||
195 | * |
||
196 | * @param Search $search |
||
197 | * @param string $resultsType |
||
198 | * |
||
199 | * @return DocumentIterator|RawIterator|array |
||
200 | * |
||
201 | * @throws \Exception |
||
202 | */ |
||
203 | public function execute(Search $search, $resultsType = Result::RESULTS_OBJECT) |
||
207 | |||
208 | /** |
||
209 | * Counts documents by given search. |
||
210 | * |
||
211 | * @param Search $search |
||
212 | * @param array $params |
||
213 | * @param bool $returnRaw If set true returns raw response gotten from client. |
||
214 | * |
||
215 | * @return int|array |
||
216 | */ |
||
217 | public function count(Search $search, array $params = [], $returnRaw = false) |
||
238 | |||
239 | /** |
||
240 | * Removes a single document data by ID. |
||
241 | * |
||
242 | * @param string $id Document ID to remove. |
||
243 | * |
||
244 | * @return array |
||
245 | * |
||
246 | * @throws \LogicException |
||
247 | */ |
||
248 | public function remove($id) |
||
260 | |||
261 | /** |
||
262 | * Partial document update. |
||
263 | * |
||
264 | * @param string $id Document id to update. |
||
265 | * @param array $fields Fields array to update. |
||
266 | * @param string $script Groovy script to update fields. |
||
267 | * @param array $params Additional parameters to pass to the client. |
||
268 | * |
||
269 | * @return array |
||
270 | */ |
||
271 | public function update($id, array $fields = [], $script = null, array $params = []) |
||
292 | |||
293 | /** |
||
294 | * Resolves elasticsearch type by class name. |
||
295 | * |
||
296 | * @param string $className |
||
297 | * |
||
298 | * @return array |
||
299 | */ |
||
300 | private function resolveType($className) |
||
304 | |||
305 | /** |
||
306 | * Returns fully qualified class name. |
||
307 | * |
||
308 | * @return string |
||
309 | */ |
||
310 | public function getClassName() |
||
314 | } |
||
315 |