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\Model\Object\ObjectInterface; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Abstract object properties collection |
43
|
|
|
* |
44
|
|
|
* @package Apparat\Object |
45
|
|
|
* @subpackage Apparat\Object\Application |
46
|
|
|
*/ |
47
|
|
|
abstract class AbstractProperties implements PropertiesInterface |
48
|
|
|
{ |
49
|
|
|
/** |
50
|
|
|
* Property data |
51
|
|
|
* |
52
|
|
|
* @var array |
53
|
|
|
*/ |
54
|
|
|
protected $data = []; |
55
|
|
|
/** |
56
|
|
|
* Owner object |
57
|
|
|
* |
58
|
|
|
* @var ObjectInterface |
59
|
|
|
*/ |
60
|
|
|
protected $object = null; |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Meta properties constructor |
64
|
|
|
* |
65
|
|
|
* @param array $data Property data |
66
|
|
|
* @param ObjectInterface $object Owner object |
67
|
|
|
*/ |
68
|
23 |
|
public function __construct(array $data, ObjectInterface $object) |
69
|
|
|
{ |
70
|
23 |
|
$this->data = $data; |
71
|
23 |
|
$this->object = $object; |
72
|
23 |
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Return the owner object |
76
|
|
|
* |
77
|
|
|
* @return ObjectInterface Owner object |
78
|
|
|
*/ |
79
|
|
|
public function getObject() |
80
|
|
|
{ |
81
|
|
|
return $this->object; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Normalize and sort a property value list |
86
|
|
|
* |
87
|
|
|
* @param array $values Property values |
88
|
|
|
* @return array Normalized and sorted property values |
89
|
|
|
*/ |
90
|
19 |
|
protected function normalizeSortedPropertyValues(array $values) |
91
|
|
|
{ |
92
|
19 |
|
$values = array_unique($values); |
93
|
19 |
|
sort($values, SORT_NATURAL); |
94
|
19 |
|
return $values; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Mutate a string property |
99
|
|
|
* |
100
|
|
|
* @param string $property Property name |
101
|
|
|
* @param string $value New value |
102
|
|
|
* @return $this|AbstractProperties Self reference or clone |
103
|
|
|
*/ |
104
|
1 |
|
protected function mutateStringProperty($property, $value) |
105
|
|
|
{ |
106
|
|
|
|
107
|
|
|
// If the new value differs from the current: Return clone |
108
|
1 |
|
if (strval($this->$property) !== strval($value)) { |
109
|
1 |
|
$collection = clone $this; |
110
|
1 |
|
$collection->$property = strval($value); |
111
|
1 |
|
return $collection; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
// Else: return self reference |
115
|
1 |
|
return $this; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Mutate a float property |
120
|
|
|
* |
121
|
|
|
* @param string $property Property name |
122
|
|
|
* @param float $value New value |
123
|
|
|
* @return $this|AbstractProperties Self reference or clone |
124
|
|
|
*/ |
125
|
1 |
|
protected function mutateFloatProperty($property, $value) |
126
|
|
|
{ |
127
|
|
|
|
128
|
|
|
// If the new value differs from the current: Return clone |
129
|
1 |
|
if ($this->$property !== floatval($value)) { |
130
|
1 |
|
$collection = clone $this; |
131
|
1 |
|
$collection->$property = floatval($value); |
132
|
1 |
|
return $collection; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
// Else: return self reference |
136
|
|
|
return $this; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Mutate a list property |
141
|
|
|
* |
142
|
|
|
* @param string $property Property name |
143
|
|
|
* @param array $values New values |
144
|
|
|
* @return $this|AbstractProperties Self reference or clone |
145
|
|
|
*/ |
146
|
1 |
|
protected function mutateListProperty($property, array $values) |
147
|
|
|
{ |
148
|
|
|
// If the new values differ from the current ones: Return clone |
149
|
1 |
|
if (array_diff($this->$property, $values) || array_diff($values, $this->$property)) { |
150
|
1 |
|
$collection = clone $this; |
151
|
1 |
|
$collection->$property = $values; |
152
|
1 |
|
return $collection; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
// Else: return self reference |
156
|
1 |
|
return $this; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Mutate a neste properties property |
161
|
|
|
* |
162
|
|
|
* @param string $property Property name |
163
|
|
|
* @param PropertiesInterface $value Nested properties |
164
|
|
|
* @return $this|AbstractProperties Self reference or clone |
165
|
|
|
*/ |
166
|
1 |
|
protected function mutatePropertiesProperty($property, PropertiesInterface $value) |
167
|
|
|
{ |
168
|
|
|
// If the new value differs from the current one: Return clone |
169
|
1 |
|
if (spl_object_hash($this->$property) !== spl_object_hash($value)) { |
170
|
1 |
|
$collection = clone $this; |
171
|
1 |
|
$collection->$property = $value; |
172
|
1 |
|
return $collection; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
// Else: return self reference |
176
|
|
|
return $this; |
177
|
|
|
} |
178
|
|
|
} |
179
|
|
|
|