Flex improvements

This commit is contained in:
Matias Griese
2019-08-29 12:25:06 +03:00
parent e36a2ea1b0
commit 2e245cd36f
4 changed files with 45 additions and 13 deletions

View File

@@ -110,7 +110,7 @@ abstract class AbstractIndexCollection implements CollectionInterface
*/
public function removeElement($element)
{
$key = $this->isAllowedElement($element) ? $element->getKey() : null;
$key = $this->isAllowedElement($element) ? $this->getCurrentKey($element) : null;
if (!$key || !isset($this->entries[$key])) {
return false;
@@ -178,7 +178,7 @@ abstract class AbstractIndexCollection implements CollectionInterface
*/
public function contains($element)
{
$key = $this->isAllowedElement($element) ? $element->getKey() : null;
$key = $this->isAllowedElement($element) ? $this->getCurrentKey($element) : null;
return $key && isset($this->entries[$key]);
}
@@ -196,7 +196,7 @@ abstract class AbstractIndexCollection implements CollectionInterface
*/
public function indexOf($element)
{
$key = $this->isAllowedElement($element) ? $element->getKey() : null;
$key = $this->isAllowedElement($element) ? $this->getCurrentKey($element) : null;
return $key && isset($this->entries[$key]) ? $key : null;
}
@@ -246,10 +246,6 @@ abstract class AbstractIndexCollection implements CollectionInterface
throw new \InvalidArgumentException('Invalid argument $value');
}
if ($key !== $value->getKey()) {
$value->setKey($key);
}
$this->entries[$key] = $this->getElementMeta($value);
}
@@ -262,7 +258,7 @@ abstract class AbstractIndexCollection implements CollectionInterface
throw new \InvalidArgumentException('Invalid argument $element');
}
$this->entries[$element->getKey()] = $this->getElementMeta($element);
$this->entries[$this->getCurrentKey($element)] = $this->getElementMeta($element);
return true;
}
@@ -477,6 +473,11 @@ abstract class AbstractIndexCollection implements CollectionInterface
$this->entries = $entries;
}
protected function getCurrentKey($element)
{
return $element->getKey();
}
/**
* @param string $key
* @param mixed $value

View File

@@ -293,6 +293,11 @@ class FlexCollection extends ObjectCollection implements FlexCollectionInterface
return $this->getFlexDirectory()->getIndex($this->getKeys(), $this->getKeyField());
}
public function getCollection()
{
return $this;
}
/**
* {@inheritdoc}
* @see FlexCollectionInterface::render()

View File

@@ -258,6 +258,11 @@ class FlexIndex extends ObjectIndex implements FlexCollectionInterface, FlexInde
return $this;
}
public function getCollection()
{
return $this->loadCollection();
}
/**
* {@inheritdoc}
* @see FlexCollectionInterface::render()
@@ -359,7 +364,7 @@ class FlexIndex extends ObjectIndex implements FlexCollectionInterface, FlexInde
}
// Order by current field.
if ($ordering === 'DESC') {
if (strtoupper($ordering) === 'DESC') {
arsort($search, SORT_NATURAL);
} else {
asort($search, SORT_NATURAL);
@@ -579,6 +584,22 @@ class FlexIndex extends ObjectIndex implements FlexCollectionInterface, FlexInde
return $object->getMetaData();
}
protected function getCurrentKey($element)
{
$keyField = $this->getKeyField();
if ($keyField === 'storage_key') {
return $element->getStorageKey();
}
if ($keyField === 'flex_key') {
return $element->getFlexKey();
}
if ($keyField === 'key') {
return $element->getKey();
}
return $element->getKey();
}
/**
* @param FlexStorageInterface $storage
* @param array $index Saved index

View File

@@ -630,12 +630,15 @@ class FlexObject implements FlexObjectInterface, FlexAuthorizeInterface
$result = $storage->replaceRows([$key => $this->prepareStorage()]);
$value = reset($result);
$storageKey = (string)key($result);
$meta = $value['__META'] ?? null;
if ($meta) {
$this->_meta = $meta;
}
$storageKey = $meta['storage_key'] ?? (string)key($result);
if ($value && $storageKey) {
$this->setStorageKey($storageKey);
if (!$this->hasKey()) {
$this->setKey($storageKey);
}
$this->setKey($meta['key'] ?? $storageKey);
}
// FIXME: For some reason locator caching isn't cleared for the file, investigate!
@@ -810,6 +813,8 @@ class FlexObject implements FlexObjectInterface, FlexAuthorizeInterface
{
return [
'type:private' => $this->getFlexType(),
'storage_key:protected' => $this->getStorageKey(),
'storage_timestamp:protected' => $this->getTimestamp(),
'key:private' => $this->getKey(),
'elements:private' => $this->getElements(),
'storage:private' => $this->getStorage()