mirror of
https://github.com/getgrav/grav.git
synced 2026-07-08 10:03:03 +02:00
Added initial support for Flex keys (unique identifier)
This commit is contained in:
@@ -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
|
||||
*/
|
||||
|
||||
@@ -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[]
|
||||
*/
|
||||
|
||||
@@ -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
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user