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:
@@ -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.
|
||||
*
|
||||
|
@@ -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':
|
||||
|
Reference in New Issue
Block a user