Passed
Push — main ( 2f076f...aa2ba1 )
by Breno
02:06
created

CollectionTrait::reject()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 4
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 9
rs 10
1
<?php
2
declare(strict_types=1);
3
4
namespace BrenoRoosevelt\Validation\DataStructure;
5
6
use ArrayIterator;
7
8
trait CollectionTrait
9
{
10
    protected array $elements = [];
11
12
    protected function insert(mixed $value, $key = null): void
13
    {
14
        if ($key !== null) {
15
            $this->elements[$key] = $value;
16
        } else {
17
            $this->elements[] = $value;
18
        }
19
    }
20
21
    public function get(string|int $index, $default = null): mixed
22
    {
23
        return $this->elements[$index] ?? $default;
24
    }
25
26
    public function deleteElement(mixed $element): bool
27
    {
28
        $index = $this->indexOf($element);
29
        if ($index === false) {
30
            return false;
31
        }
32
33
        unset($this->elements[$index]);
34
        return true;
35
    }
36
37
    public function deleteByIndex($index): bool
38
    {
39
        if (!array_key_exists($index, $this->elements)) {
40
            return false;
41
        }
42
43
        unset($this->elements[$index]);
44
        return true;
45
    }
46
47
    public function indexOf(mixed $element): bool|int|string
48
    {
49
        return array_search($element, $this->elements, true);
50
    }
51
52
    public function hasIndex($index): bool
53
    {
54
        return array_key_exists($index, $this->elements);
55
    }
56
57
    public function hasElement(mixed $element): bool
58
    {
59
        return $this->indexOf($element) !== false;
60
    }
61
62
    public function clear(): static
63
    {
64
        $this->elements = [];
65
        return $this;
66
    }
67
68
    public function values(): array
69
    {
70
        return array_values($this->elements);
71
    }
72
73
    public function keys(): array
74
    {
75
        return array_keys($this->elements);
76
    }
77
78
    public function accept(callable $callback): static
79
    {
80
        foreach ($this->elements as $index => $element) {
81
            if (true !== call_user_func_array($callback, [$element, $index])) {
82
                unset($this->elements[$index]);
83
            }
84
        }
85
86
        return $this;
87
    }
88
89
    public function reject(callable $callback): static
90
    {
91
        foreach ($this->elements as $index => $element) {
92
            if (true === call_user_func_array($callback, [$element, $index])) {
93
                unset($this->elements[$index]);
94
            }
95
        }
96
97
        return $this;
98
    }
99
100
    public function forEach(callable $callback, bool $stopable = false): static
101
    {
102
        foreach ($this->elements as $index => $element) {
103
            $result = call_user_func_array($callback, [$element, $index]);
104
            if ($result === false && $stopable) {
105
                break;
106
            }
107
        }
108
109
        return $this;
110
    }
111
112
    public function count(): int
113
    {
114
        return count($this->elements);
115
    }
116
117
    public function isEmpty(): bool
118
    {
119
        return $this->count() === 0;
120
    }
121
122
    public function getIterator(): ArrayIterator
123
    {
124
        return new ArrayIterator($this->elements);
125
    }
126
}
127