Added initial support for Flex keys (unique identifier)

This commit is contained in:
Matias Griese
2018-11-16 15:13:12 +02:00
parent 9e8f700119
commit 75b2307d30
3 changed files with 81 additions and 2 deletions

View File

@@ -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
*/

View File

@@ -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[]
*/

View File

@@ -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
*/