ArrayAccessTrait   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
eloc 10
c 1
b 0
f 0
dl 0
loc 70
ccs 14
cts 14
cp 1
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getIterator() 0 3 1
A count() 0 3 1
A offsetUnset() 0 3 1
A offsetGet() 0 3 1
A offsetExists() 0 3 1
A offsetSet() 0 6 2
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