1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @link http://www.yiiframework.com/ |
4
|
|
|
* @copyright Copyright (c) 2008 Yii Software LLC |
5
|
|
|
* @license http://www.yiiframework.com/license/ |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace yii\base; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* ArrayAccessTrait provides the implementation for [[\IteratorAggregate]], [[\ArrayAccess]] and [[\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
|
|
|
* @author Qiang Xue <[email protected]> |
19
|
|
|
* @since 2.0 |
20
|
|
|
*/ |
21
|
|
|
trait ArrayAccessTrait |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* Returns an iterator for traversing the data. |
25
|
|
|
* This method is required by the SPL interface [[\IteratorAggregate]]. |
26
|
|
|
* It will be implicitly called when you use `foreach` to traverse the collection. |
27
|
|
|
* @return \ArrayIterator an iterator for traversing the cookies in the collection. |
28
|
|
|
*/ |
29
|
|
|
public function getIterator() |
30
|
|
|
{ |
31
|
|
|
return new \ArrayIterator($this->data); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Returns the number of data items. |
36
|
|
|
* This method is required by Countable interface. |
37
|
|
|
* @return integer number of data elements. |
38
|
|
|
*/ |
39
|
3 |
|
public function count() |
40
|
|
|
{ |
41
|
3 |
|
return count($this->data); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* This method is required by the interface [[\ArrayAccess]]. |
46
|
|
|
* @param mixed $offset the offset to check on |
47
|
|
|
* @return boolean |
48
|
|
|
*/ |
49
|
|
|
public function offsetExists($offset) |
50
|
|
|
{ |
51
|
|
|
return isset($this->data[$offset]); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* This method is required by the interface [[\ArrayAccess]]. |
56
|
|
|
* @param integer $offset the offset to retrieve element. |
57
|
|
|
* @return mixed the element at the offset, null if no element is found at the offset |
58
|
|
|
*/ |
59
|
4 |
|
public function offsetGet($offset) |
60
|
|
|
{ |
61
|
4 |
|
return isset($this->data[$offset]) ? $this->data[$offset] : null; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* This method is required by the interface [[\ArrayAccess]]. |
66
|
|
|
* @param integer $offset the offset to set element |
67
|
|
|
* @param mixed $item the element value |
68
|
|
|
*/ |
69
|
|
|
public function offsetSet($offset, $item) |
70
|
|
|
{ |
71
|
|
|
$this->data[$offset] = $item; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* This method is required by the interface [[\ArrayAccess]]. |
76
|
|
|
* @param mixed $offset the offset to unset element |
77
|
|
|
*/ |
78
|
|
|
public function offsetUnset($offset) |
79
|
|
|
{ |
80
|
|
|
unset($this->data[$offset]); |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|