-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathEntity.php
More file actions
executable file
·221 lines (197 loc) · 6.13 KB
/
Entity.php
File metadata and controls
executable file
·221 lines (197 loc) · 6.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
<?php
/**
* Webiny Framework (http://www.webiny.com/framework)
*
* @copyright Copyright Webiny LTD
*/
namespace Webiny\Component\Entity;
use Webiny\Component\Entity\Attribute\AbstractAttribute;
use Webiny\Component\Entity\Attribute\Validation\ValidatorInterface;
use Webiny\Component\Entity\Attribute\Validation\Validators\CountryCode;
use Webiny\Component\Entity\Attribute\Validation\Validators\CreditCardNumber;
use Webiny\Component\Entity\Attribute\Validation\Validators\Email;
use Webiny\Component\Entity\Attribute\Validation\Validators\EuVatNumber;
use Webiny\Component\Entity\Attribute\Validation\Validators\GeoLocation;
use Webiny\Component\Entity\Attribute\Validation\Validators\GreaterThan;
use Webiny\Component\Entity\Attribute\Validation\Validators\GreaterThanOrEqual;
use Webiny\Component\Entity\Attribute\Validation\Validators\InArray;
use Webiny\Component\Entity\Attribute\Validation\Validators\Integer;
use Webiny\Component\Entity\Attribute\Validation\Validators\LessThan;
use Webiny\Component\Entity\Attribute\Validation\Validators\LessThanOrEqual;
use Webiny\Component\Entity\Attribute\Validation\Validators\MaxLength;
use Webiny\Component\Entity\Attribute\Validation\Validators\MinLength;
use Webiny\Component\Entity\Attribute\Validation\Validators\Number;
use Webiny\Component\Entity\Attribute\Validation\Validators\Password;
use Webiny\Component\Entity\Attribute\Validation\Validators\Phone;
use Webiny\Component\Entity\Attribute\Validation\Validators\Regex;
use Webiny\Component\Entity\Attribute\Validation\Validators\Required;
use Webiny\Component\Entity\Attribute\Validation\Validators\Unique;
use Webiny\Component\Entity\Attribute\Validation\Validators\Url;
use Webiny\Component\Mongo\Mongo;
use Webiny\Component\Mongo\MongoTrait;
use Webiny\Component\ServiceManager\ServiceManagerTrait;
use Webiny\Component\StdLib\ComponentTrait;
use Webiny\Component\StdLib\SingletonTrait;
use Webiny\Component\StdLib\StdLibTrait;
/**
* Entity component main class
* Use this class to configure Entity component.
*
* @package \Webiny\Component\Entity
*/
class Entity
{
use MongoTrait, ComponentTrait, SingletonTrait, ServiceManagerTrait, StdLibTrait;
/**
* @var null|Mongo
*/
protected static $database = null;
/**
* @var array
*/
private $validators = [];
/**
* @var array
*/
public $pool;
/**
* Get entity database
* @return Mongo
*/
public function getDatabase()
{
if (self::$database === null) {
self::$database = self::mongo(self::getConfig()->Database);
}
return self::$database;
}
/**
* Set entity database
*
* @param Mongo $mongoDatabase
*/
public function setDatabase(Mongo $mongoDatabase)
{
self::$database = $mongoDatabase;
}
/**
* Get entity instance or false if entity does not exist
*
* @param $class
* @param $id
*
* @return bool|AbstractEntity
*/
public function get($class, $id)
{
return $this->pool[$class][$id] ?? false;
}
/**
* Add instance to the pool
*
* @param $instance
*
* @return mixed
*/
public function add($instance)
{
$class = get_class($instance);
$entityPool = $this->pool[$class] ?? [];
$entityPool[$instance->id] = $instance;
$this->pool[$class] = $entityPool;
return $instance;
}
/**
* Add attribute validator to Entity component
*
* @param ValidatorInterface $validator
*
* @return $this
*/
public function addValidator(ValidatorInterface $validator)
{
$this->validators[$validator->getName()] = $validator;
return $this;
}
public function getValidator($name)
{
return $this->validators[$name];
}
/**
* Remove instance from pool
*
* @param $instance
*
* @return bool
*/
public function remove(AbstractEntity $instance)
{
$class = get_class($instance);
$entityPool = $this->pool[$class] ?? [];
$id = '' . $instance->id;
if (isset($entityPool[$id])) {
unset($entityPool[$id]);
$this->pool[$class] = $entityPool;
}
unset($instance);
return true;
}
/**
* Remove all loaded instances from pool
*/
public function reset()
{
$this->pool = [];
}
protected function init()
{
// Create entity cache pool
$this->pool = [];
// Load built-in validators
$builtInValidators = [
new Email(),
new GreaterThan(),
new GreaterThanOrEqual(),
new GeoLocation(),
new LessThan(),
new LessThanOrEqual(),
new MinLength(),
new MaxLength(),
new InArray(),
new Number(),
new Integer(),
new Url(),
new Password(),
new Required(),
new Phone(),
new CountryCode(),
new CreditCardNumber(),
new EuVatNumber(),
new Unique(),
new Regex()
];
/* @var $v ValidatorInterface */
foreach ($builtInValidators as $v) {
$this->validators[$v->getName()] = $v;
}
// Load validators registered as a service
$validators = $this->servicesByTag('entity-validator', ValidatorInterface::class);
foreach ($validators as $v) {
$this->validators[$v->getName()] = $v;
}
}
protected static function postSetConfig()
{
// If attribute class maps are defined - set them into EntityAttributeBuilder
$classMap = Entity::getConfig()->get('Attributes');
if ($classMap) {
foreach ($classMap as $attr => $class) {
if (!in_array(AbstractAttribute::class, class_parents($class))) {
$message = 'Attribute class for attribute "%s" must extend %s';
throw new EntityException($message, [$attr, AbstractAttribute::class]);
}
EntityAttributeContainer::$classMap[$attr] = $class;
}
}
}
}