From d3a2189a6e6b6034d80bb8405926bc5eb4f593d9 Mon Sep 17 00:00:00 2001 From: Vincent Vanwaelscappel Date: Fri, 19 Aug 2022 17:50:44 +0200 Subject: [PATCH] wip #5399 @1 --- src/app/Magic/EntityData.php | 40 +++++++++++++++++++++++++++------- src/app/Magic/Fields/Model.php | 2 +- 2 files changed, 33 insertions(+), 9 deletions(-) diff --git a/src/app/Magic/EntityData.php b/src/app/Magic/EntityData.php index 511921f..1d71307 100644 --- a/src/app/Magic/EntityData.php +++ b/src/app/Magic/EntityData.php @@ -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[] diff --git a/src/app/Magic/Fields/Model.php b/src/app/Magic/Fields/Model.php index ed6cd0e..d801b7d 100644 --- a/src/app/Magic/Fields/Model.php +++ b/src/app/Magic/Fields/Model.php @@ -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); -- 2.39.5