Data::offsetGet()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 4
rs 10
1
<?php
2
/**
3
 *
4
 * This file is part of the Aura project for PHP.
5
 *
6
 * @package Aura.Marshal
7
 *
8
 * @license https://opensource.org/licenses/mit-license.php MIT
9
 *
10
 */
11
namespace Aura\Marshal;
12
13
/**
14
 *
15
 * Represents a data set. This is similar to the SPL ArrayObject, but allows
16
 * you easier access to the underlying data itself. This class serves as a
17
 * base for ...
18
 *
19
 * - the generic type object, where $data represents an IdentityMap;
20
 *
21
 * - the generic entity object, where $data represents the entity fields; and
22
 *
23
 * - the generic collection object, where $data represents an array of
24
 *   entities.
25
 *
26
 * @package Aura.Marshal
27
 * 
28
 * @implements \ArrayAccess<int|string, mixed>
29
 * @implements \IteratorAggregate<int|string, mixed>
30
 *
31
 */
32
class Data implements \ArrayAccess, \Countable, \IteratorAggregate
33
{
34
    /**
35
     *
36
     * Key-value pairs of data.
37
     *
38
     * @var array<int|string, GenericEntity|mixed>
39
     *
40
     */
41
    protected $data = [];
42
43
    /**
44
     *
45
     * Constructor.
46
     *
47
     * @param array<int|string, mixed> $data The data for this object.
48
     *
49
     */
50
    public function __construct(array $data = [])
51
    {
52
        $this->data = $data;
53
    }
54
55
    /**
56
     *
57
     * ArrayAccess: does the requested key exist?
58
     *
59
     * @param int|string $key The requested key.
60
     *
61
     * @return bool
62
     *
63
     */
64
    #[\ReturnTypeWillChange]
65
    public function offsetExists($key)
66
    {
67
        return array_key_exists($key, $this->data);
68
    }
69
70
    /**
71
     *
72
     * ArrayAccess: get a key value.
73
     *
74
     * @param int|string $key The requested key.
75
     *
76
     * @return mixed
77
     *
78
     */
79
    #[\ReturnTypeWillChange]
80
    public function offsetGet($key)
81
    {
82
        return $this->data[$key];
83
    }
84
85
    /**
86
     *
87
     * ArrayAccess: set a key value.
88
     *
89
     * @param int|string $key The requested key.
90
     *
91
     * @param mixed $val The value to set it to.
92
     *
93
     * @return void
94
     *
95
     */
96
    #[\ReturnTypeWillChange]
97
    public function offsetSet($key, $val)
98
    {
99
        $this->data[$key] = $val;
100
    }
101
102
    /**
103
     *
104
     * ArrayAccess: unset a key.
105
     *
106
     * @param int|string $key The requested key.
107
     *
108
     * @return void
109
     *
110
     */
111
    #[\ReturnTypeWillChange]
112
    public function offsetUnset($key)
113
    {
114
        unset($this->data[$key]);
115
    }
116
117
    /**
118
     *
119
     * Countable: how many keys are there?
120
     *
121
     * @return int
122
     *
123
     */
124
    #[\ReturnTypeWillChange]
125
    public function count()
126
    {
127
        return count($this->data);
128
    }
129
130
    /**
131
     *
132
     * IteratorAggregate: returns an external iterator for this struct.
133
     *
134
     * @return DataIterator
135
     *
136
     */
137
    #[\ReturnTypeWillChange]
138
    public function getIterator()
139
    {
140
        return new DataIterator($this, array_keys($this->data));
141
    }
142
}
143