Added select() and unselect() methods to CollectionInterface and its base classes

Added `orderBy()` and `limit()` methods to `ObjectCollectionInterface` and its base classes
This commit is contained in:
Matias Griese
2018-11-15 10:01:09 +02:00
parent 7975ec8d09
commit 02d48693dd
9 changed files with 136 additions and 56 deletions

View File

@@ -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)

View File

@@ -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.
*

View File

@@ -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}
*/

View File

@@ -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.
*

View File

@@ -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);
}

View File

@@ -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
*/

View File

@@ -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);
}

View File

@@ -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}
*/

View File

@@ -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}
*/