ArrayCollection::getOnlyElement()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 0
dl 0
loc 11
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\SAML2\Utilities;
6
7
use ArrayIterator;
8
use Closure;
9
use SimpleSAML\SAML2\Exception\RuntimeException;
10
11
use function array_filter;
12
use function array_map;
13
use function array_search;
14
use function count;
15
use function end;
16
use function reset;
17
use function sprintf;
18
19
/**
20
 * Simple Array implementation of Collection.
21
 */
22
class ArrayCollection implements Collection
23
{
24
    /**
25
     * ArrayCollection constructor.
26
     *
27
     * @param array $elements
28
     */
29
    public function __construct(
30
        protected array $elements = [],
31
    ) {
32
    }
33
34
35
    /**
36
     * @param mixed $element
37
     *
38
     */
39
    public function add($element): void
40
    {
41
        $this->elements[] = $element;
42
    }
43
44
45
    /**
46
     * @param mixed $key
47
     *
48
     * @return mixed|null
49
     */
50
    public function get($key)
51
    {
52
        return isset($this->elements[$key]) ? $this->elements[$key] : null;
53
    }
54
55
56
    /**
57
     * @param \Closure $f
58
     *
59
     * @return \SimpleSAML\SAML2\Utilities\ArrayCollection
60
     */
61
    public function filter(Closure $filterFunction): Collection
62
    {
63
        return new self(array_filter($this->elements, $filterFunction));
64
    }
65
66
67
    /**
68
     * @param mixed $key
69
     * @param mixed $value
70
     */
71
    public function set($key, $value): void
72
    {
73
        $this->elements[$key] = $value;
74
    }
75
76
77
    /**
78
     * @param mixed $element
79
     *
80
     */
81
    public function remove($element): void
82
    {
83
        $key = array_search($element, $this->elements);
84
        if ($key === false) {
85
            return;
86
        }
87
        unset($this->elements[$key]);
88
    }
89
90
91
    /**
92
     * @throws \SimpleSAML\SAML2\Exception\RuntimeException
93
     * @return bool|mixed
94
     */
95
    public function getOnlyElement()
96
    {
97
        if ($this->count() !== 1) {
98
            throw new RuntimeException(sprintf(
99
                __METHOD__ . ' requires that the collection has exactly one element, '
100
                . '"%d" elements found',
101
                $this->count(),
102
            ));
103
        }
104
105
        return reset($this->elements);
106
    }
107
108
109
    /**
110
     * @return bool|mixed
111
     */
112
    public function first()
113
    {
114
        return reset($this->elements);
115
    }
116
117
118
    /**
119
     * @return bool|mixed
120
     */
121
    public function last()
122
    {
123
        return end($this->elements);
124
    }
125
126
127
    /**
128
     * @param \Closure $function
129
     *
130
     * @return \SimpleSAML\SAML2\Utilities\ArrayCollection
131
     */
132
    public function map(Closure $function): ArrayCollection
133
    {
134
        return new self(array_map($function, $this->elements));
135
    }
136
137
138
    /**
139
     * @return int
140
     */
141
    public function count(): int
142
    {
143
        return count($this->elements);
144
    }
145
146
147
    /**
148
     * @return \ArrayIterator
149
     */
150
    public function getIterator(): ArrayIterator
151
    {
152
        return new ArrayIterator($this->elements);
153
    }
154
155
156
    /**
157
     * @param mixed $offset
158
     *
159
     * @return bool
160
     */
161
    public function offsetExists($offset): bool
162
    {
163
        return isset($this->elements[$offset]);
164
    }
165
166
167
    /**
168
     * @param mixed $offset
169
     *
170
     * @return mixed
171
     */
172
    #[\ReturnTypeWillChange]
173
    public function offsetGet($offset)
174
    {
175
        return $this->elements[$offset];
176
    }
177
178
179
    /**
180
     * @param mixed $offset
181
     * @param mixed $value
182
     */
183
    public function offsetSet($offset, $value): void
184
    {
185
        $this->elements[$offset] = $value;
186
    }
187
188
189
    /**
190
     * @param $offset
191
     */
192
    public function offsetUnset($offset): void
193
    {
194
        unset($this->elements[$offset]);
195
    }
196
}
197