|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* apparat-object |
|
5
|
|
|
* |
|
6
|
|
|
* @category Apparat |
|
7
|
|
|
* @package Apparat\Object |
|
8
|
|
|
* @subpackage Apparat\Object\Application |
|
9
|
|
|
* @author Joschi Kuphal <[email protected]> / @jkphl |
|
10
|
|
|
* @copyright Copyright © 2016 Joschi Kuphal <[email protected]> / @jkphl |
|
11
|
|
|
* @license http://opensource.org/licenses/MIT The MIT License (MIT) |
|
12
|
|
|
*/ |
|
13
|
|
|
|
|
14
|
|
|
/*********************************************************************************** |
|
15
|
|
|
* The MIT License (MIT) |
|
16
|
|
|
* |
|
17
|
|
|
* Copyright © 2016 Joschi Kuphal <[email protected]> / @jkphl |
|
18
|
|
|
* |
|
19
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy of |
|
20
|
|
|
* this software and associated documentation files (the "Software"), to deal in |
|
21
|
|
|
* the Software without restriction, including without limitation the rights to |
|
22
|
|
|
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of |
|
23
|
|
|
* the Software, and to permit persons to whom the Software is furnished to do so, |
|
24
|
|
|
* subject to the following conditions: |
|
25
|
|
|
* |
|
26
|
|
|
* The above copyright notice and this permission notice shall be included in all |
|
27
|
|
|
* copies or substantial portions of the Software. |
|
28
|
|
|
* |
|
29
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
30
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS |
|
31
|
|
|
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR |
|
32
|
|
|
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER |
|
33
|
|
|
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
|
34
|
|
|
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
|
35
|
|
|
***********************************************************************************/ |
|
36
|
|
|
|
|
37
|
|
|
namespace Apparat\Object\Domain\Model\Properties; |
|
38
|
|
|
|
|
39
|
|
|
use Apparat\Object\Domain\Contract\SerializablePropertyInterface; |
|
40
|
|
|
use Apparat\Object\Domain\Model\Object\ObjectInterface; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Abstract object properties collection |
|
44
|
|
|
* |
|
45
|
|
|
* @package Apparat\Object |
|
46
|
|
|
* @subpackage Apparat\Object\Application |
|
47
|
|
|
*/ |
|
48
|
|
|
abstract class AbstractProperties implements PropertiesInterface |
|
49
|
|
|
{ |
|
50
|
|
|
/** |
|
51
|
|
|
* Property data |
|
52
|
|
|
* |
|
53
|
|
|
* @var array |
|
54
|
|
|
*/ |
|
55
|
|
|
protected $data = []; |
|
56
|
|
|
/** |
|
57
|
|
|
* Owner object |
|
58
|
|
|
* |
|
59
|
|
|
* @var ObjectInterface |
|
60
|
|
|
*/ |
|
61
|
|
|
protected $object = null; |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Meta properties constructor |
|
65
|
|
|
* |
|
66
|
|
|
* @param array $data Property data |
|
67
|
|
|
* @param ObjectInterface $object Owner object |
|
68
|
|
|
*/ |
|
69
|
39 |
|
public function __construct(array $data, ObjectInterface $object) |
|
70
|
|
|
{ |
|
71
|
39 |
|
$this->data = $data; |
|
72
|
39 |
|
$this->object = $object; |
|
73
|
39 |
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Return the owner object |
|
77
|
|
|
* |
|
78
|
|
|
* @return ObjectInterface Owner object |
|
79
|
|
|
*/ |
|
80
|
3 |
|
public function getObject() |
|
81
|
|
|
{ |
|
82
|
3 |
|
return $this->object; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Normalize and sort a property value list |
|
87
|
|
|
* |
|
88
|
|
|
* @param array $values Property values |
|
89
|
|
|
* @return array Normalized and sorted property values |
|
90
|
|
|
*/ |
|
91
|
30 |
|
protected function normalizeSortedPropertyValues(array $values) |
|
92
|
|
|
{ |
|
93
|
30 |
|
$values = array_unique($values); |
|
94
|
30 |
|
sort($values, SORT_NATURAL); |
|
95
|
30 |
|
return $values; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Mutate a string property |
|
100
|
|
|
* |
|
101
|
|
|
* @param string $property Property name |
|
102
|
|
|
* @param string $value New value |
|
103
|
|
|
* @return $this|AbstractProperties Self reference or clone |
|
104
|
|
|
*/ |
|
105
|
3 |
|
protected function mutateStringProperty($property, $value) |
|
106
|
|
|
{ |
|
107
|
|
|
|
|
108
|
|
|
// If the new value differs from the current: Return clone |
|
109
|
3 |
|
if (strval($this->$property) !== strval($value)) { |
|
110
|
3 |
|
$collection = clone $this; |
|
111
|
3 |
|
$collection->$property = strval($value); |
|
112
|
3 |
|
return $collection; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
// Else: return self reference |
|
116
|
1 |
|
return $this; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* Mutate a float property |
|
121
|
|
|
* |
|
122
|
|
|
* @param string $property Property name |
|
123
|
|
|
* @param float $value New value |
|
124
|
|
|
* @return $this|AbstractProperties Self reference or clone |
|
125
|
|
|
*/ |
|
126
|
1 |
|
protected function mutateFloatProperty($property, $value) |
|
127
|
|
|
{ |
|
128
|
|
|
|
|
129
|
|
|
// If the new value differs from the current: Return clone |
|
130
|
1 |
|
if ($this->$property !== floatval($value)) { |
|
131
|
1 |
|
$collection = clone $this; |
|
132
|
1 |
|
$collection->$property = floatval($value); |
|
133
|
1 |
|
return $collection; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
// Else: return self reference |
|
137
|
1 |
|
return $this; |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* Mutate a list property |
|
142
|
|
|
* |
|
143
|
|
|
* @param string $property Property name |
|
144
|
|
|
* @param array $values New values |
|
145
|
|
|
* @return $this|AbstractProperties Self reference or clone |
|
146
|
|
|
*/ |
|
147
|
1 |
|
protected function mutateListProperty($property, array $values) |
|
148
|
|
|
{ |
|
149
|
|
|
// If the new values differ from the current ones: Return clone |
|
150
|
1 |
|
if (array_diff($this->$property, $values) || array_diff($values, $this->$property)) { |
|
151
|
1 |
|
$collection = clone $this; |
|
152
|
1 |
|
$collection->$property = $values; |
|
153
|
1 |
|
return $collection; |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
// Else: return self reference |
|
157
|
1 |
|
return $this; |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
/** |
|
161
|
|
|
* Mutate a neste properties property |
|
162
|
|
|
* |
|
163
|
|
|
* @param string $property Property name |
|
164
|
|
|
* @param PropertiesInterface $value Nested properties |
|
165
|
|
|
* @return $this|AbstractProperties Self reference or clone |
|
166
|
|
|
*/ |
|
167
|
1 |
|
protected function mutatePropertiesProperty($property, PropertiesInterface $value) |
|
168
|
|
|
{ |
|
169
|
|
|
// If the new value differs from the current one: Return clone |
|
170
|
1 |
|
if (spl_object_hash($this->$property) !== spl_object_hash($value)) { |
|
171
|
1 |
|
$collection = clone $this; |
|
172
|
1 |
|
$collection->$property = $value; |
|
173
|
1 |
|
return $collection; |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
// Else: return self reference |
|
177
|
1 |
|
return $this; |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
/** |
|
181
|
|
|
* Return the property values as array |
|
182
|
|
|
* |
|
183
|
|
|
* @param bool $serialize Serialize property objects |
|
184
|
|
|
* @return array Property values |
|
185
|
|
|
*/ |
|
186
|
9 |
|
public function toArray($serialize = true) |
|
187
|
|
|
{ |
|
188
|
9 |
|
return $this->toSerializedArray($serialize, $this->data); |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
/** |
|
192
|
|
|
* Return the potentially serialized property values |
|
193
|
|
|
* |
|
194
|
|
|
* @param boolean $serialize Serialize objects |
|
195
|
|
|
* @param array $data Property values |
|
196
|
|
|
* @return array Serialized property values |
|
197
|
|
|
*/ |
|
198
|
9 |
|
protected function toSerializedArray($serialize, array $data) |
|
199
|
|
|
{ |
|
200
|
|
|
// Filter all empty values |
|
201
|
9 |
|
$data = array_filter($data); |
|
202
|
|
|
|
|
203
|
|
|
// If the values should be serialized |
|
204
|
9 |
|
if ($serialize) { |
|
205
|
|
|
// Run through all properties |
|
206
|
9 |
|
while (list($property, $value) = each($data)) { |
|
207
|
|
|
// If the value is an array itself: Recurse |
|
208
|
9 |
|
if (is_array($value)) { |
|
209
|
4 |
|
$data[$property] = $this->toSerializedArray($serialize, $value); |
|
210
|
|
|
// Else if the value is serializable |
|
211
|
4 |
|
} elseif ( |
|
212
|
9 |
|
is_object($value) && |
|
213
|
9 |
|
(new \ReflectionClass($value))->implementsInterface(SerializablePropertyInterface::class) |
|
214
|
9 |
|
) { |
|
215
|
|
|
/** @var $value SerializablePropertyInterface */ |
|
216
|
|
|
$data[$property] = $value->serialize(); |
|
217
|
|
|
} |
|
218
|
9 |
|
} |
|
219
|
9 |
|
} |
|
220
|
|
|
|
|
221
|
9 |
|
return $data; |
|
222
|
|
|
} |
|
223
|
|
|
} |
|
224
|
|
|
|