|
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
|
|
|
public function getIterator(): Traversable |
|
27
|
|
|
{ |
|
28
|
|
|
return new ArrayIterator($this->items); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
public function isEmpty(): bool |
|
32
|
|
|
{ |
|
33
|
|
|
return !$this->items; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
public function count(): int |
|
37
|
|
|
{ |
|
38
|
|
|
return count($this->items); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
public function countItem($value): int |
|
42
|
|
|
{ |
|
43
|
|
|
$count = 0; |
|
44
|
|
|
|
|
45
|
|
|
foreach ($this->items as $item) { |
|
46
|
|
|
if ($this->theseAreEqual($item, $value)) { |
|
47
|
|
|
$count++; |
|
48
|
|
|
} |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
return $count; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
public function contains($value): bool |
|
55
|
|
|
{ |
|
56
|
|
|
try { |
|
57
|
|
|
$this->search($value); |
|
58
|
|
|
return true; |
|
59
|
|
|
} catch (OutOfRangeException $e) { |
|
60
|
|
|
return false; |
|
61
|
|
|
} |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
abstract public function search($value); |
|
|
|
|
|
|
65
|
|
|
|
|
66
|
|
|
public function searchAll($value): Vector |
|
67
|
|
|
{ |
|
68
|
|
|
$foundKeys = []; |
|
69
|
|
|
|
|
70
|
|
|
foreach ($this->items as $pointer => $item) { |
|
71
|
|
|
if ($this->theseAreEqual($item, $value)) { |
|
72
|
|
|
$foundKeys[] = $pointer; |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
return new Vector($foundKeys); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* The reducer callable is given the carry value and an item, |
|
81
|
|
|
* and should return the value it is reduced to. |
|
82
|
|
|
* |
|
83
|
|
|
* function ($carry, $item) { |
|
84
|
|
|
* return $carry . $item; |
|
85
|
|
|
* } |
|
86
|
|
|
*/ |
|
87
|
|
|
public function reduce(callable $reducer, $initial = null) |
|
88
|
|
|
{ |
|
89
|
|
|
return array_reduce($this->items, $reducer, $initial); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
protected function theseAreEqual($left, $right): bool |
|
93
|
|
|
{ |
|
94
|
|
|
if ($left === $right) { |
|
95
|
|
|
return true; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
if ($left instanceof Equatable && $left->equals($right)) { |
|
|
|
|
|
|
99
|
|
|
return true; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
return false; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
protected function guardAgainstInvalidValue($value): void |
|
106
|
|
|
{ |
|
107
|
|
|
if (!is_scalar($value) && !is_object($value)) { |
|
108
|
|
|
throw InvalidArgumentException::invalidValueTypeInArray('values', 'scalar or object', $value); |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
} |
|
112
|
|
|
|
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
@returndoc comment to communicate to implementors of these methods what they are expected to return.