1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* apparat-object |
5
|
|
|
* |
6
|
|
|
* @category Apparat |
7
|
|
|
* @package Apparat\Object |
8
|
|
|
* @subpackage Apparat\Object\<Layer> |
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\Factory; |
38
|
|
|
|
39
|
|
|
use Apparat\Kernel\Ports\Kernel; |
40
|
|
|
use Apparat\Object\Domain\Model\Path\ApparatUrl; |
41
|
|
|
use Apparat\Object\Domain\Model\Path\Url; |
42
|
|
|
use Apparat\Object\Domain\Model\Relation\ContributedByRelation; |
43
|
|
|
use Apparat\Object\Domain\Model\Relation\ContributesRelation; |
44
|
|
|
use Apparat\Object\Domain\Model\Relation\EmbeddedByRelation; |
45
|
|
|
use Apparat\Object\Domain\Model\Relation\EmbedsRelation; |
46
|
|
|
use Apparat\Object\Domain\Model\Relation\InvalidArgumentException; |
47
|
|
|
use Apparat\Object\Domain\Model\Relation\LikedByRelation; |
48
|
|
|
use Apparat\Object\Domain\Model\Relation\LikesRelation; |
49
|
|
|
use Apparat\Object\Domain\Model\Relation\OutOfBoundsException; |
50
|
|
|
use Apparat\Object\Domain\Model\Relation\ReferredByRelation; |
51
|
|
|
use Apparat\Object\Domain\Model\Relation\RefersToRelation; |
52
|
|
|
use Apparat\Object\Domain\Model\Relation\RelationInterface; |
53
|
|
|
use Apparat\Object\Domain\Model\Relation\RepliedByRelation; |
54
|
|
|
use Apparat\Object\Domain\Model\Relation\RepliesToRelation; |
55
|
|
|
use Apparat\Object\Domain\Model\Relation\RepostedByRelation; |
56
|
|
|
use Apparat\Object\Domain\Model\Relation\RepostsRelation; |
57
|
|
|
use Apparat\Object\Domain\Repository\RepositoryInterface; |
58
|
|
|
use Apparat\Object\Infrastructure\Utilities\Validator; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Relation factory |
62
|
|
|
* |
63
|
|
|
* @package Apparat\Object |
64
|
|
|
* @subpackage Apparat\Object\Domain |
65
|
|
|
*/ |
66
|
|
|
class RelationFactory |
67
|
|
|
{ |
68
|
|
|
/** |
69
|
|
|
* URL component key |
70
|
|
|
* |
71
|
|
|
* @string |
72
|
|
|
*/ |
73
|
|
|
const PARSE_URL = 'url'; |
74
|
|
|
/** |
75
|
|
|
* Label component key |
76
|
|
|
* |
77
|
|
|
* @string |
78
|
|
|
*/ |
79
|
|
|
const PARSE_LABEL = 'label'; |
80
|
|
|
/** |
81
|
|
|
* Email component key |
82
|
|
|
* |
83
|
|
|
* @string |
84
|
|
|
*/ |
85
|
|
|
const PARSE_EMAIL = 'email'; |
86
|
|
|
/** |
87
|
|
|
* Component relation coupling |
88
|
|
|
* |
89
|
|
|
* @string |
90
|
|
|
*/ |
91
|
|
|
const PARSE_COUPLING = 'coupling'; |
92
|
|
|
/** |
93
|
|
|
* Relation types |
94
|
|
|
* |
95
|
|
|
* @var array |
96
|
|
|
*/ |
97
|
|
|
public static $relationTypes = [ |
98
|
|
|
ContributesRelation::TYPE => ContributesRelation::class, |
99
|
|
|
ContributedByRelation::TYPE => ContributedByRelation::class, |
100
|
|
|
EmbedsRelation::TYPE => EmbedsRelation::class, |
101
|
|
|
EmbeddedByRelation::TYPE => EmbeddedByRelation::class, |
102
|
|
|
LikesRelation::TYPE => LikesRelation::class, |
103
|
|
|
LikedByRelation::TYPE => LikedByRelation::class, |
104
|
|
|
RefersToRelation::TYPE => RefersToRelation::class, |
105
|
|
|
ReferredByRelation::TYPE => ReferredByRelation::class, |
106
|
|
|
RepliesToRelation::TYPE => RepliesToRelation::class, |
107
|
|
|
RepliedByRelation::TYPE => RepliedByRelation::class, |
108
|
|
|
RepostsRelation::TYPE => RepostsRelation::class, |
109
|
|
|
RepostedByRelation::TYPE => RepostedByRelation::class, |
110
|
|
|
]; |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Parse a relation serialization and instantiate the relation |
114
|
|
|
* |
115
|
|
|
* @param string $relationType Relation type |
116
|
|
|
* @param string $relation Relation serialization |
117
|
|
|
* @param RepositoryInterface $contextRepository Context repository |
118
|
|
|
* @return RelationInterface Relation object |
119
|
|
|
*/ |
120
|
18 |
|
public static function createFromString($relationType, $relation, RepositoryInterface $contextRepository) |
121
|
|
|
{ |
122
|
|
|
// Validate the relation type |
123
|
18 |
|
self::validateRelationType($relationType); |
124
|
|
|
|
125
|
|
|
// Create the relation instance |
126
|
18 |
|
return Kernel::create( |
127
|
18 |
|
self::$relationTypes[$relationType], |
128
|
18 |
|
array_values(self::parseRelationString($relation, $contextRepository)) |
129
|
|
|
); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Validate a relation type |
134
|
|
|
* |
135
|
|
|
* @param string $relationType Relation type |
136
|
|
|
* @throws InvalidArgumentException If the relation type is invalid |
137
|
|
|
*/ |
138
|
18 |
|
public static function validateRelationType($relationType) { |
139
|
|
|
// If the relation type is invalid |
140
|
18 |
|
if (empty($relationType) || empty(self::$relationTypes[$relationType])) { |
141
|
|
|
throw new OutOfBoundsException( |
142
|
|
|
sprintf('Invalid object relation type "%s"', $relationType), |
143
|
|
|
OutOfBoundsException::INVALID_OBJECT_RELATION_TYPE |
144
|
|
|
); |
145
|
|
|
} |
146
|
18 |
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Parse a relation serialization and instantiate the relation object |
150
|
|
|
* |
151
|
|
|
* @param string $relation Relation serialization |
152
|
|
|
* @param RepositoryInterface $contextRepository Context repository |
153
|
|
|
* @return array Parsed relation components |
154
|
|
|
* @throws InvalidArgumentException If the email component has already been registered |
155
|
|
|
* @throws InvalidArgumentException If the URL component has already been registered |
156
|
|
|
*/ |
157
|
18 |
|
protected static function parseRelationString($relation, RepositoryInterface $contextRepository) |
158
|
|
|
{ |
159
|
|
|
// TODO: Document exceptions |
160
|
|
|
$parsed = [ |
161
|
18 |
|
self::PARSE_URL => null, |
162
|
18 |
|
self::PARSE_LABEL => null, |
163
|
18 |
|
self::PARSE_EMAIL => null, |
164
|
18 |
|
self::PARSE_COUPLING => RelationInterface::LOOSE_COUPLING, |
165
|
|
|
]; |
166
|
|
|
|
167
|
|
|
// Split the relation string and parse the components |
168
|
18 |
|
foreach (preg_split('%\s+%', $relation) as $relationComponent) { |
|
|
|
|
169
|
|
|
|
170
|
|
|
// If it's an email component |
171
|
18 |
|
if (!strncmp('<', $relationComponent, 1)) { |
172
|
|
|
// If the email component has already been registered |
173
|
15 |
|
if (!empty($parsed[self::PARSE_EMAIL])) { |
174
|
|
|
throw new InvalidArgumentException( |
175
|
|
|
sprintf('Repeated relation email component "%s" not allowed', self::PARSE_EMAIL), |
176
|
|
|
InvalidArgumentException::REPEATED_RELATION_COMPONENT_NOT_ALLOWED |
177
|
|
|
); |
178
|
|
|
} |
179
|
|
|
|
180
|
15 |
|
$parsed[self::PARSE_EMAIL] = self::parseRelationEmail($relationComponent); |
181
|
15 |
|
continue; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
// Next: Try to parse it as URL |
185
|
|
|
try { |
186
|
18 |
|
$url = self::parseRelationUrl($relationComponent, $parsed[self::PARSE_COUPLING], $contextRepository); |
|
|
|
|
187
|
|
|
|
188
|
|
|
// If the URL component has already been registered |
189
|
15 |
|
if (!empty($parsed[self::PARSE_URL])) { |
190
|
|
|
throw new InvalidArgumentException( |
191
|
|
|
sprintf('Repeated relation url component "%s" not allowed', self::PARSE_URL), |
192
|
|
|
InvalidArgumentException::REPEATED_RELATION_COMPONENT_NOT_ALLOWED |
193
|
|
|
); |
194
|
|
|
} |
195
|
|
|
|
196
|
15 |
|
$parsed[self::PARSE_URL] = $url; |
197
|
|
|
|
198
|
|
|
// Else: Process as label component |
199
|
18 |
|
} catch (\Exception $e) { |
200
|
18 |
|
$parsed[self::PARSE_LABEL] = trim($parsed[self::PARSE_LABEL].' '.$relationComponent); |
201
|
|
|
} |
202
|
|
|
} |
203
|
|
|
|
204
|
18 |
|
return $parsed; |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* Parse and validate a relation email address component |
209
|
|
|
* |
210
|
|
|
* @param string $relationEmail Email address |
211
|
|
|
* @return string Valid email address |
212
|
|
|
* @throws InvalidArgumentException If the email address is invalid |
213
|
|
|
*/ |
214
|
15 |
|
protected static function parseRelationEmail($relationEmail) |
215
|
|
|
{ |
216
|
|
|
// If it's a valid email address |
217
|
15 |
|
if (preg_match('%^\<(.+)\>$%', $relationEmail, $emailAddress) && Validator::isEmail($emailAddress[1])) { |
218
|
15 |
|
return $emailAddress[1]; |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
throw new InvalidArgumentException( |
222
|
|
|
sprintf('Invalid relation email address "%s"', $relationEmail), |
223
|
|
|
InvalidArgumentException::INVALID_RELATION_EMAIL_ADDRESS |
224
|
|
|
); |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* Parse and instantiate a relation URL |
229
|
|
|
* |
230
|
|
|
* @param string $url URL |
231
|
|
|
* @param boolean $coupling Strong coupling |
232
|
|
|
* @param RepositoryInterface $contextRepository Context repository |
233
|
|
|
* @return Url URL |
234
|
|
|
* @throws InvalidArgumentException If the relation URL is invalid |
235
|
|
|
*/ |
236
|
18 |
|
protected static function parseRelationUrl($url, &$coupling, RepositoryInterface $contextRepository) |
237
|
|
|
{ |
238
|
18 |
|
if (strlen($url)) { |
239
|
|
|
// If the URL requires tight coupling |
240
|
18 |
|
if (!strncmp('!', $url, 1)) { |
241
|
15 |
|
$coupling = RelationInterface::TIGHT_COUPLING; |
242
|
15 |
|
$url = substr($url, 1); |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
// Try to instantiate as an apparat URL |
246
|
|
|
try { |
247
|
18 |
|
return Kernel::create(ApparatUrl::class, [$url, true, $contextRepository]); |
248
|
|
|
|
249
|
|
|
// If there's an apparat URL problem: Try to instantiate as a regular URL |
250
|
18 |
|
} catch (\Apparat\Object\Domain\Model\Path\InvalidArgumentException $e) { |
251
|
|
|
/** @var Url $urlInstance */ |
252
|
18 |
|
$urlInstance = Kernel::create(Url::class, [$url]); |
253
|
18 |
|
if ($urlInstance->isAbsolute()) { |
254
|
15 |
|
return $urlInstance; |
255
|
|
|
} |
256
|
|
|
} |
257
|
|
|
} |
258
|
|
|
|
259
|
18 |
|
throw new InvalidArgumentException( |
260
|
18 |
|
sprintf('Invalid relation URL "%s"', $url), |
261
|
18 |
|
InvalidArgumentException::INVALID_RELATION_URL |
262
|
|
|
); |
263
|
|
|
} |
264
|
|
|
} |
265
|
|
|
|