|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace DMT\Serializer; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Class ParameterBag |
|
7
|
|
|
* |
|
8
|
|
|
* @package DMT\Serializer |
|
9
|
|
|
*/ |
|
10
|
|
|
class ParameterBag extends \ArrayObject |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* ParameterBag constructor. |
|
14
|
|
|
* |
|
15
|
|
|
* @param ParameterInterface[] $parameters |
|
16
|
|
|
*/ |
|
17
|
38 |
|
public function __construct(array $parameters = []) |
|
18
|
|
|
{ |
|
19
|
38 |
|
foreach ($parameters as $parameter) { |
|
20
|
6 |
|
$this[] = $parameter; |
|
21
|
|
|
} |
|
22
|
38 |
|
} |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Whether a offset exists. |
|
26
|
|
|
* |
|
27
|
|
|
* @param string|int $offset |
|
28
|
|
|
* @return bool |
|
29
|
|
|
*/ |
|
30
|
24 |
|
public function offsetExists($offset): bool |
|
31
|
|
|
{ |
|
32
|
24 |
|
if (gettype($offset) === 'string') { |
|
33
|
24 |
|
$offset = $this->getOffsetFromName($offset); |
|
|
|
|
|
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
24 |
|
if (gettype($offset) === 'integer') { |
|
37
|
4 |
|
return parent::offsetExists($offset); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
24 |
|
return false; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Offset to retrieve. |
|
45
|
|
|
* |
|
46
|
|
|
* @param string|int $offset |
|
47
|
|
|
* @return ParameterInterface|null |
|
48
|
|
|
*/ |
|
49
|
8 |
|
public function offsetGet($offset): ?ParameterInterface |
|
50
|
|
|
{ |
|
51
|
8 |
|
if (gettype($offset) === 'string') { |
|
52
|
2 |
|
$offset = $this->getOffsetFromName($offset); |
|
|
|
|
|
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
8 |
|
if (gettype($offset) === 'integer' && parent::offsetExists($offset)) { |
|
56
|
7 |
|
return parent::offsetGet($offset); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
1 |
|
return null; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Offset to set. |
|
64
|
|
|
* |
|
65
|
|
|
* @param string|int|null $offset |
|
66
|
|
|
* @param ParameterInterface $value |
|
67
|
|
|
*/ |
|
68
|
25 |
|
public function offsetSet($offset, $value): void |
|
69
|
|
|
{ |
|
70
|
25 |
|
if (!$value instanceof ParameterInterface) { |
|
|
|
|
|
|
71
|
1 |
|
throw new \RuntimeException('Parameter(s) in ParameterBag must implement ' . ParameterInterface::class); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
24 |
|
if (!$this->offsetExists($value->getName())) { |
|
75
|
24 |
|
parent::offsetSet(null, $value); |
|
76
|
|
|
} |
|
77
|
24 |
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Offset to unset. |
|
81
|
|
|
* |
|
82
|
|
|
* @param string|int $offset |
|
83
|
|
|
* @return void |
|
84
|
|
|
*/ |
|
85
|
2 |
|
public function offsetUnset($offset): void |
|
86
|
|
|
{ |
|
87
|
2 |
|
if (gettype($offset) === 'string') { |
|
88
|
1 |
|
$offset = $this->getOffsetFromName($offset); |
|
|
|
|
|
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
2 |
|
if (gettype($offset) === 'integer') { |
|
92
|
2 |
|
parent::offsetUnset($offset); |
|
93
|
|
|
} |
|
94
|
2 |
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* Search for the offset that corresponds with the parameter's name. |
|
98
|
|
|
* |
|
99
|
|
|
* @param string $name |
|
100
|
|
|
* @return int|null |
|
101
|
|
|
*/ |
|
102
|
25 |
|
protected function getOffsetFromName(string $name): ?int |
|
103
|
|
|
{ |
|
104
|
25 |
|
foreach ($this as $key => $parameter) { |
|
105
|
17 |
|
if ($parameter->getName() === $name) { |
|
106
|
17 |
|
return $key; |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
25 |
|
return null; |
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
|
This check looks for function or method calls that always return null and whose return value is assigned to a variable.
The method
getObject()can return nothing but null, so it makes no sense to assign that value to a variable.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.