1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @license https://github.com/f500/equatable/blob/master/LICENSE MIT |
5
|
|
|
*/ |
6
|
|
|
|
7
|
|
|
declare(strict_types=1); |
8
|
|
|
|
9
|
|
|
namespace F500\Equatable; |
10
|
|
|
|
11
|
|
|
use ArrayIterator; |
12
|
|
|
use Countable; |
13
|
|
|
use F500\Equatable\Exceptions\InvalidArgumentException; |
14
|
|
|
use F500\Equatable\Exceptions\OutOfRangeException; |
15
|
|
|
use IteratorAggregate; |
16
|
|
|
use Traversable; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @copyright Copyright (c) 2015 Future500 B.V. |
20
|
|
|
* @author Jasper N. Brouwer <[email protected]> |
21
|
|
|
*/ |
22
|
|
|
abstract class Collection implements Equatable, Countable, IteratorAggregate |
23
|
|
|
{ |
24
|
|
|
protected $items = []; |
25
|
|
|
|
26
|
63 |
|
public function getIterator(): Traversable |
27
|
|
|
{ |
28
|
63 |
|
return new ArrayIterator($this->items); |
29
|
|
|
} |
30
|
|
|
|
31
|
78 |
|
public function isEmpty(): bool |
32
|
|
|
{ |
33
|
78 |
|
return !$this->items; |
34
|
|
|
} |
35
|
|
|
|
36
|
282 |
|
public function count(): int |
37
|
|
|
{ |
38
|
282 |
|
return count($this->items); |
39
|
|
|
} |
40
|
|
|
|
41
|
57 |
|
public function countItem($value): int |
42
|
|
|
{ |
43
|
57 |
|
$count = 0; |
44
|
|
|
|
45
|
57 |
|
foreach ($this->items as $item) { |
46
|
57 |
|
if ($this->theseAreEqual($item, $value)) { |
47
|
57 |
|
$count++; |
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
|
51
|
57 |
|
return $count; |
52
|
|
|
} |
53
|
|
|
|
54
|
51 |
|
public function contains($value): bool |
55
|
|
|
{ |
56
|
|
|
try { |
57
|
51 |
|
$this->search($value); |
58
|
42 |
|
return true; |
59
|
45 |
|
} catch (OutOfRangeException $e) { |
60
|
45 |
|
return false; |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
abstract public function search($value); |
|
|
|
|
65
|
|
|
|
66
|
66 |
|
public function searchAll($value): Vector |
67
|
|
|
{ |
68
|
66 |
|
$foundPointers = []; |
69
|
|
|
|
70
|
66 |
|
foreach ($this->items as $pointer => $item) { |
71
|
66 |
|
if ($this->theseAreEqual($item, $value)) { |
72
|
66 |
|
$foundPointers[] = $pointer; |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
76
|
66 |
|
return new Vector($foundPointers); |
77
|
|
|
} |
78
|
|
|
|
79
|
6 |
|
public function toArray(): array |
80
|
|
|
{ |
81
|
6 |
|
return $this->items; |
82
|
|
|
} |
83
|
|
|
|
84
|
12 |
|
public function first() |
|
|
|
|
85
|
|
|
{ |
86
|
12 |
|
if ($this->isEmpty()) { |
87
|
6 |
|
throw OutOfRangeException::doesNotContainAnything(); |
88
|
|
|
} |
89
|
|
|
|
90
|
6 |
|
return reset($this->items); |
91
|
|
|
} |
92
|
|
|
|
93
|
12 |
|
public function last() |
|
|
|
|
94
|
|
|
{ |
95
|
12 |
|
if ($this->isEmpty()) { |
96
|
6 |
|
throw OutOfRangeException::doesNotContainAnything(); |
97
|
|
|
} |
98
|
|
|
|
99
|
6 |
|
return end($this->items); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* The reducer callable is given the carry value and an item, |
104
|
|
|
* and should return the value it is reduced to. |
105
|
|
|
* |
106
|
|
|
* function ($carry, $item) { |
107
|
|
|
* return $carry . $item; |
108
|
|
|
* } |
109
|
|
|
*/ |
110
|
12 |
|
public function reduce(callable $reducer, $initial = null) |
111
|
|
|
{ |
112
|
12 |
|
return array_reduce($this->items, $reducer, $initial); |
113
|
|
|
} |
114
|
|
|
|
115
|
267 |
|
protected function theseAreEqual($left, $right): bool |
116
|
|
|
{ |
117
|
267 |
|
if ($left === $right) { |
118
|
126 |
|
return true; |
119
|
|
|
} |
120
|
|
|
|
121
|
240 |
|
if ($left instanceof Equatable && $left->equals($right)) { |
|
|
|
|
122
|
111 |
|
return true; |
123
|
|
|
} |
124
|
|
|
|
125
|
207 |
|
return false; |
126
|
|
|
} |
127
|
|
|
|
128
|
462 |
|
protected function guardAgainstInvalidValue($value) |
129
|
|
|
{ |
130
|
462 |
|
if (!is_scalar($value) && !is_object($value)) { |
131
|
6 |
|
throw InvalidArgumentException::invalidValueInArray('values', 'scalar or object', $value); |
132
|
|
|
} |
133
|
456 |
|
} |
134
|
|
|
} |
135
|
|
|
|
For interface and abstract methods, it is impossible to infer the return type from the immediate code. In these cases, it is generally advisible to explicitly annotate these methods with a
@return
doc comment to communicate to implementors of these methods what they are expected to return.