1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* micrometa |
5
|
|
|
* |
6
|
|
|
* @category Jkphl |
7
|
|
|
* @package Jkphl\Micrometa |
8
|
|
|
* @subpackage Jkphl\Micrometa\Infrastructure |
9
|
|
|
* @author Joschi Kuphal <[email protected]> / @jkphl |
10
|
|
|
* @copyright Copyright © 2018 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 © 2018 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 Jkphl\Micrometa\Infrastructure\Factory; |
38
|
|
|
|
39
|
|
|
use Jkphl\Micrometa\Infrastructure\Parser\ProfiledNamesList; |
40
|
|
|
use Jkphl\Micrometa\Ports\Exceptions\InvalidArgumentException; |
41
|
|
|
use Jkphl\Micrometa\Ports\Item\Item; |
42
|
|
|
use Jkphl\Micrometa\Ports\Item\ItemList; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Profiled name factory |
46
|
|
|
* |
47
|
|
|
* @package Jkphl\Micrometa |
48
|
|
|
* @subpackage Jkphl\Micrometa\Infrastructure |
49
|
|
|
*/ |
50
|
|
|
class ProfiledNamesFactory |
51
|
|
|
{ |
52
|
|
|
/** |
53
|
|
|
* Create a list of profiled names from method arguments |
54
|
|
|
* |
55
|
|
|
* The method takes an arbitrary number of arguments and tries to parse them as profiled names. Arguments |
56
|
|
|
* may be strings, arrays or objects. |
57
|
|
|
* |
58
|
|
|
* String values are interpreted as names — with one exception: If the first two arguments are both strings, |
59
|
|
|
* the second one is taken as profile IRI. Optionally following string arguments are taken as names again, |
60
|
|
|
* assuming to share the same profile: |
61
|
|
|
* |
62
|
|
|
* createFromArguments($name1 [, $profile]) |
63
|
|
|
* createFromArguments($name1, $profile1, $name2, $profile2 ...) |
64
|
|
|
* |
65
|
|
|
* Arrays arguments are expected to have at least one argument which is taken as name. If present, the |
66
|
|
|
* second argument is used as profile (otherwise an empty profile is assumed): |
67
|
|
|
* |
68
|
|
|
* createFromArguments(array($name [, $profile])) |
69
|
|
|
* |
70
|
|
|
* Object values are expected to have a "name" and an optional "profile" property: |
71
|
|
|
* |
72
|
|
|
* createFromArguments((object)array('name' => $name [, 'profile' => $profile])) |
73
|
|
|
* |
74
|
|
|
* When an array or object argument is consumed, the profile value will be used for any following string |
75
|
|
|
* argument. You can "reset" the profile to another value by specifying another array or object value in |
76
|
|
|
* this case. |
77
|
|
|
* |
78
|
|
|
* createFromArguments(array($name1, $profile1), $name2, $name3 ...) |
79
|
|
|
* |
80
|
|
|
* @param array $args Arguments |
81
|
|
|
* |
82
|
|
|
* @return ProfiledNamesList Profiled names |
83
|
|
|
* @see Item::isOfType() |
84
|
|
|
* @see Item::getFirstProperty() |
85
|
|
|
* @see ItemList::getFirstItem() |
86
|
|
|
* @see ItemList::getItems() |
87
|
|
|
*/ |
88
|
17 |
|
public static function createFromArguments(array $args) |
89
|
|
|
{ |
90
|
17 |
|
$profiledNames = []; |
91
|
17 |
|
$profile = false; |
92
|
|
|
|
93
|
|
|
// Consume and register all given names and profiles |
94
|
17 |
|
while (count($args)) { |
95
|
17 |
|
$profiledNames[] = self::consumeProfiledName($args, $profile); |
96
|
|
|
} |
97
|
|
|
|
98
|
17 |
|
return new ProfiledNamesList($profiledNames); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Create a single profiled name by argument consumption |
103
|
|
|
* |
104
|
|
|
* @param array $args Arguments |
105
|
|
|
* @param string|boolean|null $profile Profile |
106
|
|
|
* |
107
|
|
|
* @return \stdClass Profiled name |
108
|
|
|
*/ |
109
|
17 |
|
protected static function consumeProfiledName(&$args, &$profile) |
110
|
|
|
{ |
111
|
17 |
|
$profiledName = new \stdClass(); |
112
|
17 |
|
$profiledName->profile = $profile; |
113
|
|
|
|
114
|
|
|
// Get the first argument |
115
|
17 |
|
$arg = array_shift($args); |
116
|
|
|
|
117
|
|
|
// If it's not a scalar argument |
118
|
17 |
|
if (!is_scalar($arg)) { |
119
|
8 |
|
return self::consumeNonScalarProfiledName($arg, $profile); |
120
|
|
|
} |
121
|
|
|
|
122
|
13 |
|
if (($profile === false) && is_string(current($args))) { |
123
|
5 |
|
$profile = array_shift($args); |
124
|
|
|
} |
125
|
|
|
|
126
|
13 |
|
return self::createProfiledNameFromString(strval($arg), $profile); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Create a profiled name by consuming a non-scalar argument |
131
|
|
|
* |
132
|
|
|
* @param \stdClass|array $arg Argument |
133
|
|
|
* @param string|boolean|null $profile Profile |
134
|
|
|
* |
135
|
|
|
* @return \stdClass Profiled name |
136
|
|
|
*/ |
137
|
8 |
|
protected static function consumeNonScalarProfiledName($arg, &$profile) |
138
|
|
|
{ |
139
|
|
|
// If it's an object argument |
140
|
8 |
|
if (is_object($arg)) { |
141
|
6 |
|
return self::createProfiledNameFromObject($arg, $profile); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
// Else: It must be an array |
145
|
2 |
|
return self::createProfiledNameFromArray($arg, $profile); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Create a profiled name from an object argument |
150
|
|
|
* |
151
|
|
|
* @param \stdClass $arg Object argument |
152
|
|
|
* @param string|boolean|null $profile Profile |
153
|
|
|
* |
154
|
|
|
* @return \stdClass Profiled name |
155
|
|
|
* @throws InvalidArgumentException If the name is missing |
156
|
|
|
*/ |
157
|
7 |
|
protected static function createProfiledNameFromObject($arg, &$profile) |
158
|
|
|
{ |
159
|
|
|
// If the name is invalid |
160
|
7 |
|
if (!isset($arg->name)) { |
161
|
|
|
throw new InvalidArgumentException( |
162
|
|
|
InvalidArgumentException::INVALID_TYPE_PROPERTY_NAME_STR, |
163
|
|
|
InvalidArgumentException::INVALID_TYPE_PROPERTY_NAME |
164
|
|
|
); |
165
|
|
|
} |
166
|
|
|
|
167
|
7 |
|
if (isset($arg->profile)) { |
168
|
7 |
|
$profile = trim($arg->profile) ?: null; |
169
|
|
|
} |
170
|
|
|
|
171
|
7 |
|
return self::createProfiledNameFromString($arg->name, $profile); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* Create a profiled name from string arguments |
176
|
|
|
* |
177
|
|
|
* @param string $name Name |
178
|
|
|
* @param string|boolean|null $profile Profile |
179
|
|
|
* |
180
|
|
|
* @return \stdClass Profiled name |
181
|
|
|
* @throws InvalidArgumentException If the name is invalid |
182
|
|
|
*/ |
183
|
17 |
|
protected static function createProfiledNameFromString($name, $profile) |
184
|
|
|
{ |
185
|
|
|
// If the name is invalid |
186
|
17 |
|
if (!strlen(trim($name))) { |
187
|
|
|
throw new InvalidArgumentException( |
188
|
|
|
InvalidArgumentException::INVALID_TYPE_PROPERTY_NAME_STR, |
189
|
|
|
InvalidArgumentException::INVALID_TYPE_PROPERTY_NAME |
190
|
|
|
); |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
return (object)[ |
194
|
17 |
|
'name' => trim($name), |
195
|
17 |
|
'profile' => trim($profile) ?: null, |
196
|
|
|
]; |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* Create a profiled name from an array argument |
201
|
|
|
* |
202
|
|
|
* @param array $arg Array argument |
203
|
|
|
* @param string|boolean|null $profile Profile |
204
|
|
|
* |
205
|
|
|
* @return \stdClass Profiled name |
206
|
|
|
* @throws InvalidArgumentException If the array definition is invalid |
207
|
|
|
*/ |
208
|
2 |
|
protected static function createProfiledNameFromArray(array $arg, &$profile) |
209
|
|
|
{ |
210
|
|
|
// If it's an associative array containing a "name" key |
211
|
2 |
|
if (array_key_exists('name', $arg)) { |
212
|
1 |
|
return self::createProfiledNameFromObject((object)$arg, $profile); |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
// If the argument has two items at least |
216
|
1 |
|
if (count($arg) > 1) { |
217
|
1 |
|
$name = array_shift($arg); |
218
|
1 |
|
$profile = trim(array_shift($arg)) ?: null; |
219
|
|
|
|
220
|
1 |
|
return self::createProfiledNameFromString($name, $profile); |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
throw new InvalidArgumentException( |
224
|
|
|
InvalidArgumentException::INVALID_TYPE_PROPERTY_ARRAY_STR, |
225
|
|
|
InvalidArgumentException::INVALID_TYPE_PROPERTY_ARRAY |
226
|
|
|
); |
227
|
|
|
} |
228
|
|
|
} |
229
|
|
|
|