]> _ Git - cubist_cms-back.git/commitdiff
wip #5408 @0.5
authorVincent Vanwaelscappel <vincent@cubedesigners.com>
Mon, 22 Aug 2022 14:10:02 +0000 (16:10 +0200)
committerVincent Vanwaelscappel <vincent@cubedesigners.com>
Mon, 22 Aug 2022 14:10:02 +0000 (16:10 +0200)
src/app/Magic/Data.php [new file with mode: 0644]
src/app/Magic/EntityData.php

diff --git a/src/app/Magic/Data.php b/src/app/Magic/Data.php
new file mode 100644 (file)
index 0000000..b2982da
--- /dev/null
@@ -0,0 +1,221 @@
+<?php
+
+namespace Cubist\Backpack\Magic;
+
+use Illuminate\Support\Arr;
+use Illuminate\Support\Facades\App;
+
+class Data implements \ArrayAccess
+{
+    protected $_offsetPrefix = null;
+
+    /**
+     * @var array
+     */
+    protected $_data = [];
+
+    /**
+     * @param $data
+     */
+    public function __construct($data = [])
+    {
+        $this->setRawData($data);
+    }
+
+    /**
+     * @param string $offset
+     * @param $value
+     */
+    public function set($offset, $value)
+    {
+        $offset = square_brackets_to_dots($offset);
+        Arr::set($this->_data, $this->_offset($offset), $this->_fixValue($value));
+    }
+
+    protected function _fixValue($value)
+    {
+        if (!is_array($value)) {
+            return $value;
+        }
+        return $this->_fixArray($value);
+    }
+
+    protected function _fixArray($value)
+    {
+        $dot = Arr::dot($value);
+        $res = [];
+        foreach ($dot as $k => $v) {
+            Arr::set($res, $this->_offset(square_brackets_to_dots($k)), $v);
+        }
+        return $res;
+    }
+
+    /**
+     * @param string $offset
+     * @return bool
+     */
+    public function exists($offset)
+    {
+        return Arr::has($this->_data, $this->_offset($offset));
+    }
+
+    /**
+     * @param string $offset
+     * @return bool
+     */
+    public function has($offset)
+    {
+        return $this->exists($offset);
+    }
+
+    /**
+     * @param string $offset
+     */
+    public function unset($offset)
+    {
+        Arr::set($this->_data, $this->_offset($offset), null);
+    }
+
+    /**
+     * @param string|array $offset
+     * @param mixed $default
+     * @return mixed
+     */
+    public function get($offset, $default = null)
+    {
+        if (!is_array($offset)) {
+            $offset = [$offset];
+        }
+
+        foreach ($offset as $key) {
+            if (!Arr::has($this->_data, $this->_offset($key))) {
+                continue;
+            }
+
+            $res = Arr::get($this->_data, $this->_offset($key), $default);
+
+            // If an array value is saved without any items, it might be returned
+            // as a string "[]" when instead it should be an empty array...
+            if ($res === '[]') {
+                $res = [];
+            }
+
+            // Get the translated value if the array contains a key matching the locale.
+            // We have to check if the key exists because the actual value may be null.
+            // Using isset() on the locale key would give a false negative if the value is null.
+            if (is_array($res) && array_key_exists(App::getLocale(), $res)) {
+                $res = $res[App::getLocale()];
+            }
+
+            if (null === $res) {
+                return $default;
+            }
+            return $res;
+
+        }
+        return $default;
+    }
+
+    /**
+     * @param mixed $offset
+     * @param mixed $value
+     */
+    public function offsetSet($offset, $value)
+    {
+        $this->set($offset, $value);
+    }
+
+    /**
+     * @param mixed $offset
+     * @return bool
+     */
+    public function offsetExists($offset)
+    {
+        return $this->exists($offset);
+    }
+
+    /**
+     * @param mixed $offset
+     */
+    public function offsetUnset($offset)
+    {
+        $this->unset($offset);
+    }
+
+    /**
+     * @param mixed $offset
+     * @return mixed
+     */
+    public function offsetGet($offset)
+    {
+        return $this->get($offset);
+    }
+
+    /**
+     * @param $name string
+     * @return mixed
+     */
+    public function __get($name)
+    {
+        return $this->get($name);
+    }
+
+    /**
+     * @param string $name
+     * @param mixed $value
+     */
+    public function __set($name, $value)
+    {
+        $this->set($name, $value);
+    }
+
+    /**
+     * @param string $name
+     */
+    public function __unset($name)
+    {
+        $this->unset($name);
+    }
+
+    public function __isset($name)
+    {
+        return $this->has($name);
+    }
+
+
+    /**
+     * @return array
+     */
+    public function getRawData()
+    {
+        return $this->_data;
+    }
+
+    public function setRawData(array $data)
+    {
+        $this->_data = $data;
+    }
+
+    /**
+     * @return null
+     */
+    public function getOffsetPrefix()
+    {
+        return $this->_offsetPrefix;
+    }
+
+    /**
+     * @param null $offsetPrefix
+     */
+    public function setOffsetPrefix($offsetPrefix): void
+    {
+        $this->_offsetPrefix = $offsetPrefix;
+    }
+
+    protected function _offset($offset)
+    {
+        return null === $this->_offsetPrefix ? $offset : $this->_offsetPrefix . $offset;
+    }
+
+
+}
index 1d713074e51cb11adde333cf0cd2474f4a620bb0..d0ca06622b3467cc6cfdba404ac1d206f01c1f26 100644 (file)
@@ -1,23 +1,14 @@
 <?php
 
