1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ValueObjects\Structure; |
4
|
|
|
|
5
|
|
|
use ValueObjects\Util\Util; |
6
|
|
|
use ValueObjects\Number\Natural; |
7
|
|
|
use ValueObjects\StringLiteral\StringLiteral; |
8
|
|
|
use ValueObjects\ValueObjectInterface; |
9
|
|
|
|
10
|
|
|
class Collection implements ValueObjectInterface |
11
|
|
|
{ |
12
|
|
|
/** @var \SplFixedArray */ |
13
|
|
|
protected $items; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Returns a new Collection object |
17
|
|
|
* |
18
|
|
|
* @param \SplFixedArray $array |
|
|
|
|
19
|
|
|
* @return self |
20
|
|
|
*/ |
21
|
3 |
|
public static function fromNative() |
22
|
|
|
{ |
23
|
3 |
|
$array = \func_get_arg(0); |
24
|
3 |
|
$items = array(); |
25
|
|
|
|
26
|
3 |
|
foreach ($array as $item) { |
27
|
3 |
|
if ($item instanceof \Traversable || \is_array($item)) { |
28
|
2 |
|
$items[] = static::fromNative($item); |
29
|
2 |
|
} else { |
30
|
3 |
|
$items[] = new StringLiteral(\strval($item)); |
31
|
|
|
} |
32
|
3 |
|
} |
33
|
|
|
|
34
|
3 |
|
$fixedArray = \SplFixedArray::fromArray($items); |
35
|
|
|
|
36
|
3 |
|
return new static($fixedArray); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Returns a new Collection object |
41
|
|
|
* |
42
|
|
|
* @return self |
|
|
|
|
43
|
|
|
*/ |
44
|
12 |
|
public function __construct(\SplFixedArray $items) |
45
|
|
|
{ |
46
|
12 |
|
foreach ($items as $item) { |
47
|
12 |
|
if (false === $item instanceof ValueObjectInterface) { |
48
|
1 |
|
$type = \is_object($item) ? \get_class($item) : \gettype($item); |
49
|
1 |
|
throw new \InvalidArgumentException(\sprintf('Passed SplFixedArray object must contains "ValueObjectInterface" objects only. "%s" given.', $type)); |
50
|
|
|
} |
51
|
12 |
|
} |
52
|
|
|
|
53
|
12 |
|
$this->items = $items; |
54
|
12 |
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Tells whether two Collection are equal by comparing their size and items (item order matters) |
58
|
|
|
* |
59
|
|
|
* @param ValueObjectInterface $collection |
60
|
|
|
* @return bool |
61
|
|
|
*/ |
62
|
6 |
|
public function sameValueAs(ValueObjectInterface $collection) |
63
|
|
|
{ |
64
|
6 |
|
if (false === Util::classEquals($this, $collection) || false === $this->count()->sameValueAs($collection->count())) { |
|
|
|
|
65
|
1 |
|
return false; |
66
|
|
|
} |
67
|
|
|
|
68
|
6 |
|
$arrayCollection = $collection->toArray(); |
|
|
|
|
69
|
|
|
|
70
|
6 |
|
foreach ($this->items as $index => $item) { |
71
|
6 |
|
if (!isset($arrayCollection[$index]) || false === $item->sameValueAs($arrayCollection[$index])) { |
72
|
1 |
|
return false; |
73
|
|
|
} |
74
|
6 |
|
} |
75
|
|
|
|
76
|
6 |
|
return true; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Returns the number of objects in the collection |
81
|
|
|
* |
82
|
|
|
* @return Natural |
83
|
|
|
*/ |
84
|
10 |
|
public function count() |
85
|
|
|
{ |
86
|
10 |
|
return new Natural($this->items->count()); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Tells whether the Collection contains an object |
91
|
|
|
* |
92
|
|
|
* @param ValueObjectInterface $object |
93
|
|
|
* @return bool |
94
|
|
|
*/ |
95
|
3 |
|
public function contains(ValueObjectInterface $object) |
96
|
|
|
{ |
97
|
3 |
|
foreach ($this->items as $item) { |
98
|
3 |
|
if ($item->sameValueAs($object)) { |
99
|
3 |
|
return true; |
100
|
|
|
} |
101
|
3 |
|
} |
102
|
|
|
|
103
|
3 |
|
return false; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Returns a native array representation of the Collection |
108
|
|
|
* |
109
|
|
|
* @return array |
110
|
|
|
*/ |
111
|
7 |
|
public function toArray() |
112
|
|
|
{ |
113
|
7 |
|
return $this->items->toArray(); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Returns a native string representation of the Collection object |
118
|
|
|
* |
119
|
|
|
* @return string |
120
|
|
|
*/ |
121
|
1 |
|
public function __toString() |
122
|
|
|
{ |
123
|
1 |
|
$string = \sprintf('%s(%d)', \get_class($this), $this->count()->toNative()); |
124
|
|
|
|
125
|
1 |
|
return $string; |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|
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.