|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
namespace OpenStack\Common\Transport; |
|
4
|
|
|
|
|
5
|
|
|
use OpenStack\Common\Api\Parameter; |
|
6
|
|
|
use OpenStack\Common\JsonPath; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Class responsible for populating the JSON body of a {@see GuzzleHttp\Message\Request} object. |
|
10
|
|
|
* |
|
11
|
|
|
* @package OpenStack\Common\Transport |
|
12
|
|
|
*/ |
|
13
|
|
|
class JsonSerializer |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* Populates the actual value into a JSON field, i.e. it has reached the end of the line and no |
|
17
|
|
|
* further nesting is required. |
|
18
|
|
|
* |
|
19
|
|
|
* @param Parameter $param The schema that defines how the JSON field is being populated |
|
20
|
|
|
* @param mixed $userValue The user value that is populating a JSON field |
|
21
|
|
|
* @param array $json The existing JSON structure that will be populated |
|
22
|
|
|
* |
|
23
|
|
|
* @return array|mixed |
|
24
|
|
|
*/ |
|
25
|
70 |
|
private function stockValue(Parameter $param, $userValue, array $json): array |
|
26
|
|
|
{ |
|
27
|
70 |
|
$name = $param->getName(); |
|
28
|
70 |
|
if ($path = $param->getPath()) { |
|
29
|
7 |
|
$jsonPath = new JsonPath($json); |
|
30
|
7 |
|
$jsonPath->set(sprintf("%s.%s", $path, $name), $userValue); |
|
31
|
7 |
|
$json = $jsonPath->getStructure(); |
|
32
|
70 |
|
} elseif ($name) { |
|
33
|
67 |
|
$json[$name] = $userValue; |
|
34
|
67 |
|
} else { |
|
35
|
12 |
|
$json[] = $userValue; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
70 |
|
return $json; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Populates a value into an array-like structure. |
|
43
|
|
|
* |
|
44
|
|
|
* @param Parameter $param The schema that defines how the JSON field is being populated |
|
45
|
|
|
* @param mixed $userValue The user value that is populating a JSON field |
|
46
|
|
|
* |
|
47
|
|
|
* @return array|mixed |
|
48
|
|
|
*/ |
|
49
|
12 |
|
private function stockArrayJson(Parameter $param, array $userValue): array |
|
50
|
|
|
{ |
|
51
|
12 |
|
$elems = []; |
|
52
|
12 |
|
foreach ($userValue as $item) { |
|
53
|
12 |
|
$elems = $this->stockJson($param->getItemSchema(), $item, $elems); |
|
|
|
|
|
|
54
|
12 |
|
} |
|
55
|
12 |
|
return $elems; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Populates a value into an object-like structure. |
|
60
|
|
|
* |
|
61
|
|
|
* @param Parameter $param The schema that defines how the JSON field is being populated |
|
62
|
|
|
* @param mixed $userValue The user value that is populating a JSON field |
|
63
|
|
|
* |
|
64
|
|
|
* @return array |
|
65
|
|
|
*/ |
|
66
|
21 |
|
private function stockObjectJson(Parameter $param, \stdClass $userValue): array |
|
67
|
|
|
{ |
|
68
|
21 |
|
$object = []; |
|
69
|
21 |
|
foreach ($userValue as $key => $val) { |
|
|
|
|
|
|
70
|
21 |
|
$object = $this->stockJson($param->getProperty($key), $val, $object); |
|
|
|
|
|
|
71
|
21 |
|
} |
|
72
|
21 |
|
return $object; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* A generic method that will populate a JSON structure with a value according to a schema. It |
|
77
|
|
|
* supports multiple types and will delegate accordingly. |
|
78
|
|
|
* |
|
79
|
|
|
* @param Parameter $param The schema that defines how the JSON field is being populated |
|
80
|
|
|
* @param mixed $userValue The user value that is populating a JSON field |
|
81
|
|
|
* @param array $json The existing JSON structure that will be populated |
|
82
|
|
|
* |
|
83
|
|
|
* @return array |
|
84
|
|
|
*/ |
|
85
|
70 |
|
public function stockJson(Parameter $param, $userValue, array $json): array |
|
86
|
|
|
{ |
|
87
|
70 |
|
if ($param->isArray()) { |
|
88
|
12 |
|
$userValue = $this->stockArrayJson($param, $userValue); |
|
89
|
70 |
|
} elseif ($param->isObject()) { |
|
90
|
21 |
|
$userValue = $this->stockObjectJson($param, $this->serializeObjectValue($userValue)); |
|
91
|
21 |
|
} |
|
92
|
|
|
// Populate the final value |
|
93
|
70 |
|
return $this->stockValue($param, $userValue, $json); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
private function serializeObjectValue($value) |
|
97
|
|
|
{ |
|
98
|
|
|
if (is_object($value)) { |
|
99
|
|
|
if ($value instanceof Serializable) { |
|
100
|
|
|
$value = $value->serialize(); |
|
101
|
|
|
} elseif (!($value instanceof \stdClass)) { |
|
102
|
|
|
throw new \InvalidArgumentException(sprintf( |
|
103
|
|
|
'When an object value is provided, it must either be \stdClass or implement the Serializable ' |
|
104
|
|
|
. 'interface, you provided %s', print_r($value, true) |
|
105
|
|
|
)); |
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
return (object) $value; |
|
110
|
|
|
} |
|
111
|
|
|
} |
|
112
|
|
|
|
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: