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