|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* apparat/object |
|
5
|
|
|
* |
|
6
|
|
|
* @category Apparat |
|
7
|
|
|
* @package Apparat\Server |
|
8
|
|
|
* @subpackage Apparat\Object\Domain\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
|
|
|
use Apparat\Object\Application\Model\Properties\Datatype\DatatypeInterface; |
|
41
|
|
|
use Apparat\Object\Domain\Model\Object\ObjectInterface; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Property model |
|
45
|
|
|
* |
|
46
|
|
|
* @package Apparat\Object |
|
47
|
|
|
* @subpackage Apparat\Object\Domain |
|
48
|
|
|
*/ |
|
49
|
|
|
class PropertyModel |
|
50
|
|
|
{ |
|
51
|
|
|
/** |
|
52
|
|
|
* Multivalue property |
|
53
|
|
|
* |
|
54
|
|
|
* @var boolean |
|
55
|
|
|
*/ |
|
56
|
|
|
protected $multivalue; |
|
57
|
|
|
/** |
|
58
|
|
|
* Allowed datatypes |
|
59
|
|
|
* |
|
60
|
|
|
* @var array |
|
61
|
|
|
*/ |
|
62
|
|
|
protected $datatypes; |
|
63
|
|
|
/** |
|
64
|
|
|
* Datatype filter |
|
65
|
|
|
* |
|
66
|
|
|
* @var array |
|
67
|
|
|
*/ |
|
68
|
|
|
protected $filter; |
|
69
|
|
|
/** |
|
70
|
|
|
* Owning object |
|
71
|
|
|
* |
|
72
|
|
|
* @var ObjectInterface |
|
73
|
|
|
*/ |
|
74
|
|
|
private $object; |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Constructor |
|
78
|
|
|
* |
|
79
|
|
|
* @param ObjectInterface $object Owning object |
|
80
|
|
|
* @param bool $multivalue Multivalue property |
|
81
|
|
|
* @param array $datatypes Allowed datatypes |
|
82
|
|
|
* @param array $filter Datatype filters |
|
83
|
|
|
* @throws InvalidArgumentException If no datatypes are allowed |
|
84
|
|
|
* @throws InvalidArgumentException If the datatype is invalid |
|
85
|
|
|
*/ |
|
86
|
5 |
|
public function __construct(ObjectInterface $object, $multivalue, array $datatypes, array $filter = []) |
|
87
|
|
|
{ |
|
88
|
5 |
|
$this->object = $object; |
|
89
|
5 |
|
$this->multivalue = boolval($multivalue); |
|
90
|
5 |
|
$this->datatypes = $datatypes; |
|
91
|
5 |
|
$this->filter = $filter; |
|
92
|
|
|
|
|
93
|
|
|
// If no datatypes are allowed |
|
94
|
5 |
|
if (!count($this->datatypes)) { |
|
95
|
1 |
|
throw new InvalidArgumentException( |
|
96
|
1 |
|
'Invalid property datatypes list', |
|
97
|
1 |
|
InvalidArgumentException::INVALID_PROPERTY_DATATYPES |
|
98
|
|
|
); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
// Validate & instantiate the datatypes |
|
102
|
4 |
|
foreach ($this->datatypes as $datatypeIndex => $datatype) { |
|
103
|
|
|
// If the datatype is invalid |
|
104
|
4 |
|
if (!$datatype |
|
105
|
4 |
|
|| !class_exists($datatype) |
|
106
|
4 |
|
|| !(new \ReflectionClass($datatype))->implementsInterface(DatatypeInterface::class) |
|
107
|
|
|
) { |
|
108
|
1 |
|
throw new InvalidArgumentException( |
|
109
|
1 |
|
sprintf('Invalid property datatype "%s"', $datatype), |
|
110
|
1 |
|
InvalidArgumentException::INVALID_PROPERTY_DATATYPE |
|
111
|
|
|
); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
3 |
|
$this->datatypes[$datatypeIndex] = Kernel::create( |
|
115
|
|
|
$datatype, |
|
116
|
3 |
|
[$this->object, empty($this->filter[$datatype]) ? [] : (array)$this->filter[$datatype]] |
|
117
|
|
|
); |
|
118
|
|
|
} |
|
119
|
3 |
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* Filter a property value |
|
123
|
|
|
* |
|
124
|
|
|
* @param mixed $value Property value |
|
125
|
|
|
* @return mixed Filtered value |
|
126
|
|
|
* @throws DomainException If the domain property value is invalid |
|
127
|
|
|
*/ |
|
128
|
3 |
|
public function filterValue($value) |
|
129
|
|
|
{ |
|
130
|
|
|
// If the property model expects an array as value |
|
131
|
3 |
|
if ($this->multivalue) { |
|
132
|
1 |
|
$values = (array)$value; |
|
133
|
1 |
|
foreach ($values as $index => $value) { |
|
134
|
1 |
|
$values[$index] = $this->filterSingleValue($value); |
|
135
|
|
|
} |
|
136
|
1 |
|
return $values; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
3 |
|
return $this->filterSingleValue($value); |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* Filter a single property value |
|
144
|
|
|
* |
|
145
|
|
|
* @param mixed $value Property value |
|
146
|
|
|
* @return mixed Filtered value |
|
147
|
|
|
* @throws DomainException If the domain property value is invalid |
|
148
|
|
|
*/ |
|
149
|
3 |
|
protected function filterSingleValue($value) |
|
150
|
|
|
{ |
|
151
|
|
|
// If the value is empty |
|
152
|
3 |
|
if (empty($value)) { |
|
153
|
1 |
|
return null; |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
// Run through all allowed datatypes |
|
157
|
|
|
/** @var DatatypeInterface $datatype */ |
|
158
|
3 |
|
foreach ($this->datatypes as $datatype) { |
|
159
|
|
|
try { |
|
160
|
3 |
|
return $datatype->match($value); |
|
161
|
2 |
|
} catch (DomainException $e) { |
|
162
|
1 |
|
continue; |
|
163
|
1 |
|
} catch (\Exception $e) { |
|
164
|
1 |
|
break; |
|
165
|
|
|
} |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
// If the domain property value is invalid |
|
169
|
1 |
|
throw new DomainException( |
|
170
|
1 |
|
sprintf('Invalid domain property value "%s"', $value), |
|
171
|
1 |
|
DomainException::INVALID_DOMAIN_PROPERTY_VALUE |
|
172
|
|
|
); |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
/** |
|
176
|
|
|
* Return whether this property model is multivalued |
|
177
|
|
|
* |
|
178
|
|
|
* @return boolean Multivalued |
|
179
|
|
|
*/ |
|
180
|
2 |
|
public function isMultivalue() |
|
181
|
|
|
{ |
|
182
|
2 |
|
return $this->multivalue; |
|
183
|
|
|
} |
|
184
|
|
|
} |