Completed
Push — master ( 873ba2...664c4a )
by Carlos
01:29
created

ArrayAccessible::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of the overtrue/wechat.
5
 *
6
 * (c) overtrue <[email protected]>
7
 *
8
 * This source file is subject to the MIT license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
12
namespace EasyWeChat\Kernel\Support;
13
14
use ArrayAccess;
15
use ArrayIterator;
16
use EasyWeChat\Kernel\Contracts\Arrayable;
17
use IteratorAggregate;
18
19
/**
20
 * Class ArrayAccessible.
21
 *
22
 * @author overtrue <[email protected]>
23
 */
24
class ArrayAccessible implements ArrayAccess, IteratorAggregate, Arrayable
25
{
26
    private $array;
27
28
    public function __construct(array $array = [])
29
    {
30
        $this->array = $array;
31
    }
32
33
    public function offsetExists($offset)
34
    {
35
        return array_key_exists($offset, $this->array);
36
    }
37
38
    public function offsetGet($offset)
39
    {
40
        return $this->array[$offset];
41
    }
42
43
    public function offsetSet($offset, $value)
44
    {
45
        if (null === $offset) {
46
            $this->array[] = $value;
47
        } else {
48
            $this->array[$offset] = $value;
49
        }
50
    }
51
52
    public function offsetUnset($offset)
53
    {
54
        unset($this->array[$offset]);
55
    }
56
57
    public function getIterator()
58
    {
59
        return new ArrayIterator($this->array);
60
    }
61
62
    public function toArray()
63
    {
64
        return $this->array;
65
    }
66
}
67