Skip to content

Commit d62925c

Browse files
committed
Entity documentation + Raw usage documentation
1 parent eefb2b2 commit d62925c

4 files changed

Lines changed: 320 additions & 6 deletions

File tree

doc/01_intro.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,27 @@ elasticSearch:
2727

2828
For more config options see default values in `\Spameri\Elastic\DI\ElasticSearchExtension::$defaults`. [Here](../src/DI/ElasticSearchExtension.php#L9).
2929

30+
#### Raw client usage
31+
- After this configuration you are ready to use ElasticSearch in your Nette application.
32+
- Where needed just inject `\Spameri\Elastic\ClientProvider` and then directly call what you need, like this:
33+
```php
34+
$result = $this->clientProvider->client()->search(
35+
(
36+
new \Spameri\ElasticQuery\Document(
37+
$index,
38+
new \Spameri\ElasticQuery\Document\Body\Plain(
39+
$elasticQuery->toArray()
40+
),
41+
$index
42+
)
43+
)->toArray()
44+
);
45+
```
46+
- [Client](https://github.com/elastic/elasticsearch-php/blob/master/src/Elasticsearch/Client.php) is provided from **elasticsearch/elasticsearch** and you can see their [documentation](https://github.com/elastic/elasticsearch-php#quickstart)
47+
what methods and arrays are supported.
48+
- When in doubt what how many arrays or how many arguments **match** supports use [Spameri/ElasticQuery](https://github.com/Spameri/ElasticQuery/blob/master/doc/02-query-objects.md)
49+
- This is library used in later examples. But direct approach is also possible.
50+
3051
---
3152

3253
### 2. First entity

doc/03_entity_class.md

Lines changed: 224 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,13 @@ class KeyWord implements \Spameri\Elastic\Entity\IValue
9999
```
100100

101101
#### Value collection property - `Video.Story.KeyWordCollection`
102-
TODO Description
102+
- If you need array of scalar values lets create ValueCollection.
103+
- For easy setup you can use `\Spameri\Elastic\Entity\AbstractValueCollection` just create your collection and extend this abstract as you need.
104+
For more advanced and typed approach use interface, as described next.
105+
- Interface `\Spameri\Elastic\Entity\IValueCollection` is when you want typed and validated scalar value collection.
106+
- After implementing interface you need to implement **getIterator()** method.
107+
- Next to be type save you want to add methods **add**, **remove**, **get**, **__construct**
108+
- For **__construct** you best fill values to collection as here [\Spameri\Elastic\Entity\AbstractValueCollection#L20](../src/Entity/AbstractValueCollection.php#L20)
103109
```php
104110
namespace SpameriTests\Data\Entity\Video\Story;
105111

@@ -125,13 +131,28 @@ class KeyWordCollection implements \Spameri\Elastic\Entity\IValueCollection
125131

126132

127133
public function add(
128-
KeyWord $keyWord
134+
\SpameriTests\Data\Entity\Video\Story\KeyWord $keyWord
129135
) : void
130136
{
131137
$this->collection[$keyWord->value()] = $keyWord;
132138
}
133139

134140

141+
public function remove(string $key) : void
142+
{
143+
unset($this->collection[$key]);
144+
}
145+
146+
147+
public function get(string $key) : ?\SpameriTests\Data\Entity\Video\Story\KeyWord
148+
{
149+
if ( ! isset($this->collection[$key])) {
150+
return NULL;
151+
}
152+
153+
return $this->collection[$key];
154+
}
155+
135156
public function getIterator() : \ArrayIterator
136157
{
137158
return new \ArrayIterator($this->collection);
@@ -140,7 +161,11 @@ class KeyWordCollection implements \Spameri\Elastic\Entity\IValueCollection
140161
```
141162

142163
#### Single entity property - `Video.Story`
143-
TODO Description
164+
- If you need some nested structure `\Spameri\Elastic\Entity\IEntity` interface is here for you.
165+
- Also when feeling lazy there is `\Spameri\Elastic\Entity\AbstractEntity` for you to extend with methods implemented.
166+
- In our example we have entity **Story** to encapsulate keywords and other story related properties.
167+
- Library then can convert this entity to array and save it as array with no more help.
168+
144169
```php
145170
namespace SpameriTests\Data\Entity\Video;
146171

@@ -170,13 +195,15 @@ class Story implements \Spameri\Elastic\Entity\IEntity
170195

171196
public function key() : string
172197
{
173-
198+
return \md5(\implode('_', $this->entityVariables()));
174199
}
175200
}
176201
```
177202

178203
#### Entity collection property - `Video.Connections.FollowsCollection`
179-
TODO Description
204+
- ElasticSearch is powerful tool and it allows you to nest objects and collection as you need, so you can make collection of nested objects.
205+
- This is simple you have Entity **Story** with implemented `IEntity` interface and all you need is create collection, extend `class FollowsCollection extends \Spameri\Elastic\Entity\Collection\EntityCollection`
206+
and you are done.
180207
```php
181208
namespace SpameriTests\Data\Entity\Video\Connections;
182209

@@ -188,7 +215,12 @@ class FollowsCollection extends \Spameri\Elastic\Entity\Collection\EntityCollect
188215
```
189216

190217
#### ElasticEntity collection property - `Video.People`
191-
TODO Description
218+
- `\Spameri\Elastic\Entity\Collection\ElasticEntityCollection` provides basic relations for entities in ElasticSearch.
219+
- It saves **_id** to current entity as reference in raw data but when loaded you have full entity with that id.
220+
Any changes made to related entity/ies will be persisted when main entity is saved.
221+
- Entity can be manually related 1:1 with manual lazy load in Factory (example in [factory](11_entity_factory.md) documentation)
222+
- Or multiple entities can be in collection lazily loaded all at once, also in factory example.
223+
- All you need is extend `\Spameri\Elastic\Entity\Collection\ElasticEntityCollection` and fill with your entities, library will do saving and resolving for you.
192224
```php
193225
namespace SpameriTests\Data\Entity\Video;
194226

@@ -212,3 +244,189 @@ class People extends \Spameri\Elastic\Entity\Collection\ElasticEntityCollection
212244
}
213245
```
214246

247+
## Final product [Example](../tests/SpameriTests/Data/Entity/Video.php)
248+
```php
249+
namespace SpameriTests\Data\Entity;
250+
251+
252+
class Video implements \Spameri\Elastic\Entity\IElasticEntity
253+
{
254+
255+
/**
256+
* @var \Spameri\Elastic\Entity\Property\IElasticId
257+
*/
258+
private $id;
259+
260+
/**
261+
* @var \SpameriTests\Data\Entity\Video\Identification
262+
*/
263+
private $identification;
264+
265+
/**
266+
* @var \SpameriTests\Data\Entity\Property\Name
267+
*/
268+
private $name;
269+
270+
/**
271+
* @var \SpameriTests\Data\Entity\Property\Year
272+
*/
273+
private $year;
274+
275+
/**
276+
* @var \SpameriTests\Data\Entity\Video\Technical
277+
*/
278+
private $technical;
279+
280+
/**
281+
* @var \SpameriTests\Data\Entity\Video\Story
282+
*/
283+
private $story;
284+
285+
/**
286+
* @var \SpameriTests\Data\Entity\Video\Details
287+
*/
288+
private $details;
289+
290+
/**
291+
* @var \SpameriTests\Data\Entity\Video\HighLights
292+
*/
293+
private $highLights;
294+
295+
/**
296+
* @var \SpameriTests\Data\Entity\Video\Connections
297+
*/
298+
private $connections;
299+
300+
/**
301+
* @var \SpameriTests\Data\Entity\Video\SeasonCollection
302+
*/
303+
private $season;
304+
305+
/**
306+
* @var \SpameriTests\Data\Entity\Video\People
307+
*/
308+
private $people;
309+
310+
311+
public function __construct(
312+
\Spameri\Elastic\Entity\Property\IElasticId $id
313+
, \SpameriTests\Data\Entity\Video\Identification $identification
314+
, \SpameriTests\Data\Entity\Property\Name $name
315+
, \SpameriTests\Data\Entity\Property\Year $year
316+
, \SpameriTests\Data\Entity\Video\Technical $technical
317+
, \SpameriTests\Data\Entity\Video\Story $story
318+
, \SpameriTests\Data\Entity\Video\Details $details
319+
, \SpameriTests\Data\Entity\Video\HighLights $highLights
320+
, \SpameriTests\Data\Entity\Video\Connections $connections
321+
, \SpameriTests\Data\Entity\Video\People $people
322+
, \SpameriTests\Data\Entity\Video\SeasonCollection $season = NULL
323+
)
324+
{
325+
$this->id = $id;
326+
$this->identification = $identification;
327+
$this->name = $name;
328+
$this->year = $year;
329+
$this->technical = $technical;
330+
$this->story = $story;
331+
$this->details = $details;
332+
$this->highLights = $highLights;
333+
$this->connections = $connections;
334+
335+
if ($season === NULL) {
336+
$season = new \SpameriTests\Data\Entity\Video\SeasonCollection();
337+
}
338+
$this->season = $season;
339+
$this->people = $people;
340+
}
341+
342+
343+
public function entityVariables() : array
344+
{
345+
return \get_object_vars($this);
346+
}
347+
348+
349+
public function id() : \Spameri\Elastic\Entity\Property\IElasticId
350+
{
351+
return $this->id;
352+
}
353+
354+
355+
public function identification() : \SpameriTests\Data\Entity\Video\Identification
356+
{
357+
return $this->identification;
358+
}
359+
360+
361+
public function name() : \SpameriTests\Data\Entity\Property\Name
362+
{
363+
return $this->name;
364+
}
365+
366+
367+
public function rename(\SpameriTests\Data\Entity\Property\Name $name) : void
368+
{
369+
$this->name = $name;
370+
}
371+
372+
373+
public function year() : \SpameriTests\Data\Entity\Property\Year
374+
{
375+
return $this->year;
376+
}
377+
378+
379+
public function setYear(\SpameriTests\Data\Entity\Property\Year $year) : void
380+
{
381+
$this->year = $year;
382+
}
383+
384+
385+
public function technical() : \SpameriTests\Data\Entity\Video\Technical
386+
{
387+
return $this->technical;
388+
}
389+
390+
391+
public function setTechnicalFromImdb(\SpameriTests\Data\Entity\Video\Technical $technical) : void
392+
{
393+
$this->technical = $technical;
394+
}
395+
396+
397+
public function story() : \SpameriTests\Data\Entity\Video\Story
398+
{
399+
return $this->story;
400+
}
401+
402+
403+
public function details() : \SpameriTests\Data\Entity\Video\Details
404+
{
405+
return $this->details;
406+
}
407+
408+
409+
public function highLights() : \SpameriTests\Data\Entity\Video\HighLights
410+
{
411+
return $this->highLights;
412+
}
413+
414+
415+
public function connections() : \SpameriTests\Data\Entity\Video\Connections
416+
{
417+
return $this->connections;
418+
}
419+
420+
421+
public function season() : \SpameriTests\Data\Entity\Video\SeasonCollection
422+
{
423+
return $this->season;
424+
}
425+
426+
427+
public function people() : \SpameriTests\Data\Entity\Video\People
428+
{
429+
return $this->people;
430+
}
431+
}
432+
```

src/Entity/AbstractEntity.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Spameri\Elastic\Entity;
4+
5+
6+
abstract class AbstractEntity implements IEntity
7+
{
8+
9+
public function entityVariables() : array
10+
{
11+
return \get_object_vars($this);
12+
}
13+
14+
15+
public function key() : string
16+
{
17+
return \md5(\implode('_', $this->entityVariables()));
18+
}
19+
20+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Spameri\Elastic\Entity;
4+
5+
6+
abstract class AbstractValueCollection implements IValueCollection
7+
{
8+
9+
/**
10+
* @var array<\Spameri\Elastic\Entity\IValue>
11+
*/
12+
protected $collection;
13+
14+
15+
public function __construct(
16+
\Spameri\Elastic\Entity\IValue ... $collection
17+
)
18+
{
19+
$this->collection = [];
20+
foreach ($collection as $value) {
21+
$this->add($value);
22+
}
23+
}
24+
25+
26+
public function add(
27+
\Spameri\Elastic\Entity\IValue $value
28+
) : void
29+
{
30+
$this->collection[$value->value()] = $value;
31+
}
32+
33+
34+
public function remove($key) : void
35+
{
36+
unset($this->collection[$key]);
37+
}
38+
39+
40+
public function get($key) : ?\Spameri\Elastic\Entity\IValue
41+
{
42+
if ( ! isset($this->collection[$key])) {
43+
return NULL;
44+
}
45+
46+
return $this->collection[$key];
47+
}
48+
49+
50+
public function getIterator() : \ArrayIterator
51+
{
52+
return new \ArrayIterator($this->collection);
53+
}
54+
55+
}

0 commit comments

Comments
 (0)