From 09805f7091edf271e567c9c10fa1b7cefd4eef47 Mon Sep 17 00:00:00 2001 From: Matias Griese Date: Wed, 13 Feb 2019 23:30:35 +0200 Subject: [PATCH] Fixed `FlexIndex` keys being lost when `FlexCollection` gets loaded --- CHANGELOG.md | 1 + system/src/Grav/Framework/Flex/FlexCollection.php | 8 ++++++-- system/src/Grav/Framework/Flex/FlexDirectory.php | 15 +++++++++------ system/src/Grav/Framework/Flex/FlexIndex.php | 12 ++++++++---- .../Flex/Interfaces/FlexCollectionInterface.php | 3 ++- 5 files changed, 26 insertions(+), 13 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1e294ea26..c323493eb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ 1. [](#bugfix) * Fixed validation for select field type with selectize * Fixed `Flex[class]::getType()` to return the same value in every class + * Fixed `FlexIndex` keys being lost when `FlexCollection` gets loaded # v1.6.0-rc.2 ## 02/07/2019 diff --git a/system/src/Grav/Framework/Flex/FlexCollection.php b/system/src/Grav/Framework/Flex/FlexCollection.php index 73a359946..7530e8771 100644 --- a/system/src/Grav/Framework/Flex/FlexCollection.php +++ b/system/src/Grav/Framework/Flex/FlexCollection.php @@ -59,11 +59,15 @@ class FlexCollection extends ObjectCollection implements FlexCollectionInterface /** * @param array $entries * @param FlexDirectory $directory + * @param string $keyField * @return static */ - public static function createFromArray(array $entries, FlexDirectory $directory) : FlexCollectionInterface + public static function createFromArray(array $entries, FlexDirectory $directory, string $keyField = null) : FlexCollectionInterface { - return new static($entries, $directory); + $instance = new static($entries, $directory); + $instance->setKeyField($keyField); + + return $instance; } /** diff --git a/system/src/Grav/Framework/Flex/FlexDirectory.php b/system/src/Grav/Framework/Flex/FlexDirectory.php index 63bf8d809..cef6ee92e 100644 --- a/system/src/Grav/Framework/Flex/FlexDirectory.php +++ b/system/src/Grav/Framework/Flex/FlexDirectory.php @@ -399,26 +399,28 @@ class FlexDirectory implements FlexAuthorizeInterface /** * @param array $entries + * @param string $keyField * @return FlexCollectionInterface */ - public function createCollection(array $entries): FlexCollectionInterface + public function createCollection(array $entries, string $keyField = null): FlexCollectionInterface { /** @var string|FlexCollectionInterface $className */ $className = $this->collectionClassName ?: $this->getCollectionClass(); - return $className::createFromArray($entries, $this); + return $className::createFromArray($entries, $this, $keyField); } /** * @param array $entries + * @param string $keyField * @return FlexCollectionInterface */ - public function createIndex(array $entries): FlexCollectionInterface + public function createIndex(array $entries, string $keyField = null): FlexCollectionInterface { /** @var string|FlexIndexInterface $className */ $className = $this->indexClassName ?: $this->getIndexClass(); - return $className::createFromArray($entries, $this); + return $className::createFromArray($entries, $this, $keyField); } /** @@ -461,11 +463,12 @@ class FlexDirectory implements FlexAuthorizeInterface /** * @param array $entries + * @param string $keyField * @return FlexCollectionInterface */ - public function loadCollection(array $entries): FlexCollectionInterface + public function loadCollection(array $entries, string $keyField = null): FlexCollectionInterface { - return $this->createCollection($this->loadObjects($entries)); + return $this->createCollection($this->loadObjects($entries), $keyField); } /** diff --git a/system/src/Grav/Framework/Flex/FlexIndex.php b/system/src/Grav/Framework/Flex/FlexIndex.php index c19308994..4efd5762b 100644 --- a/system/src/Grav/Framework/Flex/FlexIndex.php +++ b/system/src/Grav/Framework/Flex/FlexIndex.php @@ -47,11 +47,15 @@ class FlexIndex extends ObjectIndex implements FlexCollectionInterface, FlexInde /** * @param array[] $entries * @param FlexDirectory $directory + * @param string $keyField * @return static */ - public static function createFromArray(array $entries, FlexDirectory $directory) : FlexCollectionInterface + public static function createFromArray(array $entries, FlexDirectory $directory, string $keyField = null) : FlexCollectionInterface { - return new static($entries, $directory); + $instance = new static($entries, $directory); + $instance->setKeyField($keyField); + + return $instance; } /** @@ -427,7 +431,7 @@ class FlexIndex extends ObjectIndex implements FlexCollectionInterface, FlexInde */ protected function loadElements(array $entries = null) : array { - return $this->_flexDirectory->loadObjects($entries ?? $this->withKeyField()->getEntries()); + return $this->_flexDirectory->loadObjects($entries ?? $this->getEntries()); } /** @@ -436,7 +440,7 @@ class FlexIndex extends ObjectIndex implements FlexCollectionInterface, FlexInde */ protected function loadCollection(array $entries = null) : CollectionInterface { - return $this->_flexDirectory->loadCollection($entries ?? $this->withKeyField()->getEntries()); + return $this->_flexDirectory->loadCollection($entries ?? $this->getEntries(), $this->_keyField); } /** diff --git a/system/src/Grav/Framework/Flex/Interfaces/FlexCollectionInterface.php b/system/src/Grav/Framework/Flex/Interfaces/FlexCollectionInterface.php index 4e7a58a19..210c075f9 100644 --- a/system/src/Grav/Framework/Flex/Interfaces/FlexCollectionInterface.php +++ b/system/src/Grav/Framework/Flex/Interfaces/FlexCollectionInterface.php @@ -25,9 +25,10 @@ interface FlexCollectionInterface extends ObjectCollectionInterface, NestedObjec /** * @param array $entries * @param FlexDirectory $directory + * @param string $keyField * @return static */ - public static function createFromArray(array $entries, FlexDirectory $directory) : FlexCollectionInterface; + public static function createFromArray(array $entries, FlexDirectory $directory, string $keyField = null) : FlexCollectionInterface; /** * @param array $elements