LazyArray::push()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 9
rs 9.6666
cc 2
eloc 7
nc 2
nop 2
1
<?php
2
namespace TheCodingMachine\LazyArray;
3
4
class LazyArray implements \ArrayAccess
5
{
6
    /**
7
     * The array with lazy values
8
     * @var array
9
     */
10
    private $lazyArray;
11
12
    /**
13
     * The array with constructed values
14
     * @var array
15
     */
16
    private $constructedArray = [];
17
18
    /**
19
     * @param array $lazyArray The array with lazy values
20
     */
21
    public function __construct(array $lazyArray = [])
22
    {
23
        $this->lazyArray = $lazyArray;
24
    }
25
26
    /**
27
     * @param string|object $className The FQCN or the instance to put in the array
28
     * @param array ...$params The parameters passed to the constructor.
29
     * @return int The key in the array
30
     */
31
    public function push($className, ...$params) {
32
        if (is_object($className)) {
33
            $this->lazyArray[] = $className;
34
        } else {
35
            $this->lazyArray[] = [ $className, $params ];
36
        }
37
        end($this->lazyArray);
38
        return key($this->lazyArray);
39
    }
40
41
42
    /**
43
     * Whether a offset exists
44
     * @link http://php.net/manual/en/arrayaccess.offsetexists.php
45
     * @param mixed $offset <p>
46
     * An offset to check for.
47
     * </p>
48
     * @return boolean true on success or false on failure.
49
     * </p>
50
     * <p>
51
     * The return value will be casted to boolean if non-boolean was returned.
52
     * @since 5.0.0
53
     */
54
    public function offsetExists($offset)
55
    {
56
        return isset($this->lazyArray[$offset]);
57
    }
58
59
    /**
60
     * Offset to retrieve
61
     * @link http://php.net/manual/en/arrayaccess.offsetget.php
62
     * @param mixed $offset <p>
63
     * The offset to retrieve.
64
     * </p>
65
     * @return mixed Can return all value types.
66
     * @since 5.0.0
67
     */
68
    public function offsetGet($offset)
69
    {
70
        if (isset($this->constructedArray[$offset])) {
71
            return $this->constructedArray[$offset];
72
        } else {
73
            $item = $this->lazyArray[$offset];
74
            if (is_array($item)) {
75
                $className = $item[0];
76
                $params = isset($item[1]) ? $item[1] : [];
77
            } else {
78
                $className = $item;
79
                $params = [];
80
            }
81
            $this->constructedArray[$offset] = new $className(...$params);
82
            return $this->constructedArray[$offset];
83
        }
84
    }
85
86
    /**
87
     * Offset to set
88
     * @link http://php.net/manual/en/arrayaccess.offsetset.php
89
     * @param mixed $offset <p>
90
     * The offset to assign the value to.
91
     * </p>
92
     * @param mixed $value <p>
93
     * The value to set.
94
     * </p>
95
     * @return void
96
     * @since 5.0.0
97
     */
98
    public function offsetSet($offset, $value)
99
    {
100
        throw new \LogicException('Not implemented yet');
101
    }
102
103
    /**
104
     * Offset to unset
105
     * @link http://php.net/manual/en/arrayaccess.offsetunset.php
106
     * @param mixed $offset <p>
107
     * The offset to unset.
108
     * </p>
109
     * @return void
110
     * @since 5.0.0
111
     */
112
    public function offsetUnset($offset)
113
    {
114
        unset($this->lazyArray[$offset]);
115
        unset($this->constructedArray[$offset]);
116
    }
117
}