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