1 | <?php |
||
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 | 15 | public static function createFromArguments($args) |
|
85 | { |
||
86 | 15 | $profiledNames = []; |
|
87 | 15 | $profile = false; |
|
88 | |||
89 | // Consume and register all given names and profiles |
||
90 | 15 | while (count($args)) { |
|
91 | 15 | $profiledNames[] = self::consumeProfiledName($args, $profile); |
|
92 | 15 | } |
|
93 | |||
94 | 15 | 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 | 15 | protected static function consumeProfiledName(&$args, &$profile) |
|
105 | { |
||
106 | 15 | $profiledName = new \stdClass(); |
|
107 | 15 | $profiledName->profile = $profile; |
|
108 | |||
109 | // Get the first argument |
||
110 | 15 | $arg = array_shift($args); |
|
111 | |||
112 | // If it's an object argument |
||
113 | 15 | if (is_object($arg)) { |
|
114 | 6 | return self::createProfiledNameFromObject($arg, $profile); |
|
115 | } |
||
116 | |||
117 | // If it's an array argument |
||
118 | 13 | if (is_array($arg)) { |
|
119 | 2 | return self::createProfiledNameFromArray($arg, $profile); |
|
|
|||
120 | } |
||
121 | |||
122 | 11 | if (($profile === false) && is_string(current($args))) { |
|
123 | 5 | $profile = array_shift($args); |
|
124 | 5 | } |
|
125 | 11 | return self::createProfiledNameFromString(strval($arg), $profile); |
|
126 | } |
||
127 | |||
128 | /** |
||
129 | * Create a profiled name from an object argument |
||
130 | * |
||
131 | * @param \stdClass $arg Object argument |
||
132 | * @param string $profile Profile |
||
133 | * @return \stdClass Profiled name |
||
134 | * @throws InvalidArgumentException If the name is missing |
||
135 | */ |
||
136 | 7 | protected static function createProfiledNameFromObject($arg, &$profile) |
|
137 | { |
||
138 | // If the name is invalid |
||
139 | 7 | if (!isset($arg->name)) { |
|
140 | throw new InvalidArgumentException( |
||
141 | InvalidArgumentException::INVALID_TYPE_PROPERTY_NAME_STR, |
||
142 | InvalidArgumentException::INVALID_TYPE_PROPERTY_NAME |
||
143 | ); |
||
144 | } |
||
145 | |||
146 | 7 | if (isset($arg->profile)) { |
|
147 | 7 | $profile = trim($arg->profile) ?: null; |
|
148 | 7 | } |
|
149 | |||
150 | 7 | return self::createProfiledNameFromString($arg->name, $profile); |
|
151 | } |
||
152 | |||
153 | /** |
||
154 | * Create a profiled name from an array argument |
||
155 | * |
||
156 | * @param array $arg Array argument |
||
157 | * @param string $profile Profile |
||
158 | * @return \stdClass Profiled name |
||
159 | * @throws InvalidArgumentException If the array definition is invalid |
||
160 | */ |
||
161 | 2 | protected static function createProfiledNameFromArray(array $arg, &$profile) |
|
180 | |||
181 | /** |
||
182 | * Create a profiled name from string arguments |
||
183 | * |
||
184 | * @param string $name Name |
||
185 | * @param string|null $profile Profile |
||
186 | * @return \stdClass Profiled name |
||
187 | * @throws InvalidArgumentException If the name is invalid |
||
188 | */ |
||
189 | 15 | protected static function createProfiledNameFromString($name, $profile) |
|
204 | } |
||
205 |
This check looks at variables that have been passed in as parameters and are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.