From 02d48693ddda198bd3fca8d0d2c6dab8fa820ef9 Mon Sep 17 00:00:00 2001 From: Matias Griese Date: Thu, 15 Nov 2018 10:01:09 +0200 Subject: [PATCH] Added `select()` and `unselect()` methods to `CollectionInterface` and its base classes Added `orderBy()` and `limit()` methods to `ObjectCollectionInterface` and its base classes --- CHANGELOG.md | 3 + .../Collection/AbstractIndexCollection.php | 11 ++++ .../Collection/AbstractLazyCollection.php | 18 ++++++ .../Framework/Collection/ArrayCollection.php | 31 ++++++++++ .../Collection/CollectionInterface.php | 18 ++++++ .../Grav/Framework/Flex/FlexCollection.php | 58 +------------------ .../Interfaces/ObjectCollectionInterface.php | 13 +++++ .../Framework/Object/ObjectCollection.php | 21 +++++++ .../src/Grav/Framework/Object/ObjectIndex.php | 19 ++++++ 9 files changed, 136 insertions(+), 56 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3d7b74dda..4162f62c5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,9 @@ # v1.6.0-beta.7 ## mm/dd/2018 +1. [](#new) + * Added `select()` and `unselect()` methods to `CollectionInterface` and its base classes + * Added `orderBy()` and `limit()` methods to `ObjectCollectionInterface` and its base classes 1. [](#improved) * Improve Flex storage 1. [](#bugfix) diff --git a/system/src/Grav/Framework/Collection/AbstractIndexCollection.php b/system/src/Grav/Framework/Collection/AbstractIndexCollection.php index 9098afaa4..8beed3156 100644 --- a/system/src/Grav/Framework/Collection/AbstractIndexCollection.php +++ b/system/src/Grav/Framework/Collection/AbstractIndexCollection.php @@ -396,6 +396,17 @@ abstract class AbstractIndexCollection implements CollectionInterface return $this->createFrom($list); } + /** + * Un-select items from collection. + * + * @param array $keys + * @return static + */ + public function unselect(array $keys) + { + return $this->select(array_diff($this->getKeys(), $keys)); + } + /** * Split collection into chunks. * diff --git a/system/src/Grav/Framework/Collection/AbstractLazyCollection.php b/system/src/Grav/Framework/Collection/AbstractLazyCollection.php index f13574606..27d9bcea2 100644 --- a/system/src/Grav/Framework/Collection/AbstractLazyCollection.php +++ b/system/src/Grav/Framework/Collection/AbstractLazyCollection.php @@ -52,6 +52,24 @@ abstract class AbstractLazyCollection extends BaseAbstractLazyCollection impleme return $this->collection->chunk($size); } + /** + * {@inheritDoc} + */ + public function select(array $keys) + { + $this->initialize(); + return $this->collection->select($keys); + } + + /** + * {@inheritDoc} + */ + public function unselect(array $keys) + { + $this->initialize(); + return $this->collection->unselect($keys); + } + /** * {@inheritDoc} */ diff --git a/system/src/Grav/Framework/Collection/ArrayCollection.php b/system/src/Grav/Framework/Collection/ArrayCollection.php index b48210dac..0a5c46988 100644 --- a/system/src/Grav/Framework/Collection/ArrayCollection.php +++ b/system/src/Grav/Framework/Collection/ArrayCollection.php @@ -52,6 +52,37 @@ class ArrayCollection extends BaseArrayCollection implements CollectionInterface return array_chunk($this->toArray(), $size, true); } + /** + * Select items from collection. + * + * Collection is returned in the order of $keys given to the function. + * + * @param array $keys + * @return static + */ + public function select(array $keys) + { + $list = []; + foreach ($keys as $key) { + if ($this->containsKey($key)) { + $list[$key] = $this->get($key); + } + } + + return $this->createFrom($list); + } + + /** + * Un-select items from collection. + * + * @param array $keys + * @return static + */ + public function unselect(array $keys) + { + return $this->select(array_diff($this->getKeys(), $keys)); + } + /** * Implementes JsonSerializable interface. * diff --git a/system/src/Grav/Framework/Collection/CollectionInterface.php b/system/src/Grav/Framework/Collection/CollectionInterface.php index 12bb6e9af..1a38f21ca 100644 --- a/system/src/Grav/Framework/Collection/CollectionInterface.php +++ b/system/src/Grav/Framework/Collection/CollectionInterface.php @@ -39,4 +39,22 @@ interface CollectionInterface extends Collection, \JsonSerializable * @return array */ public function chunk($size); + + /** + * Select items from collection. + * + * Collection is returned in the order of $keys given to the function. + * + * @param array $keys + * @return static + */ + public function select(array $keys); + + /** + * Un-select items from collection. + * + * @param array $keys + * @return static + */ + public function unselect(array $keys); } diff --git a/system/src/Grav/Framework/Flex/FlexCollection.php b/system/src/Grav/Framework/Flex/FlexCollection.php index cc71d64b4..f37152a70 100644 --- a/system/src/Grav/Framework/Flex/FlexCollection.php +++ b/system/src/Grav/Framework/Flex/FlexCollection.php @@ -223,7 +223,7 @@ class FlexCollection extends ObjectCollection implements FlexCollectionInterface /** * @param string $action * @param string|null $scope - * @return FlexCollection + * @return static */ public function authorize(string $action, string $scope = null) { @@ -236,7 +236,7 @@ class FlexCollection extends ObjectCollection implements FlexCollectionInterface /** * @param string $value * @param string $field - * @return object|null + * @return FlexObject|null */ public function find($value, $field = 'id') { @@ -249,60 +249,6 @@ class FlexCollection extends ObjectCollection implements FlexCollectionInterface return null; } - /** - * @param array $ordering - * @return FlexCollection - */ - public function orderBy(array $ordering) - { - $criteria = Criteria::create()->orderBy($ordering); - - return $this->matching($criteria); - } - - /** - * @param int $start - * @param int|null $limit - * @return FlexCollection - */ - public function limit($start, $limit = null) - { - return $this->createFrom($this->slice($start, $limit)); - } - - /** - * Select items from collection. - * - * Collection is returned in the order of $keys given to the function. - * - * @param array $keys - * @return FlexCollection - */ - public function select(array $keys) - { - $list = []; - foreach ($keys as $key) { - if ($this->containsKey($key)) { - $list[$key] = $this->get($key); - } - } - - return $this->createFrom($list); - } - - /** - * Un-select items from collection. - * - * Collection is returned in the order of $keys given to the function. - * - * @param array $keys - * @return FlexCollection - */ - public function unselect(array $keys) - { - return $this->select(array_diff($this->getKeys(), $keys)); - } - /** * @return array */ diff --git a/system/src/Grav/Framework/Object/Interfaces/ObjectCollectionInterface.php b/system/src/Grav/Framework/Object/Interfaces/ObjectCollectionInterface.php index d06b891f6..2587ef774 100644 --- a/system/src/Grav/Framework/Object/Interfaces/ObjectCollectionInterface.php +++ b/system/src/Grav/Framework/Object/Interfaces/ObjectCollectionInterface.php @@ -65,4 +65,17 @@ interface ObjectCollectionInterface extends CollectionInterface, Selectable, Obj * @return static[] */ public function collectionGroup($property); + + /** + * @param array $ordering + * @return ObjectCollectionInterface + */ + public function orderBy(array $ordering); + + /** + * @param int $start + * @param int|null $limit + * @return ObjectCollectionInterface + */ + public function limit($start, $limit = null); } diff --git a/system/src/Grav/Framework/Object/ObjectCollection.php b/system/src/Grav/Framework/Object/ObjectCollection.php index b09a106fe..f428a6a9e 100644 --- a/system/src/Grav/Framework/Object/ObjectCollection.php +++ b/system/src/Grav/Framework/Object/ObjectCollection.php @@ -39,6 +39,27 @@ class ObjectCollection extends ArrayCollection implements ObjectCollectionInterf $this->setKey($key); } + /** + * @param array $ordering + * @return static + */ + public function orderBy(array $ordering) + { + $criteria = Criteria::create()->orderBy($ordering); + + return $this->matching($criteria); + } + + /** + * @param int $start + * @param int|null $limit + * @return static + */ + public function limit($start, $limit = null) + { + return $this->createFrom($this->slice($start, $limit)); + } + /** * {@inheritDoc} */ diff --git a/system/src/Grav/Framework/Object/ObjectIndex.php b/system/src/Grav/Framework/Object/ObjectIndex.php index a2f6bc340..62d3e9ded 100644 --- a/system/src/Grav/Framework/Object/ObjectIndex.php +++ b/system/src/Grav/Framework/Object/ObjectIndex.php @@ -189,6 +189,25 @@ abstract class ObjectIndex extends AbstractIndexCollection implements ObjectColl return $this->getKeys(); } + /** + * @param array $ordering + * @return ObjectCollectionInterface + */ + public function orderBy(array $ordering) + { + return $this->__call('orderBy', [$ordering]); + } + + /** + * @param int $start + * @param int|null $limit + * @return static + */ + public function limit($start, $limit = null) + { + return $this->createFrom($this->slice($start, $limit)); + } + /** * {@inheritDoc} */