aboutsummaryrefslogtreecommitdiff
path: root/lib/classes/MultiDimArrayObject.php
diff options
context:
space:
mode:
authorPhilipp Schüttlöffel <schuettloeffel@zqs.uni-hannover.de>2024-09-24 10:53:31 +0200
committerPhilipp Schüttlöffel <schuettloeffel@zqs.uni-hannover.de>2024-09-24 10:53:31 +0200
commit4459dd7917f4d1c34f40bb68f0e991e9c3d53e4c (patch)
tree5c07151ae61276d334e88f6309c30d439a85c12e /lib/classes/MultiDimArrayObject.php
parentda0022e5c1abbf9825ae76debaabdff7e8623bb4 (diff)
parent97a188592c679890a25c37ab78463add76a52ff7 (diff)
Merge branch 'main' into issue-3911issue-3911
Diffstat (limited to 'lib/classes/MultiDimArrayObject.php')
-rw-r--r--lib/classes/MultiDimArrayObject.php129
1 files changed, 129 insertions, 0 deletions
diff --git a/lib/classes/MultiDimArrayObject.php b/lib/classes/MultiDimArrayObject.php
new file mode 100644
index 0000000..b578018
--- /dev/null
+++ b/lib/classes/MultiDimArrayObject.php
@@ -0,0 +1,129 @@
+<?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 array
+ */
+ 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;
+ }
+
+ $storage = $this->storage;
+
+ $this->storage = $this->recursiveArrayToArrayObjects($data);
+
+ return $storage;
+ }
+
+ /**
+ * 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
+ */
+ public function getIterator(): Traversable
+ {
+ $class = $this->iteratorClass;
+
+ return new $class($this->getArrayCopy());
+ }
+
+ /**
+ * Sets the value at the specified key to value
+ *
+ * @param mixed $key
+ * @param mixed $value
+ */
+ public function offsetSet($key, $value): void
+ {
+ $new_value = $this->recursiveArrayToArrayObjects($value);
+ if (is_array($new_value)) {
+ $class = get_called_class();
+ $new_value = new $class($new_value, $this->getFlags(), $this->getIteratorClass());
+ }
+ if (is_null($key)) {
+ $this->storage[] = $new_value;
+ } else {
+ $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)) {
+ $class = get_called_class();
+ $new_data[$key] = new $class($new_value, $this->getFlags(), $this->getIteratorClass());
+ } else {
+ $new_data[$key] = $value;
+ }
+ }
+ return $new_data;
+ }
+ return $data;
+ }
+}