-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathEntityAttributeContainer.php
executable file
·262 lines (232 loc) · 6.79 KB
/
EntityAttributeContainer.php
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
<?php
/**
* Webiny Framework (http://www.webiny.com/framework)
*
* @copyright Copyright Webiny LTD
*/
namespace Webiny\Component\Entity;
use ArrayIterator;
use Traversable;
use Webiny\Component\Entity\Attribute\ArrayAttribute;
use Webiny\Component\Entity\Attribute\AbstractAttribute;
use Webiny\Component\Entity\Attribute\AttributeType;
use Webiny\Component\Entity\Attribute\BooleanAttribute;
use Webiny\Component\Entity\Attribute\CharAttribute;
use Webiny\Component\Entity\Attribute\DateAttribute;
use Webiny\Component\Entity\Attribute\DateTimeAttribute;
use Webiny\Component\Entity\Attribute\DynamicAttribute;
use Webiny\Component\Entity\Attribute\FloatAttribute;
use Webiny\Component\Entity\Attribute\GeoPointAttribute;
use Webiny\Component\Entity\Attribute\IntegerAttribute;
use Webiny\Component\Entity\Attribute\Many2ManyAttribute;
use Webiny\Component\Entity\Attribute\Many2OneAttribute;
use Webiny\Component\Entity\Attribute\ObjectAttribute;
use Webiny\Component\Entity\Attribute\One2ManyAttribute;
/**
* EntityAttributeContainer
* @package Webiny\Component\Entity
*/
class EntityAttributeContainer implements \ArrayAccess, \IteratorAggregate
{
public static $classMap = [
'boolean' => AttributeType::BOOLEAN,
'char' => AttributeType::CHAR,
'integer' => AttributeType::INTEGER,
'float' => AttributeType::FLOAT,
'arr' => AttributeType::ARR,
'object' => AttributeType::OBJECT,
'datetime' => AttributeType::DATE_TIME,
'date' => AttributeType::DATE,
'many2one' => AttributeType::MANY2ONE,
'one2many' => AttributeType::ONE2MANY,
'many2many' => AttributeType::MANY2MANY,
'dynamic' => AttributeType::DYNAMIC,
'geoPoint' => AttributeType::GEOPOINT
];
protected $entity;
protected $attributes = [];
protected $attribute;
/**
* @inheritDoc
*/
function __construct(AbstractEntity $entity)
{
$this->entity = $entity;
}
/**
* Create a new attribute
*
* @param $attribute
*
* @return $this
* @throws EntityException
*/
public function attr($attribute)
{
if (strpos($attribute, '_') !== false) {
throw new EntityException('Underscore is not allowed in attribute names (found in \'' . $attribute . '\')');
}
$this->attribute = $attribute;
return $this;
}
/**
* Remove one or more attributes
* Ex: remove('name', 'name2', 'name3');
*
* @param array|string ...$attributes
*/
public function remove(...$attributes)
{
foreach ($attributes as $attr) {
unset($this->attributes[$attr]);
}
}
/**
* Set attribute instance
*
* @param AbstractAttribute $attribute
*
* @return AbstractAttribute
*/
public function smart(AbstractAttribute $attribute)
{
$attribute->setName($this->attribute)->setParent($this->entity);
return $this->attributes[$this->attribute] = $attribute;
}
/**
* @return BooleanAttribute
*/
public function boolean()
{
return $this->attributes[$this->attribute] = new self::$classMap['boolean']($this->attribute, $this->entity);
}
/**
* @return ArrayAttribute
*/
public function arr()
{
return $this->attributes[$this->attribute] = new self::$classMap['arr']($this->attribute, $this->entity);
}
/**
* @return ObjectAttribute
*/
public function object()
{
return $this->attributes[$this->attribute] = new self::$classMap['object']($this->attribute, $this->entity);
}
/**
* @return IntegerAttribute
*/
public function integer()
{
return $this->attributes[$this->attribute] = new self::$classMap['integer']($this->attribute, $this->entity);
}
/**
* @return CharAttribute
*/
public function char()
{
return $this->attributes[$this->attribute] = new self::$classMap['char']($this->attribute, $this->entity);
}
/**
* @return DateTimeAttribute
*/
public function datetime()
{
return $this->attributes[$this->attribute] = new self::$classMap['datetime']($this->attribute, $this->entity);
}
/**
* @return DateAttribute
*/
public function date()
{
return $this->attributes[$this->attribute] = new self::$classMap['date']($this->attribute, $this->entity);
}
/**
* @return FloatAttribute
*/
public function float()
{
return $this->attributes[$this->attribute] = new self::$classMap['float']($this->attribute, $this->entity);
}
/**
* @return Many2OneAttribute
*/
public function many2one()
{
return $this->attributes[$this->attribute] = new self::$classMap['many2one']($this->attribute, $this->entity);
}
/**
* @param $relatedAttribute
*
* @return One2ManyAttribute
*/
public function one2many($relatedAttribute)
{
return $this->attributes[$this->attribute] = new self::$classMap['one2many']($this->attribute, $this->entity, $relatedAttribute);
}
/**
* @param string $collectionName Intermediate collection name
*
* @param string $thisField Field name containing ID of this entity
* @param string $refField Field name containing ID of referenced entity
*
* @return Many2ManyAttribute
*/
public function many2many($collectionName, $thisField, $refField)
{
$params = [$this->attribute, $thisField, $refField, $this->entity, $collectionName];
return $this->attributes[$this->attribute] = new self::$classMap['many2many'](...$params);
}
/**
* @param callable $callable
*
* @return DynamicAttribute
*/
public function dynamic($callable)
{
return $this->attributes[$this->attribute] = new self::$classMap['dynamic']($this->attribute, $this->entity, $callable);
}
/**
* @return GeoPointAttribute
*/
public function geoPoint()
{
return $this->attributes[$this->attribute] = new self::$classMap['geoPoint']($this->attribute, $this->entity);
}
/**
* @inheritdoc
*/
public function getIterator()
{
return new ArrayIterator($this->attributes);
}
/**
* @inheritdoc
*/
public function offsetExists($offset)
{
return array_key_exists($offset, $this->attributes);
}
/**
* @inheritdoc
*/
public function offsetGet($offset)
{
return $this->attributes[$offset];
}
/**
* @inheritdoc
*/
public function offsetSet($offset, $value)
{
$this->attributes[$offset] = $value;
}
/**
* @inheritdoc
*/
public function offsetUnset($offset)
{
unset($this->attributes[$offset]);
}
}