Passed
Push — scrutinizer-migrate-to-new-eng... ( 58afd6 )
by Alexander
18:11
created

ArrayAccessTrait::getIterator()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
c 0
b 0
f 0
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 int number of data elements.
38
     */
39 4
    public function count()
40
    {
41 4
        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 bool
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 int $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 9
    public function offsetGet($offset)
60
    {
61 9
        return isset($this->data[$offset]) ? $this->data[$offset] : null;
62
    }
63
64
    /**
65
     * This method is required by the interface [[\ArrayAccess]].
66
     * @param int $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