aboutsummaryrefslogtreecommitdiff
path: root/lib/classes/MultiDimArrayObject.class.php
blob: fb218febb63bf5328c13d5fde39ea56ccb853674 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
<?php
/**
 * MultiDimArrayObject
 *
 *
 * @author      André Noack <noack@data-quest.de>
 *
 */
class MultiDimArrayObject extends StudipArrayObject
{
    /**
     * Constructor
     *
     * @param array  $input
     * @param int    $flags
     * @param string $iteratorClass
     */
    public function __construct($input = [], $flags = self::STD_PROP_LIST, $iteratorClass = 'ArrayIterator')
    {
        parent::__construct([], $flags, $iteratorClass);
        $this->exchangeArray($input);
    }

    /**
     * Appends the value
     *
     * @param  mixed $value
     * @return void
     */
    public function append($value)
    {
        $this->offsetSet(null, $value);
    }

    /**
     * Exchange the array for another one.
     *
     * @param  array|ArrayObject $data
     * @return
     */
    public function exchangeArray($data)
    {
        if (!is_array($data) && !is_object($data)) {
            throw new InvalidArgumentException('Passed variable is not an array or object');
        }

        if ($data instanceof \StudipArrayObject) {
            $data = $data->getArrayCopy();
        }
        if (!is_array($data)) {
            $data = (array) $data;
        }

        $this->storage = $this->recursiveArrayToArrayObjects($data);

    }

    /**
     * Creates a copy of the ArrayObject.
     *
     * @return array
     */
    public function getArrayCopy()
    {
        $ret = [];
        foreach($this->storage as $key => $value) {
            if ($value instanceOf StudipArrayObject) {
                $ret[$key] = $value->getArrayCopy();
            } else {
                $ret[$key] = $value;
            }
        }
        return $ret;
    }

    /**
     * Create a new iterator from an ArrayObject instance
     *
     * @return \Iterator
     */
    public function getIterator()
    {
        $class = $this->iteratorClass;

        return new $class($this->getArrayCopy());
    }

    /**
     * Sets the value at the specified key to value
     *
     * @param  mixed $key
     * @param  mixed $value
     * @return void
     */
    public function offsetSet($key, $value)
    {
        $new_value = $this->recursiveArrayToArrayObjects($value);
        if (is_array($new_value)) {
            $new_value = new static($new_value, $this->getFlags(), $this->getIteratorClass());
        }
        if (is_null($key)) {
            return $this->storage[] = $new_value;
        }
        $this->storage[$key] = $new_value;
    }

    protected function recursiveArrayToArrayObjects($data)
    {
        if ($data instanceOf StudipArrayObject) {
            $data = $data->getArrayCopy();
        }
        if (is_array($data)) {
            $new_data = [];
            foreach ($data as $key => $value) {
                $new_value = $this->recursiveArrayToArrayObjects($value);
                if (is_array($new_value)) {
                    $new_data[$key] = new static($new_value, $this->getFlags(), $this->getIteratorClass());
                } else {
                    $new_data[$key] = $value;
                }
            }
            return $new_data;
        }
        return $data;
    }
}