1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* apparat/object |
5
|
|
|
* |
6
|
|
|
* @category Apparat |
7
|
|
|
* @package Apparat\Server |
8
|
|
|
* @subpackage Apparat\Object\Application\Model\Properties |
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\Application\Model\Properties\Domain; |
38
|
|
|
|
39
|
|
|
use Apparat\Kernel\Ports\Kernel; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Abstract domain properties |
43
|
|
|
* |
44
|
|
|
* @package Apparat\Object |
45
|
|
|
* @subpackage Apparat\Object\Application |
46
|
|
|
*/ |
47
|
|
|
abstract class AbstractDomainProperties extends \Apparat\Object\Domain\Model\Properties\AbstractDomainProperties |
48
|
|
|
{ |
49
|
|
|
/** |
50
|
|
|
* Name |
51
|
|
|
* |
52
|
|
|
* @var string |
53
|
|
|
*/ |
54
|
|
|
const NAME = 'name'; |
55
|
|
|
/** |
56
|
|
|
* Publication date & time |
57
|
|
|
* |
58
|
|
|
* @var string |
59
|
|
|
*/ |
60
|
|
|
const PUBLISHED = 'published'; |
61
|
|
|
/** |
62
|
|
|
* Modification date & time |
63
|
|
|
* |
64
|
|
|
* @var string |
65
|
|
|
*/ |
66
|
|
|
const UPDATED = 'updated'; |
67
|
|
|
/** |
68
|
|
|
* Authors |
69
|
|
|
* |
70
|
|
|
* @var string |
71
|
|
|
*/ |
72
|
|
|
const AUTHOR = 'author'; |
73
|
|
|
/** |
74
|
|
|
* Categories |
75
|
|
|
* |
76
|
|
|
* @var string |
77
|
|
|
*/ |
78
|
|
|
const CATEGORY = 'category'; |
79
|
|
|
/** |
80
|
|
|
* URL |
81
|
|
|
* |
82
|
|
|
* @var string |
83
|
|
|
*/ |
84
|
|
|
const URL = 'url'; |
85
|
|
|
/** |
86
|
|
|
* UID |
87
|
|
|
* |
88
|
|
|
* @var string |
89
|
|
|
*/ |
90
|
|
|
const UID = 'uid'; |
91
|
|
|
/** |
92
|
|
|
* Location |
93
|
|
|
* |
94
|
|
|
* @var string |
95
|
|
|
*/ |
96
|
|
|
const LOCATION = 'location'; |
97
|
|
|
/** |
98
|
|
|
* Syndication URLs |
99
|
|
|
* |
100
|
|
|
* @var string |
101
|
|
|
*/ |
102
|
|
|
const SYNDICATION = 'syndication'; |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Set a property value by path list and base data |
106
|
|
|
* |
107
|
|
|
* @param array $propertyPath Path list |
108
|
|
|
* @param array $propertyTree Base data |
109
|
|
|
* @param mixed $value Property value |
110
|
|
|
* @return AbstractDomainProperties Self reference |
111
|
|
|
*/ |
112
|
4 |
|
protected function setPropertyPath(array $propertyPath, array $propertyTree, $value) |
113
|
|
|
{ |
114
|
|
|
// Traverse the property tree and find the property node to set |
115
|
4 |
|
$created = false; |
116
|
4 |
|
$propertyModel = null; |
117
|
4 |
|
$data =& $this->findPropertyNode($propertyPath, $propertyTree, $created, $propertyModel); |
118
|
|
|
|
119
|
|
|
// If a new property is created with a non-empty value or an existing property is altered: Mutate |
120
|
4 |
|
if ($created ? !empty($value) : !$this->assertEquals($data, $value)) { |
121
|
4 |
|
$this->setPropertyValue($data, $value, $propertyPath, $propertyModel); |
122
|
4 |
|
return new static($propertyTree, $this->object); |
123
|
|
|
} |
124
|
|
|
|
125
|
1 |
|
return $this; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Traverse the property tree and return a node |
130
|
|
|
* |
131
|
|
|
* @param array $propertyPath Property name path |
132
|
|
|
* @param array $propertyTree Copy of the current property tree |
133
|
|
|
* @param boolean $created Property has been created |
134
|
|
|
* @param PropertyModel $propertyModel Property model |
135
|
|
|
* @return mixed Property node |
136
|
|
|
* @throws DomainException If an invalid subproperty should be allocated |
137
|
|
|
*/ |
138
|
4 |
|
protected function &findPropertyNode( |
139
|
|
|
array $propertyPath, |
140
|
|
|
array &$propertyTree, |
141
|
|
|
&$created, |
142
|
|
|
PropertyModel &$propertyModel = null |
143
|
|
|
) { |
144
|
4 |
|
$propertyModel = null; |
145
|
4 |
|
$propertyModelName = 'pm'; |
146
|
4 |
|
$propertyPathSteps = []; |
147
|
4 |
|
$data =& $propertyTree; |
148
|
|
|
|
149
|
|
|
// Run through all sub-properties |
150
|
4 |
|
foreach ($propertyPath as $property) { |
151
|
|
|
// If an invalid sub-property should be allocated |
152
|
4 |
|
if ($propertyModel !== null) { |
153
|
1 |
|
throw new DomainException( |
154
|
|
|
sprintf( |
155
|
1 |
|
'Property data model of "%s" does not allow sub-properties', |
156
|
1 |
|
implode(self::PROPERTY_TRAVERSAL_SEPARATOR, $propertyPathSteps) |
157
|
|
|
), |
158
|
1 |
|
DomainException::INVALID_DOMAIN_SUBPROPERTY |
159
|
|
|
); |
160
|
|
|
} |
161
|
|
|
|
162
|
4 |
|
$propertyPathSteps[] = $property; |
163
|
4 |
|
$propertyModelName .= ucfirst($property); |
164
|
|
|
/** @var PropertyModel $propertyModel */ |
165
|
4 |
|
$propertyModel = isset($this->$propertyModelName) ? |
166
|
4 |
|
Kernel::create(PropertyModel::class, array_merge([$this->object], $this->$propertyModelName)) : null; |
167
|
|
|
|
168
|
|
|
// If the sub-property doesn't exist |
169
|
4 |
|
if (!array_key_exists($property, $data)) { |
170
|
4 |
|
$data[$property] = (($propertyModel === null) || $propertyModel->isMultivalue()) ? [] : null; |
171
|
4 |
|
$created = true; |
172
|
|
|
} |
173
|
|
|
|
174
|
4 |
|
$data =& $data[$property]; |
175
|
|
|
} |
176
|
|
|
|
177
|
4 |
|
return $data; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* Set a property value |
182
|
|
|
* |
183
|
|
|
* @param mixed $property Property |
184
|
|
|
* @param mixed $value Value |
185
|
|
|
* @param array $propertyPath Property path |
186
|
|
|
* @param PropertyModel $propertyModel Property model |
187
|
|
|
*/ |
188
|
4 |
|
protected function setPropertyValue( |
189
|
|
|
&$property, |
190
|
|
|
$value, |
191
|
|
|
array $propertyPath = null, |
192
|
|
|
PropertyModel $propertyModel = null |
193
|
|
|
) { |
194
|
|
|
// If the property path is not empty |
195
|
4 |
|
if (is_array($propertyPath) && count($propertyPath)) { |
196
|
|
|
// Filter the value if a property model is given |
197
|
4 |
|
if ($propertyModel) { |
198
|
3 |
|
$value = $propertyModel->filterValue($value); |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
// Determine the setter name |
202
|
4 |
|
$propertySetter = 'setPm'.implode(array_map('ucfirst', $propertyPath)); |
203
|
|
|
|
204
|
|
|
// If there's an explicit setter for this property: Use it |
205
|
4 |
|
if (is_callable([$this, $propertySetter])) { |
206
|
2 |
|
$this->$propertySetter($property, $value); |
207
|
2 |
|
return; |
208
|
|
|
} |
209
|
|
|
|
210
|
3 |
|
$property = $value; |
211
|
|
|
} |
212
|
3 |
|
} |
213
|
|
|
} |
214
|
|
|
|