Passed
Push — main ( fdff82...257176 )
by Breno
01:48
created

CollectionTrait::get()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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