1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the eav package. |
4
|
|
|
* |
5
|
|
|
* @author Aleksandr Drobotik <[email protected]> |
6
|
|
|
* @copyright 2023 Aleksandr Drobotik |
7
|
|
|
* @license https://opensource.org/license/mit The MIT License |
8
|
|
|
*/ |
9
|
|
|
declare(strict_types=1); |
10
|
|
|
|
11
|
|
|
namespace Drobotik\Eav; |
12
|
|
|
|
13
|
|
|
use Drobotik\Eav\Enum\_ENTITY; |
14
|
|
|
use Drobotik\Eav\Exception\EntityException; |
15
|
|
|
use Drobotik\Eav\Result\Result; |
16
|
|
|
use Drobotik\Eav\Trait\SingletonsTrait; |
17
|
|
|
|
18
|
|
|
class EntityGnome |
19
|
|
|
{ |
20
|
|
|
use SingletonsTrait; |
21
|
|
|
|
22
|
|
|
private Entity $entity; |
23
|
|
|
|
24
|
1 |
|
public function __construct(Entity $entity) |
25
|
|
|
{ |
26
|
1 |
|
$this->entity = $entity; |
27
|
|
|
} |
28
|
|
|
|
29
|
1 |
|
public function getEntity(): Entity |
30
|
|
|
{ |
31
|
1 |
|
return $this->entity; |
32
|
|
|
} |
33
|
|
|
|
34
|
10 |
|
public function beforeSave(): int |
35
|
|
|
{ |
36
|
10 |
|
$entity = $this->getEntity(); |
37
|
10 |
|
$set = $entity->getAttributeSet(); |
38
|
10 |
|
if (!$entity->hasKey()) { |
39
|
6 |
|
if (!$entity->hasDomainKey()) { |
40
|
1 |
|
EntityException::undefinedDomainKey(); |
41
|
|
|
} |
42
|
5 |
|
if (!$set->hasKey()) { |
43
|
1 |
|
EntityException::undefinedAttributeSetKey(); |
44
|
|
|
} |
45
|
4 |
|
$domainKey = $entity->getDomainKey(); |
46
|
4 |
|
$this->checkDomainExists($domainKey); |
47
|
3 |
|
$setKey = $set->getKey(); |
48
|
3 |
|
$this->checkAttrSetExist($setKey); |
49
|
2 |
|
$model = $this->makeEntityModel(); |
50
|
2 |
|
$key = $model->create([ |
51
|
2 |
|
_ENTITY::DOMAIN_ID->column() => $domainKey, |
52
|
2 |
|
_ENTITY::ATTR_SET_ID->column() => $setKey |
53
|
2 |
|
]); |
54
|
2 |
|
$entity->setKey($key); |
55
|
|
|
|
56
|
2 |
|
return 1; |
57
|
|
|
} |
58
|
4 |
|
$key = $entity->getKey(); |
59
|
4 |
|
$record = $this->checkEntityExists($key); |
60
|
3 |
|
$domainKey = (int) $record[_ENTITY::DOMAIN_ID->column()]; |
61
|
3 |
|
$setKey = (int) $record[_ENTITY::ATTR_SET_ID->column()]; |
62
|
|
|
|
63
|
3 |
|
$this->checkDomainExists($domainKey); |
64
|
2 |
|
$this->checkAttrSetExist($setKey); |
65
|
1 |
|
$set->setKey($setKey); |
66
|
1 |
|
$entity->setDomainKey($domainKey); |
67
|
|
|
|
68
|
1 |
|
return 2; |
69
|
|
|
} |
70
|
|
|
|
71
|
3 |
|
public function find(): Result |
72
|
|
|
{ |
73
|
3 |
|
$result = new Result(); |
74
|
|
|
|
75
|
3 |
|
$entity = $this->getEntity(); |
76
|
|
|
|
77
|
3 |
|
if (!$entity->hasKey()) { |
78
|
1 |
|
return $result->empty(); |
79
|
|
|
} |
80
|
2 |
|
$key = $entity->getKey(); |
81
|
|
|
|
82
|
2 |
|
$model = $this->makeEntityModel(); |
83
|
2 |
|
$record = $model->findByKey($key); |
84
|
|
|
|
85
|
2 |
|
if ($record === false) { |
86
|
1 |
|
return $result->notFound(); |
87
|
|
|
} |
88
|
|
|
|
89
|
1 |
|
$entity->setDomainKey($record[_ENTITY::DOMAIN_ID->column()]); |
90
|
1 |
|
$set = $entity->getAttributeSet(); |
91
|
1 |
|
$set->setKey($record[_ENTITY::ATTR_SET_ID->column()]); |
92
|
1 |
|
$set->fetchContainers(); |
93
|
|
|
|
94
|
1 |
|
$result->found(); |
95
|
|
|
|
96
|
1 |
|
return $result; |
97
|
|
|
} |
98
|
|
|
|
99
|
14 |
|
public function save(): Result |
100
|
|
|
{ |
101
|
14 |
|
$entity = $this->getEntity(); |
102
|
14 |
|
$result = new Result(); |
103
|
14 |
|
$operationType = $this->beforeSave(); |
104
|
7 |
|
$set = $entity->getAttributeSet(); |
105
|
7 |
|
$set->fetchContainers(); |
106
|
7 |
|
$valueResults = []; |
107
|
7 |
|
foreach ($set->getContainers() as $container) { |
108
|
2 |
|
$attribute = $container->getAttribute(); |
109
|
2 |
|
$valueResults[$attribute->getName()] = $container->getStrategy()->save(); |
110
|
|
|
} |
111
|
7 |
|
1 == $operationType |
112
|
3 |
|
? $result->created() |
113
|
4 |
|
: $result->updated(); |
114
|
7 |
|
$result->setData($valueResults); |
115
|
7 |
|
$bag = $entity->getBag(); |
116
|
7 |
|
$bag->clear(); |
117
|
|
|
|
118
|
7 |
|
return $result; |
119
|
|
|
} |
120
|
|
|
|
121
|
4 |
|
public function delete() |
122
|
|
|
{ |
123
|
4 |
|
$entity = $this->getEntity(); |
124
|
4 |
|
$set = $entity->getAttributeSet(); |
125
|
|
|
|
126
|
4 |
|
if (!$entity->hasKey()) { |
127
|
1 |
|
EntityException::undefinedEntityKey(); |
128
|
|
|
} |
129
|
3 |
|
if (!$set->hasKey()) { |
130
|
1 |
|
EntityException::undefinedAttributeSetKey(); |
131
|
|
|
} |
132
|
|
|
|
133
|
2 |
|
$result = new Result(); |
134
|
2 |
|
$set->fetchContainers(); |
135
|
2 |
|
$deleteResults = []; |
136
|
2 |
|
foreach ($set->getContainers() as $container) { |
137
|
1 |
|
$attribute = $container->getAttribute(); |
138
|
1 |
|
$deleteResults[$attribute->getName()] = $container->getStrategy()->delete(); |
139
|
|
|
} |
140
|
2 |
|
$entityModel = $this->makeEntityModel(); |
141
|
2 |
|
$recordResult = $entityModel->deleteByKey($entity->getKey()); |
142
|
2 |
|
if (!$recordResult) { |
143
|
1 |
|
return $result->notDeleted(); |
144
|
|
|
} |
145
|
|
|
|
146
|
1 |
|
$entity->setKey(0); |
147
|
1 |
|
$entity->setDomainKey(0); |
148
|
1 |
|
$set->setKey(0); |
149
|
1 |
|
$set->resetContainers(); |
150
|
|
|
|
151
|
1 |
|
$result->deleted(); |
152
|
1 |
|
$result->setData($deleteResults); |
153
|
|
|
|
154
|
1 |
|
return $result; |
155
|
|
|
} |
156
|
|
|
|
157
|
2 |
|
public function validate(): Result |
158
|
|
|
{ |
159
|
2 |
|
$result = new Result(); |
160
|
2 |
|
$result->validationPassed(); |
161
|
2 |
|
$entity = $this->getEntity(); |
162
|
2 |
|
$set = $entity->getAttributeSet(); |
163
|
2 |
|
$set->fetchContainers(); |
164
|
2 |
|
$errors = []; |
165
|
2 |
|
foreach ($set->getContainers() as $container) { |
166
|
2 |
|
$validationResult = $container->getValueValidator()->validateField(); |
167
|
2 |
|
if (!is_null($validationResult)) { |
168
|
1 |
|
$errors[$container->getAttribute()->getName()] = $validationResult; |
169
|
|
|
} |
170
|
|
|
} |
171
|
2 |
|
if (count($errors) > 0) { |
172
|
1 |
|
$result->validationFails(); |
173
|
1 |
|
$result->setData($errors); |
174
|
|
|
} |
175
|
|
|
|
176
|
2 |
|
return $result; |
177
|
|
|
} |
178
|
|
|
|
179
|
1 |
|
public function toArray(): array |
180
|
|
|
{ |
181
|
1 |
|
$result = []; |
182
|
1 |
|
$entity = $this->getEntity(); |
183
|
1 |
|
foreach ($entity->getAttributeSet()->getContainers() as $container) { |
184
|
1 |
|
$result[$container->getAttribute()->getName()] = $container->getValueManager()->getValue(); |
185
|
|
|
} |
186
|
|
|
|
187
|
1 |
|
return $result; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* @throws EntityException |
192
|
|
|
*/ |
193
|
|
|
private function checkEntityExists(int $key): array |
194
|
|
|
{ |
195
|
|
|
$entity = $this->makeEntityModel(); |
196
|
|
|
|
197
|
|
|
$result = $entity->findByKey($key); |
198
|
|
|
if ($result === false) |
199
|
|
|
EntityException::entityNotFound(); |
200
|
|
|
|
201
|
|
|
return $result; |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* @throws EntityException |
206
|
|
|
*/ |
207
|
1 |
|
private function checkDomainExists(int $key): void |
208
|
|
|
{ |
209
|
1 |
|
$domain = $this->makeDomainModel(); |
210
|
|
|
|
211
|
1 |
|
if ($domain->findByKey($key) === false) |
212
|
1 |
|
EntityException::domainNotFound(); |
213
|
|
|
} |
214
|
|
|
|
215
|
1 |
|
private function checkAttrSetExist(int $key): void |
216
|
|
|
{ |
217
|
1 |
|
$model = $this->makeAttributeSetModel(); |
218
|
|
|
|
219
|
1 |
|
if($model->findByKey($key) === false) { |
220
|
1 |
|
EntityException::attrSetNotFound(); |
221
|
|
|
} |
222
|
|
|
} |
223
|
|
|
} |
224
|
|
|
|