Some bug fixes in Flex collection / index

This commit is contained in:
Matias Griese
2018-11-17 00:34:53 +02:00
parent 37e880b64a
commit cc78fcf31c
2 changed files with 34 additions and 17 deletions

View File

@@ -28,6 +28,9 @@ class FlexCollection extends ObjectCollection implements FlexCollectionInterface
/** @var FlexDirectory */
private $_flexDirectory;
/** @var string */
private $_keyField;
/**
* @return array
*/
@@ -81,13 +84,17 @@ class FlexCollection extends ObjectCollection implements FlexCollectionInterface
* instance should be created when constructor semantics have changed.
*
* @param array $elements Elements.
* @package string|null $keyField
*
* @return static
* @throws \InvalidArgumentException
*/
protected function createFrom(array $elements)
protected function createFrom(array $elements, $keyField = null)
{
return new static($elements, $this->_flexDirectory);
$collection = new static($elements, $this->_flexDirectory);
$collection->setKeyField($keyField);
return $collection;
}
/**
@@ -237,19 +244,6 @@ class FlexCollection extends ObjectCollection implements FlexCollectionInterface
return $object ? $object->getMetaData() : [];
}
/**
* @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
*/
@@ -300,7 +294,15 @@ class FlexCollection extends ObjectCollection implements FlexCollectionInterface
}
}
return $this->createFrom($entries);
return $this->createFrom($entries, $keyField);
}
/**
* @return string
*/
public function getKeyField() : string
{
return $this->_keyField ?? 'key';
}
/**
@@ -350,6 +352,16 @@ class FlexCollection extends ObjectCollection implements FlexCollectionInterface
return $elements;
}
public function __debugInfo()
{
return [
'type' => $this->getType(),
'key' => $this->getKey(),
'entries_key' => $this->getKeyField(),
'objects' => $this->jsonSerialize()
];
}
/**
* @param string $layout
* @return \Twig_Template
@@ -385,4 +397,9 @@ class FlexCollection extends ObjectCollection implements FlexCollectionInterface
return $flex->getDirectory($type);
}
protected function setKeyField($keyField = null)
{
$this->_keyField = $keyField ?? 'storage_key';
}
}

View File

@@ -163,7 +163,7 @@ class FlexIndex extends ObjectIndex implements FlexCollectionInterface, FlexInde
*/
public function getKeyField() : string
{
return $this->_keyField;
return $this->_keyField ?? 'key';
}
/**