diff --git a/system/src/Grav/Framework/Flex/FlexCollection.php b/system/src/Grav/Framework/Flex/FlexCollection.php index 0c12f80f7..1ebdb3fc7 100644 --- a/system/src/Grav/Framework/Flex/FlexCollection.php +++ b/system/src/Grav/Framework/Flex/FlexCollection.php @@ -211,6 +211,34 @@ class FlexCollection extends ObjectCollection implements FlexCollectionInterface return $this->_flexDirectory; } + /** + * @return string[] + */ + public function getFlexKeys() + { + // Get storage keys for the objects. + $keys = []; + + foreach ($this as $key => $object) { + $keys[$object->getFlexKey()] = $key; + } + + return $keys; + } + + /** + * @param string $flexKey + * @return mixed|null + */ + public function locate(string $flexKey) + { + $keys = $this->getFlexKeys(); + + $key = $keys[$flexKey] ?? null; + + return $key ? $this->get($key) : null; + } + /** * @return string */ diff --git a/system/src/Grav/Framework/Flex/FlexIndex.php b/system/src/Grav/Framework/Flex/FlexIndex.php index 71672ecdc..59874fc17 100644 --- a/system/src/Grav/Framework/Flex/FlexIndex.php +++ b/system/src/Grav/Framework/Flex/FlexIndex.php @@ -94,12 +94,43 @@ class FlexIndex extends ObjectIndex implements FlexCollectionInterface, FlexInde // Get storage keys for the objects. $keys = []; foreach ($this->getEntries() as $key => $value) { - $keys[\is_array($value) ? $value['storage_key'] ?? $value[0] : $key] = $key; + $keys[$value['storage_key'] ?? $key] = $key; } return $keys; } + /** + * @return string[] + */ + public function getFlexKeys() + { + // Get storage keys for the objects. + $keys = []; + $type = $this->_flexDirectory->getType() . '.obj:'; + + foreach ($this->getEntries() as $key => $value) { + $flexKey = $value['flex_key'] ?? $type . ($value['storage_key'] ?? $key); + + $keys[$flexKey] = $key; + } + + return $keys; + } + + /** + * @param string $flexKey + * @return mixed|null + */ + public function locate(string $flexKey) + { + $keys = $this->getFlexKeys(); + + $key = $keys[$flexKey] ?? null; + + return $key ? $this->get($key) : null; + } + /** * @return int[] */ diff --git a/system/src/Grav/Framework/Flex/FlexObject.php b/system/src/Grav/Framework/Flex/FlexObject.php index 7f5dbd421..23bacdb6b 100644 --- a/system/src/Grav/Framework/Flex/FlexObject.php +++ b/system/src/Grav/Framework/Flex/FlexObject.php @@ -189,6 +189,16 @@ class FlexObject implements FlexObjectInterface, FlexAuthorizeInterface return $this->getBlueprint(); } + /** + * Returns a string representation of this object. + * + * @return string + */ + public function getFlexKey() + { + return $this->_flexDirectory->getType() . '.obj:' . $this->getStorageKey(); + } + /** * @return string */ @@ -210,7 +220,7 @@ class FlexObject implements FlexObjectInterface, FlexAuthorizeInterface */ public function getStorageKey() { - return $this->_storageKey ?? $this->__toString(); + return $this->_storageKey ?? $this->getType() . '@@' . spl_object_hash($this); } /** @@ -466,6 +476,16 @@ class FlexObject implements FlexObjectInterface, FlexAuthorizeInterface return $this; } + /** + * Returns a string representation of this object. + * + * @return string + */ + public function __toString() + { + return $this->getFlexKey(); + } + /** * @return array */