1
0
mirror of https://github.com/flextype/flextype.git synced 2025-08-08 22:26:46 +02:00

feat(collection): add only() method for Collection #455

This commit is contained in:
Awilum
2020-08-19 10:37:24 +03:00
parent 533ed511d1
commit 30b097d324
2 changed files with 25 additions and 0 deletions

View File

@@ -141,6 +141,26 @@ class Collection
return $this;
}
/**
* Get a subset of the items from the given collection.
*
* @param array $array
* @param array|string $keys
* @return static
*/
public function only($keys)
{
$result = [];
foreach ($this->collection->toArray() as $key => $value) {
$result[$key] = array_intersect_key($value, array_flip((array) $keys));
}
$this->collection = new ArrayCollection($result);
return $this;
}
/**
* Sets the where expression to evaluate when this Criteria is searched for.
*

View File

@@ -100,6 +100,11 @@ if (! function_exists('collect_filter')) {
$collection->orderBy($bind_order_by['order_by']['field'], $bind_order_by['order_by']['direction']);
}
// Exec: only
if (isset($filter['only'])) {
$collection->only($filter['only']);
}
// Gets a native PHP array representation of the collection.
switch ($bind_return) {
case 'first':