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 © 2017 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 © 2017 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
|
|
|
* @return ProfiledNamesList Profiled names |
82
|
|
|
* @see Item::isOfType() |
83
|
|
|
* @see Item::getFirstProperty() |
84
|
|
|
* @see ItemList::getFirstItem() |
85
|
|
|
* @see ItemList::getItems() |
86
|
|
|
*/ |
87
|
20 |
|
public static function createFromArguments(array $args) |
88
|
|
|
{ |
89
|
20 |
|
$profiledNames = []; |
90
|
20 |
|
$profile = false; |
91
|
|
|
|
92
|
|
|
// Consume and register all given names and profiles |
93
|
20 |
|
while (count($args)) { |
94
|
20 |
|
$profiledNames[] = self::consumeProfiledName($args, $profile); |
95
|
|
|
} |
96
|
|
|
|
97
|
17 |
|
return new ProfiledNamesList($profiledNames); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Create a single profiled name by argument consumption |
102
|
|
|
* |
103
|
|
|
* @param array $args Arguments |
104
|
|
|
* @param string|boolean|null $profile Profile |
105
|
|
|
* @return \stdClass Profiled name |
106
|
|
|
*/ |
107
|
20 |
|
protected static function consumeProfiledName(&$args, &$profile) |
108
|
|
|
{ |
109
|
20 |
|
$profiledName = new \stdClass(); |
110
|
20 |
|
$profiledName->profile = $profile; |
111
|
|
|
|
112
|
|
|
// Get the first argument |
113
|
20 |
|
$arg = array_shift($args); |
114
|
|
|
|
115
|
|
|
// If it's not a scalar argument |
116
|
20 |
|
if (!is_scalar($arg)) { |
117
|
10 |
|
return self::consumeNonScalarProfiledName($arg, $profile); |
118
|
|
|
} |
119
|
|
|
|
120
|
14 |
|
if (($profile === false) && is_string(current($args))) { |
121
|
5 |
|
$profile = array_shift($args); |
122
|
|
|
} |
123
|
14 |
|
return self::createProfiledNameFromString(strval($arg), $profile); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Create a profiled name by consuming a non-scalar argument |
128
|
|
|
* |
129
|
|
|
* @param \stdClass|array $arg Argument |
130
|
|
|
* @param string|boolean|null $profile Profile |
131
|
|
|
* @return \stdClass Profiled name |
132
|
|
|
*/ |
133
|
10 |
|
protected static function consumeNonScalarProfiledName($arg, &$profile) |
134
|
|
|
{ |
135
|
|
|
// If it's an object argument |
136
|
10 |
|
if (is_object($arg)) { |
137
|
7 |
|
return self::createProfiledNameFromObject($arg, $profile); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
// Else: It must be an array |
141
|
3 |
|
return self::createProfiledNameFromArray($arg, $profile); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Create a profiled name from an object argument |
146
|
|
|
* |
147
|
|
|
* @param \stdClass $arg Object argument |
148
|
|
|
* @param string|boolean|null $profile Profile |
149
|
|
|
* @return \stdClass Profiled name |
150
|
|
|
* @throws InvalidArgumentException If the name is missing |
151
|
|
|
*/ |
152
|
8 |
|
protected static function createProfiledNameFromObject($arg, &$profile) |
153
|
|
|
{ |
154
|
|
|
// If the name is invalid |
155
|
8 |
|
if (!isset($arg->name)) { |
156
|
1 |
|
throw new InvalidArgumentException( |
157
|
1 |
|
InvalidArgumentException::INVALID_TYPE_PROPERTY_NAME_STR, |
158
|
1 |
|
InvalidArgumentException::INVALID_TYPE_PROPERTY_NAME |
159
|
|
|
); |
160
|
|
|
} |
161
|
|
|
|
162
|
7 |
|
if (isset($arg->profile)) { |
163
|
7 |
|
$profile = trim($arg->profile) ?: null; |
164
|
|
|
} |
165
|
|
|
|
166
|
7 |
|
return self::createProfiledNameFromString($arg->name, $profile); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* Create a profiled name from an array argument |
171
|
|
|
* |
172
|
|
|
* @param array $arg Array argument |
173
|
|
|
* @param string|boolean|null $profile Profile |
174
|
|
|
* @return \stdClass Profiled name |
175
|
|
|
* @throws InvalidArgumentException If the array definition is invalid |
176
|
|
|
*/ |
177
|
3 |
|
protected static function createProfiledNameFromArray(array $arg, &$profile) |
178
|
|
|
{ |
179
|
|
|
// If it's an associative array containing a "name" key |
180
|
3 |
|
if (array_key_exists('name', $arg)) { |
181
|
1 |
|
return self::createProfiledNameFromObject((object)$arg, $profile); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
// If the argument has two items at least |
185
|
2 |
|
if (count($arg) > 1) { |
186
|
1 |
|
$name = array_shift($arg); |
187
|
1 |
|
$profile = trim(array_shift($arg)) ?: null; |
188
|
1 |
|
return self::createProfiledNameFromString($name, $profile); |
189
|
|
|
} |
190
|
|
|
|
191
|
1 |
|
throw new InvalidArgumentException( |
192
|
1 |
|
InvalidArgumentException::INVALID_TYPE_PROPERTY_ARRAY_STR, |
193
|
1 |
|
InvalidArgumentException::INVALID_TYPE_PROPERTY_ARRAY |
194
|
|
|
); |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* Create a profiled name from string arguments |
199
|
|
|
* |
200
|
|
|
* @param string $name Name |
201
|
|
|
* @param string|boolean|null $profile Profile |
202
|
|
|
* @return \stdClass Profiled name |
203
|
|
|
* @throws InvalidArgumentException If the name is invalid |
204
|
|
|
*/ |
205
|
18 |
|
protected static function createProfiledNameFromString($name, $profile) |
206
|
|
|
{ |
207
|
|
|
// If the name is invalid |
208
|
18 |
|
if (!strlen(trim($name))) { |
209
|
1 |
|
throw new InvalidArgumentException( |
210
|
1 |
|
InvalidArgumentException::INVALID_TYPE_PROPERTY_NAME_STR, |
211
|
1 |
|
InvalidArgumentException::INVALID_TYPE_PROPERTY_NAME |
212
|
|
|
); |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
return (object)[ |
216
|
17 |
|
'name' => trim($name), |
217
|
17 |
|
'profile' => trim($profile) ?: null, |
218
|
|
|
]; |
219
|
|
|
} |
220
|
|
|
} |
221
|
|
|
|