|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Yiisoft\Arrays; |
|
6
|
|
|
|
|
7
|
|
|
use ArrayIterator; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* `ArrayAccessTrait` provides the implementation for {@see \IteratorAggregate}, {@see \ArrayAccess} |
|
11
|
|
|
* and {@see \Countable}. |
|
12
|
|
|
* |
|
13
|
|
|
* Note that `ArrayAccessTrait` requires the class using it contain a property named `data` which should be an array. |
|
14
|
|
|
* The data will be exposed by `ArrayAccessTrait` to support accessing the class object like an array. |
|
15
|
|
|
* |
|
16
|
|
|
* @property array $data |
|
17
|
|
|
*/ |
|
18
|
|
|
trait ArrayAccessTrait |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* Returns an iterator for traversing the data. |
|
22
|
|
|
* This method is required by the SPL interface {@see \IteratorAggregate}. |
|
23
|
|
|
* It will be implicitly called when you use `foreach` to traverse the collection. |
|
24
|
|
|
* |
|
25
|
|
|
* @return ArrayIterator An iterator for traversing the cookies in the collection. |
|
26
|
|
|
*/ |
|
27
|
1 |
|
public function getIterator(): ArrayIterator |
|
28
|
|
|
{ |
|
29
|
1 |
|
return new ArrayIterator($this->data); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Returns the number of data items. |
|
34
|
|
|
* This method is required by Countable interface. |
|
35
|
|
|
* |
|
36
|
|
|
* @return int Number of data elements. |
|
37
|
|
|
*/ |
|
38
|
1 |
|
public function count(): int |
|
39
|
|
|
{ |
|
40
|
1 |
|
return count($this->data); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* This method is required by the interface {@see \ArrayAccess}. |
|
45
|
|
|
* |
|
46
|
|
|
* @param mixed $offset The offset to check on. |
|
47
|
|
|
*/ |
|
48
|
1 |
|
public function offsetExists(mixed $offset): bool |
|
49
|
|
|
{ |
|
50
|
1 |
|
return isset($this->data[$offset]); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* This method is required by the interface {@see \ArrayAccess}. |
|
55
|
|
|
* |
|
56
|
|
|
* @param mixed $offset The offset to retrieve element. |
|
57
|
|
|
* |
|
58
|
|
|
* @return mixed The element at the offset, null if no element is found at the offset. |
|
59
|
|
|
*/ |
|
60
|
1 |
|
public function offsetGet(mixed $offset): mixed |
|
61
|
|
|
{ |
|
62
|
1 |
|
return $this->data[$offset] ?? null; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* This method is required by the interface {@see \ArrayAccess}. |
|
67
|
|
|
* |
|
68
|
|
|
* @param mixed $offset The offset to set element. |
|
69
|
|
|
* @param mixed $value The element value. |
|
70
|
|
|
*/ |
|
71
|
1 |
|
public function offsetSet(mixed $offset, mixed $value): void |
|
72
|
|
|
{ |
|
73
|
1 |
|
if ($offset === null) { |
|
74
|
1 |
|
$this->data[] = $value; |
|
75
|
|
|
} else { |
|
76
|
1 |
|
$this->data[$offset] = $value; |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* This method is required by the interface {@see \ArrayAccess}. |
|
82
|
|
|
* |
|
83
|
|
|
* @param mixed $offset The offset to unset element. |
|
84
|
|
|
*/ |
|
85
|
1 |
|
public function offsetUnset(mixed $offset): void |
|
86
|
|
|
{ |
|
87
|
1 |
|
unset($this->data[$offset]); |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
|