1 | <?php |
||
2 | |||
3 | declare(strict_types=1); |
||
4 | |||
5 | namespace Stratadox\ImmutableCollection; |
||
6 | |||
7 | use function array_search; |
||
8 | use function in_array; |
||
9 | use function is_object; |
||
10 | use Stratadox\Collection\Collection; |
||
11 | use Stratadox\Collection\NotFound; |
||
12 | |||
13 | /** |
||
14 | * Behaviour to allow "searching" the immutable collection. |
||
15 | * |
||
16 | * Provides access to searching behaviour in the form of methods that |
||
17 | * return information on the existence or position of the items in the |
||
18 | * original collection. |
||
19 | * |
||
20 | * @package Stratadox\Collection |
||
21 | * @author Stratadox |
||
22 | */ |
||
23 | trait Searching |
||
24 | { |
||
25 | /** |
||
26 | * @see Searchable::has() |
||
27 | * @param mixed $item |
||
28 | * @return bool |
||
29 | */ |
||
30 | public function has($item): bool |
||
31 | { |
||
32 | return $this->positionOf($item) !== false; |
||
33 | } |
||
34 | |||
35 | /** |
||
36 | * @see Searchable::hasThe() |
||
37 | * @param object $object |
||
38 | * @return bool |
||
39 | */ |
||
40 | public function hasThe($object): bool |
||
41 | { |
||
42 | return in_array($object, $this->items(), true); |
||
43 | } |
||
44 | |||
45 | /** |
||
46 | * @see Searchable::find() |
||
47 | * @param mixed $item |
||
48 | * @return int |
||
49 | * @throws NotFound |
||
50 | */ |
||
51 | public function find($item): int |
||
52 | { |
||
53 | $position = $this->positionOf($item); |
||
54 | $this->mustBeValid($position, $item); |
||
55 | return $position; |
||
56 | } |
||
57 | |||
58 | /** |
||
59 | * @see Searchable::findThe() |
||
60 | * @param object $object |
||
61 | * @return int |
||
62 | * @throws NotFound |
||
63 | */ |
||
64 | public function findThe($object): int |
||
65 | { |
||
66 | $position = array_search($object, $this->items(), true); |
||
67 | $this->mustBeValid($position, $object); |
||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
68 | return $position; |
||
0 ignored issues
–
show
|
|||
69 | } |
||
70 | |||
71 | /** |
||
72 | * @param mixed $item The item we're looking for. |
||
73 | * @return int|bool The position or false. |
||
74 | */ |
||
75 | private function positionOf($item) |
||
76 | { |
||
77 | if (is_object($item)) { |
||
78 | return array_search($item, $this->items(), false); |
||
79 | } |
||
80 | return array_search($item, $this->items(), true); |
||
0 ignored issues
–
show
|
|||
81 | } |
||
82 | |||
83 | /** |
||
84 | * @param int|bool $position The position or false. |
||
85 | * @param mixed $whatWeAreLookingFor The item we're looking for. |
||
86 | * @throws NotFound When the position is false. |
||
87 | */ |
||
88 | private function mustBeValid($position, $whatWeAreLookingFor): void |
||
89 | { |
||
90 | if ($position === false) { |
||
91 | /** @var Collection $this */ |
||
92 | throw NoSuchValue::couldNotFind($this, $whatWeAreLookingFor); |
||
93 | } |
||
94 | } |
||
95 | |||
96 | /** @see Collection::items() */ |
||
97 | abstract public function items(): array; |
||
98 | } |
||
99 |