11# Entity class
22
3- Lets create entity class, continuing our example, in folder ` app/ProductModule/ Entity/Product .php` given file contents:
3+ Lets create entity class, continuing our example, in folder ` tests/SpameriTests/Data/ Entity/Video .php` given file contents:
44``` php
5- namespace App\ProductModule \Entity;
5+ namespace SpameriTests\Data \Entity;
66
77
8- class Product implements \Spameri\Elastic\Entity\IElasticEntity
8+ class Video implements \Spameri\Elastic\Entity\IElasticEntity
99{
1010
1111 /**
@@ -38,7 +38,7 @@ class Product implements \Spameri\Elastic\Entity\IElasticEntity
3838
3939### Lets look at class part by part.
4040
41- - Entity is in our defined namespace for ` ProductModule ` in own folder ` Entity ` which is shared for multiple entities.
41+ - Entity is in our defined namespace in own folder ` Entity ` which is shared for multiple entities.
4242- Class extends interface ` \Spameri\Elastic\Entity\IElasticEntity ` , this is core interface for ElasticSearch document.
4343It has to have ` id ` provided by ElasticSearch, library takes care of handling this field, no need to add in mapping.
4444- Based on this interface, library figures out how to save this class.
@@ -47,26 +47,26 @@ It has to have `id` provided by ElasticSearch, library takes care of handling th
4747- Interface requires function ` id() ` based on returned value it updates or creates entity.
4848- Interface requires ` entityVariables() ` in this exact form. (This may be changed in future versions, but now is required)
4949
50- ### Adding properties to product entity
50+ ### Adding properties to video entity
5151
52- #### Single value property - ` name `
52+ #### Single value property - ` Video.Story.KeyWord `
5353
54- - Lets say our product has limited name length to maximum of 255 characters and also 0 characters is not enough to
55- describe product .
54+ - Lets say our Video has limited keyword length to maximum of 55 characters and also 0 characters is not enough to
55+ describe keyword .
5656- We do not have reliable data input so lets validate this with help of interface ` \Spameri\Elastic\Entity\IValue ` .
57- - First create value object for name ` \App\ProductModule \Entity\Product\Name ` .
57+ - First create value object for keyword ` \SpameriTests\Data \Entity\Video\Story\KeyWord ` .
5858- Class should implement interface ` \Spameri\Elastic\Entity\IValue ` .
5959- ` __construct ` should have one parameter ** string $value** .
60- - In construct do our validation for product name .
60+ - In construct do our validation for keyword .
6161- Implement ` value() ` method.
62- - Add property to ` \App\ProductModule \Entity\Product ` constructor.
63- - Generate getter in ` \App\ProductModule \Entity\Product ` entity.
62+ - Add property to ` \SpameriTests\Data \Entity\Video ` constructor.
63+ - Generate getter in ` \SpameriTests\Data \Entity\Video ` entity for KeyWord.
6464- Result:
6565``` php
66- namespace App\ProductModule \Entity\Product ;
66+ namespace SpameriTests\Data \Entity\Video\Story ;
6767
6868
69- class Name implements \Spameri\Elastic\Entity\IValue
69+ class KeyWord implements \Spameri\Elastic\Entity\IValue
7070{
7171
7272 /**
@@ -79,11 +79,11 @@ class Name implements \Spameri\Elastic\Entity\IValue
7979 string $value
8080 )
8181 {
82- if (\strlen( $value) < 0 ) {
83- throw new \InvalidArgumentException('Empty string is not supported for product name: ' . $value );
82+ if ($value === '' ) {
83+ throw new \InvalidArgumentException();
8484 }
85- if (\strlen($value) > 255 ) {
86- $value = \substr($value, 0, 255 );
85+ if (\strlen($value) > 55 ) {
86+ throw new \InvalidArgumentException( );
8787 }
8888
8989 $this->value = $value;
@@ -98,14 +98,117 @@ class Name implements \Spameri\Elastic\Entity\IValue
9898}
9999```
100100
101- #### Value collection property - ` details.tags `
102- TODO
101+ #### Value collection property - ` Video.Story.KeyWordCollection `
102+ TODO Description
103+ ``` php
104+ namespace SpameriTests\Data\Entity\Video\Story;
105+
106+
107+ class KeyWordCollection implements \Spameri\Elastic\Entity\IValueCollection
108+ {
109+
110+ /**
111+ * @var array<\SpameriTests\Data\Entity\Video\Story\KeyWord>
112+ */
113+ private $collection;
114+
115+
116+ public function __construct(
117+ KeyWord ... $entities
118+ )
119+ {
120+ $this->collection = [];
121+ foreach ($entities as $keyWord) {
122+ $this->add($keyWord);
123+ }
124+ }
125+
126+
127+ public function add(
128+ KeyWord $keyWord
129+ ) : void
130+ {
131+ $this->collection[$keyWord->value()] = $keyWord;
132+ }
103133
104- #### Single entity property - ` details `
105- TODO
106134
107- #### Entity collection property - ` parameterValues `
108- TODO
135+ public function getIterator() : \ArrayIterator
136+ {
137+ return new \ArrayIterator($this->collection);
138+ }
139+ }
140+ ```
141+
142+ #### Single entity property - ` Video.Story `
143+ TODO Description
144+ ``` php
145+ namespace SpameriTests\Data\Entity\Video;
146+
147+
148+ class Story implements \Spameri\Elastic\Entity\IEntity
149+ {
150+
151+ /**
152+ * @var \SpameriTests\Data\Entity\Video\Story\KeyWordCollection
153+ */
154+ private $keyWords;
155+
156+
157+ public function __construct(
158+ \SpameriTests\Data\Entity\Video\Story\KeyWordCollection $keyWord
159+ )
160+ {
161+ $this->keyWords = $keyWord;
162+ }
163+
164+
165+ public function entityVariables() : array
166+ {
167+ return \get_object_vars($this);
168+ }
169+
170+
171+ public function key() : string
172+ {
173+
174+ }
175+ }
176+ ```
177+
178+ #### Entity collection property - ` Video.Connections.FollowsCollection `
179+ TODO Description
180+ ``` php
181+ namespace SpameriTests\Data\Entity\Video\Connections;
182+
183+
184+ class FollowsCollection extends \Spameri\Elastic\Entity\Collection\EntityCollection
185+ {
186+
187+ }
188+ ```
189+
190+ #### ElasticEntity collection property - ` Video.People `
191+ TODO Description
192+ ``` php
193+ namespace SpameriTests\Data\Entity\Video;
194+
195+
196+ class People extends \Spameri\Elastic\Entity\Collection\ElasticEntityCollection
197+ {
198+
199+ public function personByImdb(
200+ \SpameriTests\Data\Entity\Property\ImdbId $imdb
201+ ) : ?\SpameriTests\Data\Entity\Person
202+ {
203+ /** @var \SpameriTests\Data\Entity\Person $entity */
204+ foreach ($this->collection() as $entity) {
205+ if ($imdb->value() === $entity->identification()->imdb()->value()) {
206+ return $entity;
207+ }
208+ }
209+
210+ return NULL;
211+ }
212+ }
213+ ```
109214
110- #### ElasticEntity collection property - ` details.accessories `
111- TODO
0 commit comments