-
 namespace Cubist\Backpack\Magic;
 
-
 use Cubist\Backpack\Magic\Models\CubistMagicAbstractModel;
-use Illuminate\Support\Arr;
-use Illuminate\Support\Facades\App;
 use Spatie\MediaLibrary\MediaCollections\Models\Media;
 
-class EntityData implements \ArrayAccess
+class EntityData extends Data
 {
 
-    protected $_offsetPrefix = null;
 
-    /**
-     * @var array
-     */
-    protected $_data = [];
     /**
      * @var CubistMagicAbstractModel
      */
@@ -39,170 +30,6 @@ class EntityData implements \ArrayAccess
         $this->_entity = $entity;
     }
 
-    public function __construct()
-    {
-    }
-
-    /**
-     * @param string $offset
-     * @param $value
-     */
-    public function set($offset, $value)
-    {
-        $offset = square_brackets_to_dots($offset);
-        Arr::set($this->_data, $this->_offset($offset), $this->_fixValue($value));
-    }
-
-    protected function _fixValue($value)
-    {
-        if (!is_array($value)) {
-            return $value;
-        }
-        return $this->_fixArray($value);
-    }
-
-    protected function _fixArray($value)
-    {
-        $dot = Arr::dot($value);
-        $res = [];
-        foreach ($dot as $k => $v) {
-            Arr::set($res, $this->_offset(square_brackets_to_dots($k)), $v);
-        }
-        return $res;
-    }
-
-    /**
-     * @param string $offset
-     * @return bool
-     */
-    public function exists($offset)
-    {
-        return Arr::has($this->_data, $this->_offset($offset));
-    }
-
-    /**
-     * @param string $offset
-     * @return bool
-     */
-    public function has($offset)
-    {
-        return $this->exists($offset);
-    }
-
-    /**
-     * @param string $offset
-     */
-    public function unset($offset)
-    {
-        Arr::set($this->_data, $this->_offset($offset), null);
-    }
-
-    /**
-     * @param string|array $offset
-     * @param mixed $default
-     * @return mixed
-     */
-    public function get($offset, $default = null)
-    {
-        if (!is_array($offset)) {
-            $offset = [$offset];
-        }
-
-        foreach ($offset as $key) {
-            if (!Arr::has($this->_data, $this->_offset($key))) {
-                continue;
-            }
-
-            $res = Arr::get($this->_data, $this->_offset($key), $default);
-
-            // If an array value is saved without any items, it might be returned
-            // as a string "[]" when instead it should be an empty array...
-            if ($res === '[]') {
-                $res = [];
-            }
-
-            // Get the translated value if the array contains a key matching the locale.
-            // We have to check if the key exists because the actual value may be null.
-            // Using isset() on the locale key would give a false negative if the value is null.
-            if (is_array($res) && array_key_exists(App::getLocale(), $res)) {
-                $res = $res[App::getLocale()];
-            }
-
-            if (null === $res) {
-                return $default;
-            }
-            return $res;
-
-        }
-        return $default;
-    }
-
-    /**
-     * @param mixed $offset
-     * @param mixed $value
-     */
-    public function offsetSet($offset, $value)
-    {
-        $this->set($offset, $value);
-    }
-
-    /**
-     * @param mixed $offset
-     * @return bool
-     */
-    public function offsetExists($offset)
-    {
-        return $this->exists($offset);
-    }
-
-    /**
-     * @param mixed $offset
-     */
-    public function offsetUnset($offset)
-    {
-        $this->unset($offset);
-    }
-
-    /**
-     * @param mixed $offset
-     * @return mixed
-     */
-    public function offsetGet($offset)
-    {
-        return $this->get($offset);
-    }
-
-    /**
-     * @param $name string
-     * @return mixed
-     */
-    public function __get($name)
-    {
-        return $this->get($name);
-    }
-
-    /**
-     * @param string $name
-     * @param mixed $value
-     */
-    public function __set($name, $value)
-    {
-        $this->set($name, $value);
-    }
-
-    /**
-     * @param string $name
-     */
-    public function __unset($name)
-    {
-        $this->unset($name);
-    }
-
-    public function __isset($name)
-    {
-        return $this->has($name);
-    }
-
     public function getMediaCollection($offset)
     {
         if (!$this->exists($offset)) {
@@ -359,35 +186,6 @@ class EntityData implements \ArrayAccess
         return empty($res) ? $default : $res;
     }
 
-    /**
-     * @return array
-     */
-    public function getRawData()
-    {
-        return $this->_data;
-    }
-
-    /**
-     * @return null
-     */
-    public function getOffsetPrefix()
-    {
-        return $this->_offsetPrefix;
-    }
-
-    /**
-     * @param null $offsetPrefix
-     */
-    public function setOffsetPrefix($offsetPrefix): void
-    {
-        $this->_offsetPrefix = $offsetPrefix;
-    }
-
-    protected function _offset($offset)
-    {
-        return null === $this->_offsetPrefix ? $offset : $this->_offsetPrefix . $offset;
-    }
-
     /**
      * @param $entities CubistMagicAbstractModel[]
      * @return PageData[]