From c9eea7e0190b8068a8b457c0eaa03ce874c3caaa Mon Sep 17 00:00:00 2001 From: Matias Griese Date: Mon, 19 Nov 2018 13:43:02 +0200 Subject: [PATCH] Flex: Fixed some issues with new orderBy() logic --- system/src/Grav/Framework/Flex/FlexIndex.php | 53 +++++++++++++++---- .../Framework/Object/Base/ObjectTrait.php | 2 +- 2 files changed, 44 insertions(+), 11 deletions(-) diff --git a/system/src/Grav/Framework/Flex/FlexIndex.php b/system/src/Grav/Framework/Flex/FlexIndex.php index 731f55489..baf7ac66f 100644 --- a/system/src/Grav/Framework/Flex/FlexIndex.php +++ b/system/src/Grav/Framework/Flex/FlexIndex.php @@ -70,12 +70,7 @@ class FlexIndex extends ObjectIndex implements FlexCollectionInterface, FlexInde { parent::__construct($entries); - $first = reset($entries); - $keys = $first ? array_keys($first) : []; - $keys = array_combine($keys, $keys) + ['key' => 'key', 'flex_key' => 'flex_key']; - $this->_flexDirectory = $flexDirectory; - $this->_indexKeys = $keys; $this->setKeyField(null); } @@ -211,12 +206,12 @@ class FlexIndex extends ObjectIndex implements FlexCollectionInterface, FlexInde */ public function orderBy(array $orderings) { - if (!$orderings) { + if (!$orderings || !$this->count()) { return $this; } // Check if ordering needs to load the objects. - if (array_diff_key($orderings, $this->_indexKeys)) { + if (array_diff_key($orderings, $this->getIndexKeys())) { return $this->__call('orderBy', [$orderings]); } @@ -335,10 +330,10 @@ class FlexIndex extends ObjectIndex implements FlexCollectionInterface, FlexInde /** * @param array $entries - * @param array $indexes + * @param string $keyField * @return static */ - protected function createFrom(array $entries, $keyField = null) + protected function createFrom(array $entries, string $keyField = null) { $index = new static($entries, $this->_flexDirectory); $index->setKeyField($keyField); @@ -346,11 +341,49 @@ class FlexIndex extends ObjectIndex implements FlexCollectionInterface, FlexInde return $index; } - protected function setKeyField($keyField = null) + /** + * @param string|null $keyField + */ + protected function setKeyField(string $keyField = null) { $this->_keyField = $keyField ?? 'storage_key'; } + protected function getIndexKeys() + { + if (null === $this->_indexKeys) { + $entries = $this->getEntries(); + $first = reset($entries); + if ($first) { + $keys = array_keys($first); + $keys = array_combine($keys, $keys); + } else { + $keys = []; + } + + $this->setIndexKeys($keys); + } + + return $this->_indexKeys; + } + + /** + * @param array $indexKeys + */ + protected function setIndexKeys(array $indexKeys) + { + // Add defaults. + $indexKeys += [ + 'key' => 'key', + 'storage_key' => 'storage_key', + 'storage_timestamp' => 'storage_timestamp', + 'flex_key' => 'flex_key' + ]; + + + $this->_indexKeys = $indexKeys; + } + /** * @return string */ diff --git a/system/src/Grav/Framework/Object/Base/ObjectTrait.php b/system/src/Grav/Framework/Object/Base/ObjectTrait.php index a6cacc901..395e6ad3d 100644 --- a/system/src/Grav/Framework/Object/Base/ObjectTrait.php +++ b/system/src/Grav/Framework/Object/Base/ObjectTrait.php @@ -77,7 +77,7 @@ trait ObjectTrait /** * @param string $property Object property to be updated. - * @param string $value New value. + * @param mixed $value New value. * @return $this */ public function setProperty($property, $value)