]> _ Git - cubist_cms-back.git/commitdiff
wip #5399 @1
authorVincent Vanwaelscappel <vincent@cubedesigners.com>
Fri, 19 Aug 2022 15:50:44 +0000 (17:50 +0200)
committerVincent Vanwaelscappel <vincent@cubedesigners.com>
Fri, 19 Aug 2022 15:50:44 +0000 (17:50 +0200)
src/app/Magic/EntityData.php
src/app/Magic/Fields/Model.php

index 511921f043f203f411ab44e34ed7f695d4c40ec8..1d713074e51cb11adde333cf0cd2474f4a620bb0 100644 (file)
@@ -11,6 +11,9 @@ use Spatie\MediaLibrary\MediaCollections\Models\Media;
 
 class EntityData implements \ArrayAccess
 {
+
+    protected $_offsetPrefix = null;
+
     /**
      * @var array
      */
@@ -47,7 +50,7 @@ class EntityData implements \ArrayAccess
     public function set($offset, $value)
     {
         $offset = square_brackets_to_dots($offset);
-        Arr::set($this->_data, $offset, $this->_fixValue($value));
+        Arr::set($this->_data, $this->_offset($offset), $this->_fixValue($value));
     }
 
     protected function _fixValue($value)
@@ -63,7 +66,7 @@ class EntityData implements \ArrayAccess
         $dot = Arr::dot($value);
         $res = [];
         foreach ($dot as $k => $v) {
-            Arr::set($res, square_brackets_to_dots($k), $v);
+            Arr::set($res, $this->_offset(square_brackets_to_dots($k)), $v);
         }
         return $res;
     }
@@ -74,7 +77,7 @@ class EntityData implements \ArrayAccess
      */
     public function exists($offset)
     {
-        return Arr::has($this->_data, $offset);
+        return Arr::has($this->_data, $this->_offset($offset));
     }
 
     /**
@@ -91,7 +94,7 @@ class EntityData implements \ArrayAccess
      */
     public function unset($offset)
     {
-        Arr::set($this->_data, $offset, null);
+        Arr::set($this->_data, $this->_offset($offset), null);
     }
 
     /**
@@ -106,11 +109,11 @@ class EntityData implements \ArrayAccess
         }
 
         foreach ($offset as $key) {
-            if (!Arr::has($this->_data, $key)) {
+            if (!Arr::has($this->_data, $this->_offset($key))) {
                 continue;
             }
 
-            $res = Arr::get($this->_data, $key, $default);
+            $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...
@@ -157,7 +160,7 @@ class EntityData implements \ArrayAccess
      */
     public function offsetUnset($offset)
     {
-        return $this->unset($offset);
+        $this->unset($offset);
     }
 
     /**
@@ -184,7 +187,7 @@ class EntityData implements \ArrayAccess
      */
     public function __set($name, $value)
     {
-        return $this->set($name, $value);
+        $this->set($name, $value);
     }
 
     /**
@@ -364,6 +367,27 @@ class EntityData implements \ArrayAccess
         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[]
index ed6cd0e79f0444b6f024f93d002499dd16dc4d37..d801b7d70bc21168e2c6ea6f30898e086aa026a8 100644 (file)
@@ -57,7 +57,7 @@ class Model extends Field
             $ttl = 86400;
             $cache = cache()->tags([$tag]);
             if ($force) {
-                static::$_options[$globalCacheKey] = $closure();
+                static::$_options[$globalCacheKey] = $closure->call();
                 $cache->put($globalCacheKey, $ttl, static::$_options[$globalCacheKey]);
             } else {
                 static::$_options[$globalCacheKey] = $cache->remember($globalCacheKey, $ttl, $closure);