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
|
|
|
* Absolute URL property |
64
|
|
|
* |
65
|
|
|
* @var string |
66
|
|
|
*/ |
67
|
|
|
const PROPERTY_ABSOLUTE_URL = 'absoluteUrl'; |
68
|
|
|
/** |
69
|
|
|
* Canonical URL property |
70
|
|
|
* |
71
|
|
|
* @var string |
72
|
|
|
*/ |
73
|
|
|
const PROPERTY_CANONICAL_URL = 'canonicalUrl'; |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Meta properties constructor |
77
|
|
|
* |
78
|
|
|
* @param array $data Property data |
79
|
|
|
* @param ObjectInterface $object Owner object |
80
|
|
|
*/ |
81
|
41 |
|
public function __construct(array $data, ObjectInterface $object) |
82
|
|
|
{ |
83
|
41 |
|
$this->data = $data; |
84
|
41 |
|
$this->object = $object; |
85
|
41 |
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Return the owner object |
89
|
|
|
* |
90
|
|
|
* @return ObjectInterface Owner object |
91
|
|
|
*/ |
92
|
3 |
|
public function getObject() |
93
|
|
|
{ |
94
|
3 |
|
return $this->object; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Normalize and sort a property value list |
99
|
|
|
* |
100
|
|
|
* @param array $values Property values |
101
|
|
|
* @return array Normalized and sorted property values |
102
|
|
|
*/ |
103
|
32 |
|
protected function normalizeSortedPropertyValues(array $values) |
104
|
|
|
{ |
105
|
32 |
|
$values = array_unique($values); |
106
|
32 |
|
sort($values, SORT_NATURAL); |
107
|
32 |
|
return $values; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Mutate a string property |
112
|
|
|
* |
113
|
|
|
* @param string $property Property name |
114
|
|
|
* @param string $value New value |
115
|
|
|
* @return $this|AbstractProperties Self reference or clone |
116
|
|
|
*/ |
117
|
3 |
|
protected function mutateStringProperty($property, $value) |
118
|
|
|
{ |
119
|
|
|
|
120
|
|
|
// If the new value differs from the current: Return clone |
121
|
3 |
|
if (strval($this->$property) !== strval($value)) { |
122
|
3 |
|
$collection = clone $this; |
123
|
3 |
|
$collection->$property = strval($value); |
124
|
3 |
|
return $collection; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
// Else: return self reference |
128
|
1 |
|
return $this; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Mutate a float property |
133
|
|
|
* |
134
|
|
|
* @param string $property Property name |
135
|
|
|
* @param float $value New value |
136
|
|
|
* @return $this|AbstractProperties Self reference or clone |
137
|
|
|
*/ |
138
|
1 |
|
protected function mutateFloatProperty($property, $value) |
139
|
|
|
{ |
140
|
|
|
|
141
|
|
|
// If the new value differs from the current: Return clone |
142
|
1 |
|
if ($this->$property !== floatval($value)) { |
143
|
1 |
|
$collection = clone $this; |
144
|
1 |
|
$collection->$property = floatval($value); |
145
|
1 |
|
return $collection; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
// Else: return self reference |
149
|
1 |
|
return $this; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Mutate a list property |
154
|
|
|
* |
155
|
|
|
* @param string $property Property name |
156
|
|
|
* @param array $values New values |
157
|
|
|
* @return $this|AbstractProperties Self reference or clone |
158
|
|
|
*/ |
159
|
1 |
|
protected function mutateListProperty($property, array $values) |
160
|
|
|
{ |
161
|
|
|
// If the new values differ from the current ones: Return clone |
162
|
1 |
|
if (array_diff($this->$property, $values) || array_diff($values, $this->$property)) { |
163
|
1 |
|
$collection = clone $this; |
164
|
1 |
|
$collection->$property = $values; |
165
|
1 |
|
return $collection; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
// Else: return self reference |
169
|
1 |
|
return $this; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* Mutate a neste properties property |
174
|
|
|
* |
175
|
|
|
* @param string $property Property name |
176
|
|
|
* @param PropertiesInterface $value Nested properties |
177
|
|
|
* @return $this|AbstractProperties Self reference or clone |
178
|
|
|
*/ |
179
|
1 |
|
protected function mutatePropertiesProperty($property, PropertiesInterface $value) |
180
|
|
|
{ |
181
|
|
|
// If the new value differs from the current one: Return clone |
182
|
1 |
|
if (spl_object_hash($this->$property) !== spl_object_hash($value)) { |
183
|
1 |
|
$collection = clone $this; |
184
|
1 |
|
$collection->$property = $value; |
185
|
1 |
|
return $collection; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
// Else: return self reference |
189
|
1 |
|
return $this; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* Return the property values as array |
194
|
|
|
* |
195
|
|
|
* @param bool $serialize Serialize property objects |
196
|
|
|
* @return array Property values |
197
|
|
|
*/ |
198
|
9 |
|
public function toArray($serialize = true) |
199
|
|
|
{ |
200
|
9 |
|
return $this->toSerializedArray($serialize, $this->data); |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* Return the potentially serialized property values |
205
|
|
|
* |
206
|
|
|
* @param boolean $serialize Serialize objects |
207
|
|
|
* @param array $data Property values |
208
|
|
|
* @return array Serialized property values |
209
|
|
|
*/ |
210
|
9 |
|
protected function toSerializedArray($serialize, array $data) |
211
|
|
|
{ |
212
|
|
|
// Filter all empty values |
213
|
9 |
|
$data = array_filter($data); |
214
|
|
|
|
215
|
|
|
// If the values should be serialized |
216
|
9 |
|
if ($serialize) { |
217
|
|
|
// Run through all properties |
218
|
9 |
|
while (list($property, $value) = each($data)) { |
219
|
|
|
// If the value is an array itself: Recurse |
220
|
9 |
|
if (is_array($value)) { |
221
|
4 |
|
$data[$property] = $this->toSerializedArray($serialize, $value); |
222
|
|
|
// Else if the value is serializable |
223
|
9 |
|
} elseif (is_object($value) && |
224
|
9 |
|
(new \ReflectionClass($value))->implementsInterface(SerializablePropertyInterface::class) |
225
|
|
|
) { |
226
|
|
|
/** @var $value SerializablePropertyInterface */ |
227
|
|
|
$data[$property] = $value->serialize(); |
228
|
|
|
} |
229
|
|
|
} |
230
|
|
|
} |
231
|
|
|
|
232
|
9 |
|
return $data; |
233
|
|
|
} |
234
|
|
|
} |
235
|
|
|
|