diff options
| author | Jan-Hendrik Willms <tleilax+github@gmail.com> | 2021-07-22 16:07:19 +0200 |
|---|---|---|
| committer | Jan-Hendrik Willms <tleilax+github@gmail.com> | 2021-07-22 16:19:12 +0200 |
| commit | a3da1483a9e689846179159355badfec8073dbec (patch) | |
| tree | 770dcca6bdf5f6f2a11b0e7fcbbeda6919a3fc52 /lib/classes/MultiDimArrayObject.class.php | |
current code from svn, revision 62608
Diffstat (limited to 'lib/classes/MultiDimArrayObject.class.php')
| -rw-r--r-- | lib/classes/MultiDimArrayObject.class.php | 127 |
1 files changed, 127 insertions, 0 deletions
diff --git a/lib/classes/MultiDimArrayObject.class.php b/lib/classes/MultiDimArrayObject.class.php new file mode 100644 index 0000000..fb218fe --- /dev/null +++ b/lib/classes/MultiDimArrayObject.class.php @@ -0,0 +1,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; + } +} + |
