1 | <?php |
||
34 | class Parameter |
||
35 | { |
||
36 | use HydratorStrategyTrait; |
||
37 | |||
38 | const DEFAULT_LOCATION = 'json'; |
||
39 | |||
40 | /** |
||
41 | * The human-friendly name of the parameter. This is what the user will input. |
||
42 | * |
||
43 | * @var string |
||
44 | */ |
||
45 | private $name; |
||
46 | |||
47 | /** |
||
48 | * The alias for this parameter. Although the user will always interact with the human-friendly $name property, |
||
49 | * the $sentAs is what's used over the wire. |
||
50 | * |
||
51 | * @var string |
||
52 | */ |
||
53 | private $sentAs; |
||
54 | |||
55 | /** |
||
56 | * For array parameters (for example, an array of security group names when creating a server), each array element |
||
57 | * will need to adhere to a common schema. For the aforementioned example, each element will need to be a string. |
||
58 | * For more complicated parameters, you might be validated an array of complicated objects. |
||
59 | * |
||
60 | * @var Parameter |
||
61 | */ |
||
62 | private $itemSchema; |
||
63 | |||
64 | /** |
||
65 | * For object parameters, each property will need to adhere to a specific schema. For every property in the |
||
66 | * object, it has its own schema - meaning that this property is a hash of name/schema pairs. |
||
67 | * |
||
68 | * The *only* exception to this rule is for metadata parameters, which are arbitrary key/value pairs. Since it does |
||
69 | * not make sense to have a schema for each metadata key, a common schema is use for every one. So instead of this |
||
70 | * property being a hash of schemas, it is a single Parameter object instead. This single Parameter schema will |
||
71 | * then be applied to each metadata key provided. |
||
72 | * |
||
73 | * @var []Parameter|Parameter |
||
74 | */ |
||
75 | private $properties; |
||
76 | |||
77 | /** |
||
78 | * The value's PHP type which this parameter represents; either "string", "bool", "object", "array", "NULL". |
||
79 | * |
||
80 | * @var string |
||
81 | */ |
||
82 | private $type; |
||
83 | |||
84 | /** |
||
85 | * Indicates whether this parameter requires a value from the user. |
||
86 | * |
||
87 | * @var bool |
||
88 | */ |
||
89 | private $required; |
||
90 | |||
91 | /** |
||
92 | * The location in the HTTP request where this parameter will populate; either "header", "url", "query", "raw" or |
||
93 | * "json". |
||
94 | * |
||
95 | * @var string |
||
96 | */ |
||
97 | private $location; |
||
98 | |||
99 | /** |
||
100 | * Relevant to "json" location parameters only. This property allows for deep nesting through the use of |
||
101 | * {@see OpenStack\Common\JsonPath}. |
||
102 | * |
||
103 | * @var string |
||
104 | */ |
||
105 | private $path; |
||
106 | |||
107 | /** |
||
108 | * Allows for the prefixing of parameter names. |
||
109 | * |
||
110 | * @var string |
||
111 | */ |
||
112 | private $prefix; |
||
113 | |||
114 | /** |
||
115 | * @param array $data |
||
116 | */ |
||
117 | 195 | public function __construct(array $data) |
|
131 | |||
132 | 195 | private function stockItemSchema(array $data) |
|
138 | |||
139 | 195 | private function stockProperties(array $data) |
|
151 | |||
152 | /** |
||
153 | * Retrieve the name that will be used over the wire. |
||
154 | * |
||
155 | * @return string |
||
156 | */ |
||
157 | 88 | public function getName() |
|
161 | |||
162 | /** |
||
163 | * Indicates whether the user must provide a value for this parameter. |
||
164 | * |
||
165 | * @return bool |
||
166 | */ |
||
167 | 4 | public function isRequired() |
|
171 | |||
172 | /** |
||
173 | * Validates a given user value and checks whether it passes basic sanity checking, such as types. |
||
174 | * |
||
175 | * @param $userValues The value provided by the user |
||
176 | * |
||
177 | * @return bool TRUE if the validation passes |
||
178 | * @throws \Exception If validation fails |
||
179 | */ |
||
180 | 9 | public function validate($userValues) |
|
206 | |||
207 | /** |
||
208 | * Internal method which retrieves a nested property for object parameters. |
||
209 | * |
||
210 | * @param $key The name of the child parameter |
||
211 | * |
||
212 | * @returns Parameter |
||
213 | * @throws \Exception |
||
214 | */ |
||
215 | 3 | private function getNestedProperty($key) |
|
225 | |||
226 | /** |
||
227 | * Internal method which indicates whether the user value is of the same type as the one expected |
||
228 | * by this parameter. |
||
229 | * |
||
230 | * @param $userValue The value being checked |
||
231 | * |
||
232 | * @return bool |
||
233 | */ |
||
234 | private function hasCorrectType($userValue) |
||
249 | |||
250 | /** |
||
251 | * Indicates whether this parameter represents an array type |
||
252 | * |
||
253 | * @return bool |
||
254 | */ |
||
255 | 69 | public function isArray() |
|
259 | |||
260 | /** |
||
261 | * Indicates whether this parameter represents an object type |
||
262 | * |
||
263 | * @return bool |
||
264 | */ |
||
265 | 68 | public function isObject() |
|
269 | |||
270 | 162 | public function getLocation() |
|
274 | |||
275 | /** |
||
276 | * Verifies whether the given location matches the parameter's location. |
||
277 | * |
||
278 | * @param $value |
||
279 | * |
||
280 | * @return bool |
||
281 | */ |
||
282 | 1 | public function hasLocation($value) |
|
286 | |||
287 | /** |
||
288 | * Retrieves the parameter's path. |
||
289 | * |
||
290 | * @return string|null |
||
291 | */ |
||
292 | 62 | public function getPath() |
|
296 | |||
297 | /** |
||
298 | * Retrieves the common schema that an array parameter applies to all its child elements. |
||
299 | * |
||
300 | * @return Parameter |
||
301 | */ |
||
302 | 18 | public function getItemSchema() |
|
306 | |||
307 | /** |
||
308 | * Sets the name of the parameter to a new value |
||
309 | * |
||
310 | * @param string $name |
||
311 | */ |
||
312 | 13 | public function setName($name) |
|
316 | |||
317 | /** |
||
318 | * Retrieves the child parameter for an object parameter. |
||
319 | * |
||
320 | * @param string $name The name of the child property |
||
321 | * |
||
322 | * @return null|Parameter |
||
323 | */ |
||
324 | 21 | public function getProperty($name) |
|
333 | |||
334 | /** |
||
335 | * Retrieves the prefix for a parameter, if any. |
||
336 | * |
||
337 | * @return string|null |
||
338 | */ |
||
339 | 15 | public function getPrefix() |
|
343 | } |