1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ValueObjects\Structure; |
4
|
|
|
|
5
|
|
|
use ValueObjects\StringLiteral\StringLiteral; |
6
|
|
|
use ValueObjects\ValueObjectInterface; |
7
|
|
|
|
8
|
|
|
class Dictionary extends Collection |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* Returns a new Dictionary object |
12
|
|
|
* |
13
|
|
|
* @param array $array |
|
|
|
|
14
|
|
|
* @return self |
15
|
|
|
*/ |
16
|
3 |
|
public static function fromNative() |
17
|
|
|
{ |
18
|
3 |
|
$array = \func_get_arg(0); |
19
|
3 |
|
$keyValuePairs = array(); |
20
|
|
|
|
21
|
3 |
|
foreach ($array as $arrayKey => $arrayValue) { |
22
|
2 |
|
$key = new StringLiteral(\strval($arrayKey)); |
23
|
|
|
|
24
|
2 |
|
if ($arrayValue instanceof \Traversable || \is_array($arrayValue)) { |
25
|
1 |
|
$value = Collection::fromNative($arrayValue); |
26
|
1 |
|
} else { |
27
|
2 |
|
$value = new StringLiteral(\strval($arrayValue)); |
28
|
|
|
} |
29
|
|
|
|
30
|
2 |
|
$keyValuePairs[] = new KeyValuePair($key, $value); |
31
|
3 |
|
} |
32
|
|
|
|
33
|
3 |
|
$fixedArray = \SplFixedArray::fromArray($keyValuePairs); |
34
|
|
|
|
35
|
3 |
|
return new static($fixedArray); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Returns a new Dictionary object |
40
|
|
|
* |
41
|
|
|
* @param \SplFixedArray $key_value_pairs |
42
|
|
|
*/ |
43
|
8 |
|
public function __construct(\SplFixedArray $key_value_pairs) |
44
|
|
|
{ |
45
|
8 |
|
foreach ($key_value_pairs as $keyValuePair) { |
46
|
7 |
|
if (false === $keyValuePair instanceof KeyValuePair) { |
47
|
1 |
|
$type = \is_object($keyValuePair) ? \get_class($keyValuePair) : \gettype($keyValuePair); |
48
|
1 |
|
throw new \InvalidArgumentException(\sprintf('Passed SplFixedArray object must contains "KeyValuePair" objects only. "%s" given.', $type)); |
49
|
|
|
} |
50
|
8 |
|
} |
51
|
|
|
|
52
|
8 |
|
$this->items = $key_value_pairs; |
53
|
8 |
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Returns a Collection of the keys |
57
|
|
|
* |
58
|
|
|
* @return Collection |
59
|
|
|
*/ |
60
|
2 |
|
public function keys() |
61
|
|
|
{ |
62
|
2 |
|
$count = $this->count()->toNative(); |
63
|
2 |
|
$keysArray = new \SplFixedArray($count); |
64
|
|
|
|
65
|
2 |
|
foreach ($this->items as $key => $item) { |
66
|
2 |
|
$keysArray->offsetSet($key, $item->getKey()); |
67
|
2 |
|
} |
68
|
|
|
|
69
|
2 |
|
return new Collection($keysArray); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Returns a Collection of the values |
74
|
|
|
* |
75
|
|
|
* @return Collection |
76
|
|
|
*/ |
77
|
2 |
|
public function values() |
78
|
|
|
{ |
79
|
2 |
|
$count = $this->count()->toNative(); |
80
|
2 |
|
$valuesArray = new \SplFixedArray($count); |
81
|
|
|
|
82
|
2 |
|
foreach ($this->items as $key => $item) { |
83
|
2 |
|
$valuesArray->offsetSet($key, $item->getValue()); |
84
|
2 |
|
} |
85
|
|
|
|
86
|
2 |
|
return new Collection($valuesArray); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Tells whether $object is one of the keys |
91
|
|
|
* |
92
|
|
|
* @param ValueObjectInterface $object |
93
|
|
|
* @return bool |
94
|
|
|
*/ |
95
|
1 |
|
public function containsKey(ValueObjectInterface $object) |
96
|
|
|
{ |
97
|
1 |
|
$keys = $this->keys(); |
98
|
|
|
|
99
|
1 |
|
return $keys->contains($object); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Tells whether $object is one of the values |
104
|
|
|
* |
105
|
|
|
* @param ValueObjectInterface $object |
106
|
|
|
* @return bool |
107
|
|
|
*/ |
108
|
1 |
|
public function containsValue(ValueObjectInterface $object) |
109
|
|
|
{ |
110
|
1 |
|
$values = $this->values(); |
111
|
|
|
|
112
|
1 |
|
return $values->contains($object); |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.