1 | <?php |
||
14 | abstract class Entity |
||
15 | { |
||
16 | |||
17 | /** |
||
18 | * @var array Columns |
||
19 | */ |
||
20 | protected static $columns = []; |
||
21 | |||
22 | /** |
||
23 | * @var array Internal entity properties |
||
24 | */ |
||
25 | protected $properties = []; |
||
26 | |||
27 | /** |
||
28 | * @var array Internal altered columns |
||
29 | */ |
||
30 | protected $alteredColumns = []; |
||
31 | |||
32 | /** |
||
33 | * @param int|null $id Entity ID |
||
34 | * @param \DateTime $modified Datetime of last modification |
||
35 | * @param \DateTime $created Datetime of creation |
||
36 | */ |
||
37 | public function __construct($id, \DateTime $modified, \DateTime $created) |
||
43 | |||
44 | /** |
||
45 | * Get entity columns |
||
46 | * |
||
47 | * @return array Entity columns |
||
48 | */ |
||
49 | 1 | public static function getColumns(): array |
|
62 | |||
63 | /** |
||
64 | * Set entity property |
||
65 | * |
||
66 | * @param string $key Entity property name |
||
67 | * @param object|integer|double|string|array|boolean|null $value Entity property value |
||
68 | * |
||
69 | * @throws InvalidEntityPropertyException If entity does not have that property |
||
70 | * @throws InvalidValueTypeForEntityPropertyException If value is of the wrong type |
||
71 | */ |
||
72 | 2 | public function set(string $key, $value) |
|
92 | |||
93 | /** |
||
94 | * Get entity property |
||
95 | * |
||
96 | * @param string $key Entity property |
||
97 | * |
||
98 | * @return object|integer|double|string|array|boolean|null Entity property value for given key |
||
99 | * |
||
100 | * @throws InvalidEntityPropertyException If entity does not have that property |
||
101 | */ |
||
102 | 2 | public function get(string $key) |
|
110 | |||
111 | /** |
||
112 | * Check if entity has been altered |
||
113 | * |
||
114 | * @return bool True if altered, otherwise false |
||
115 | */ |
||
116 | 1 | public function isAltered(): bool |
|
120 | |||
121 | /** |
||
122 | * Marks the entity as unaltered |
||
123 | */ |
||
124 | 1 | public function setUnaltered() |
|
128 | |||
129 | /** |
||
130 | * Get altered columns |
||
131 | * |
||
132 | * @return array |
||
133 | */ |
||
134 | 1 | public function getAlteredColumns(): array |
|
138 | |||
139 | /** |
||
140 | * Return table structur for saving |
||
141 | * Example: `[':{table_field_name}' => $this->fieldName]` |
||
142 | * |
||
143 | * @param bool $includeId Should the ID column be included |
||
144 | * |
||
145 | * @return array |
||
146 | */ |
||
147 | 2 | public function toArray(bool $includeId): array |
|
157 | |||
158 | 2 | public function alteredToArray(bool $includeId): array |
|
168 | |||
169 | protected function toArrayFromColumns(array $columns): array |
||
184 | |||
185 | /** |
||
186 | * Validate property for given value |
||
187 | * |
||
188 | * @param string $key Entity property name |
||
189 | * @param mixed $value Entity property value |
||
190 | * |
||
191 | * @return object|integer|double|string|array|boolean|null Value |
||
192 | * |
||
193 | * @throws InvalidEntityPropertyException If entity does not have that property |
||
194 | * @throws InvalidValueTypeForEntityPropertyException If value is of the wrong type |
||
195 | */ |
||
196 | 3 | protected function validatePropertyForValue(string $key, $value) |
|
241 | } |
||
242 |