|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* apparat-resource |
|
5
|
|
|
* |
|
6
|
|
|
* @category Apparat |
|
7
|
|
|
* @package Apparat\Resource |
|
8
|
|
|
* @subpackage Apparat\Resource\Domain |
|
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\Resource\Domain\Model\Hydrator; |
|
38
|
|
|
|
|
39
|
|
|
use Apparat\Kernel\Ports\Kernel; |
|
40
|
|
|
use Apparat\Resource\Domain\Factory\HydratorFactory; |
|
41
|
|
|
use Apparat\Resource\Domain\Model\Part\AbstractPart; |
|
42
|
|
|
use Apparat\Resource\Domain\Model\Part\AbstractPartAggregate; |
|
43
|
|
|
use Apparat\Resource\Domain\Model\Part\PartAggregateInterface; |
|
44
|
|
|
use Apparat\Resource\Domain\Model\Part\PartInterface; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Multipart hydrator |
|
48
|
|
|
* |
|
49
|
|
|
* @package Apparat\Resource |
|
50
|
|
|
* @subpackage Apparat\Resource\Domain |
|
51
|
|
|
*/ |
|
52
|
|
|
abstract class AbstractMultipartHydrator extends AbstractHydrator |
|
53
|
|
|
{ |
|
54
|
|
|
/** |
|
55
|
|
|
* Subhydrators |
|
56
|
|
|
* |
|
57
|
|
|
* @var array |
|
58
|
|
|
*/ |
|
59
|
|
|
protected $subhydrators = array(); |
|
60
|
|
|
/** |
|
61
|
|
|
* Minimum occurrences |
|
62
|
|
|
* |
|
63
|
|
|
* @var int |
|
64
|
|
|
*/ |
|
65
|
|
|
protected $minimumOccurrences = 1; |
|
66
|
|
|
/** |
|
67
|
|
|
* Maximum occurrences |
|
68
|
|
|
* |
|
69
|
|
|
* @var int |
|
70
|
|
|
*/ |
|
71
|
|
|
protected $maximumOccurrences = 1; |
|
72
|
|
|
/** |
|
73
|
|
|
* Part aggregate class name |
|
74
|
|
|
* |
|
75
|
|
|
* @var string |
|
76
|
|
|
*/ |
|
77
|
|
|
protected $aggregateClass = null; |
|
78
|
|
|
/** |
|
79
|
|
|
* Empty occurrence dehydration behaviour |
|
80
|
|
|
* |
|
81
|
|
|
* @var string |
|
82
|
|
|
*/ |
|
83
|
|
|
protected $occDhdrException = SkippedOccurrenceDehydrationException::class; |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Multipart hydrator constructor |
|
87
|
|
|
* |
|
88
|
|
|
* @param array $subhydrators Subpart hydrators |
|
89
|
|
|
* @param int $minOccurrences Minimum occurrences |
|
90
|
|
|
* @param int $maxOccurrences Maximum occurrences |
|
91
|
|
|
*/ |
|
92
|
37 |
|
public function __construct(array $subhydrators, $minOccurrences = 1, $maxOccurrences = 1) |
|
93
|
|
|
{ |
|
94
|
37 |
|
parent::__construct(HydratorInterface::STANDARD); |
|
95
|
|
|
|
|
96
|
|
|
// Run through all subhydrators |
|
97
|
37 |
|
foreach ($subhydrators as $part => $subhydrator) { |
|
98
|
|
|
// Validate the hydrator name |
|
99
|
37 |
|
AbstractPart::validatePartIdentifier($part); |
|
100
|
|
|
|
|
101
|
|
|
// If the subhydrator needs to be instantiated from a string or array |
|
102
|
37 |
|
if (!($subhydrator instanceof HydratorInterface)) { |
|
103
|
37 |
|
$subhydrator = HydratorFactory::build( |
|
104
|
37 |
|
is_array($subhydrator) ? $subhydrator : [[$part => $subhydrator]] |
|
105
|
|
|
); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
37 |
|
$this->subhydrators[$part] = $subhydrator; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
// Validate the occurrence numbers |
|
112
|
37 |
|
self::validateParameters($minOccurrences, $maxOccurrences); |
|
113
|
37 |
|
$this->minimumOccurrences = intval($minOccurrences); |
|
114
|
37 |
|
$this->maximumOccurrences = intval($maxOccurrences); |
|
115
|
37 |
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* Validate the parameters accepted by this hydrator |
|
119
|
|
|
* |
|
120
|
|
|
* By default, a multipart parameter accepts exactly two parameters: |
|
121
|
|
|
* - the minimum number of occurrences of the contained part aggregate |
|
122
|
|
|
* - the maximum number of occurrences of the contained part aggregate |
|
123
|
|
|
* |
|
124
|
|
|
* @param array $parameters Parameters |
|
125
|
|
|
* @return boolean Parameters are valid |
|
126
|
|
|
*/ |
|
127
|
41 |
|
public static function validateParameters(...$parameters) |
|
128
|
|
|
{ |
|
129
|
|
|
|
|
130
|
|
|
// If the number of parameters isn't exactly 2 |
|
131
|
41 |
|
if (count($parameters) != 2) { |
|
132
|
1 |
|
throw new InvalidArgumentException( |
|
133
|
|
|
sprintf( |
|
134
|
1 |
|
'Invalid multipart hydrator parameter count (%s)', |
|
135
|
|
|
count($parameters) |
|
136
|
|
|
), |
|
137
|
1 |
|
InvalidArgumentException::INVALID_MULTIPART_HYDRATOR_PARAMETER_COUNT |
|
138
|
|
|
); |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
// Validate the occurrence numbers |
|
142
|
40 |
|
AbstractPartAggregate::validateOccurrences(intval($parameters[0]), intval($parameters[1])); |
|
143
|
|
|
|
|
144
|
38 |
|
return true; |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* Serialize a file part |
|
149
|
|
|
* |
|
150
|
|
|
* @param PartInterface $part File part |
|
151
|
|
|
* @return string Serialized file part |
|
152
|
|
|
* @throws InvalidArgumentException If the part class cannot be dehydrated by this hydrator |
|
153
|
|
|
*/ |
|
154
|
12 |
|
public function dehydrate(PartInterface $part) |
|
155
|
|
|
{ |
|
156
|
|
|
// Make sure it's a part aggregate that should be dehydrated |
|
157
|
12 |
|
if (!($part instanceof PartAggregateInterface)) { |
|
158
|
1 |
|
throw new InvalidArgumentException( |
|
159
|
1 |
|
sprintf('Invalid dehydration part class "%s"', get_class($part)), |
|
160
|
1 |
|
InvalidArgumentException::INVALID_DEHYDRATION_PART_CLASS |
|
161
|
|
|
); |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
11 |
|
$occurrences = []; |
|
165
|
|
|
|
|
166
|
|
|
// Run through all occurrences of the part |
|
167
|
11 |
|
foreach ($part as $occurrence) { |
|
168
|
11 |
|
$occurrence = $this->dehydrateOccurrence($occurrence); |
|
169
|
|
|
|
|
170
|
|
|
// If the occurrence is not a string |
|
171
|
5 |
|
if (!is_string($occurrence)) { |
|
172
|
1 |
|
throw new RuntimeException( |
|
173
|
1 |
|
'Dehydrating an aggregate occurrence must return a string', |
|
174
|
1 |
|
RuntimeException::OCCURRENCE_DEHYDRATION_MUST_RETURN_A_STRING |
|
175
|
|
|
); |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
4 |
|
$occurrences[] = $occurrence; |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
// Combine and return the dehydrated occurrences |
|
182
|
4 |
|
return $this->combineDehydratedOccurrences($occurrences); |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
/******************************************************************************* |
|
186
|
|
|
* STATIC METHODS |
|
187
|
|
|
*******************************************************************************/ |
|
188
|
|
|
|
|
189
|
|
|
/** |
|
190
|
|
|
* Dehydrate a single occurrence |
|
191
|
|
|
* |
|
192
|
|
|
* @param array $occurrence Occurrence |
|
193
|
|
|
* @return string Dehydrated occurrence |
|
194
|
|
|
*/ |
|
195
|
|
|
abstract protected function dehydrateOccurrence(array $occurrence); |
|
196
|
|
|
|
|
197
|
|
|
/******************************************************************************* |
|
198
|
|
|
* PRIVATE METHODS |
|
199
|
|
|
*******************************************************************************/ |
|
200
|
|
|
|
|
201
|
|
|
/** |
|
202
|
|
|
* Combine a list of dehydrated occurrences |
|
203
|
|
|
* |
|
204
|
|
|
* @param array $occurrences List of dehydrated occurrences |
|
205
|
|
|
* @return string Combined dehydrated occurrences |
|
206
|
|
|
*/ |
|
207
|
4 |
|
protected function combineDehydratedOccurrences(array $occurrences) |
|
208
|
|
|
{ |
|
209
|
4 |
|
return implode('', array_map('strval', $occurrences)); |
|
210
|
|
|
} |
|
211
|
|
|
|
|
212
|
|
|
/** |
|
213
|
|
|
* Initialize the aggregate part |
|
214
|
|
|
* |
|
215
|
|
|
* @param string $data Part data |
|
216
|
|
|
* @return AbstractPartAggregate Part aggregate |
|
217
|
|
|
*/ |
|
218
|
33 |
|
public function hydrate($data) |
|
219
|
|
|
{ |
|
220
|
|
|
// If the part aggregate class isn't valid |
|
221
|
33 |
|
if (!$this->aggregateClass || |
|
222
|
33 |
|
!class_exists($this->aggregateClass) || |
|
223
|
33 |
|
!(new \ReflectionClass($this->aggregateClass))->implementsInterface(PartAggregateInterface::class) |
|
224
|
|
|
) { |
|
225
|
1 |
|
throw new RuntimeException( |
|
226
|
1 |
|
sprintf('Invalid part aggregate class "%s"', $this->aggregateClass), |
|
227
|
1 |
|
RuntimeException::INVALID_PART_AGGREGATE_CLASS |
|
228
|
|
|
); |
|
229
|
|
|
} |
|
230
|
|
|
|
|
231
|
32 |
|
unset($data); |
|
232
|
|
|
|
|
233
|
32 |
|
return Kernel::create( |
|
234
|
32 |
|
$this->aggregateClass, |
|
235
|
|
|
[ |
|
236
|
32 |
|
$this, |
|
237
|
32 |
|
$this->subhydrators, |
|
238
|
32 |
|
$this->minimumOccurrences, |
|
239
|
32 |
|
$this->maximumOccurrences |
|
240
|
|
|
] |
|
241
|
|
|
); |
|
242
|
|
|
} |
|
243
|
|
|
|
|
244
|
|
|
/** |
|
245
|
|
|
* Dehydrate a single part with a particular subhydrator |
|
246
|
|
|
* |
|
247
|
|
|
* @param string $subhydrator Subhydrator name |
|
248
|
|
|
* @param PartInterface $part Part instance |
|
249
|
|
|
* @return string Dehydrated part |
|
250
|
|
|
*/ |
|
251
|
4 |
|
protected function dehydratePart($subhydrator, PartInterface $part) |
|
252
|
|
|
{ |
|
253
|
|
|
/** @var HydratorInterface $subhydratorInstance */ |
|
254
|
4 |
|
$subhydratorInstance = $this->subhydrators[$subhydrator]; |
|
255
|
4 |
|
return $subhydratorInstance->dehydrate($part); |
|
256
|
|
|
} |
|
257
|
|
|
} |
|
258
|
|
|
|