Skip to content

Commit

Permalink
Merge pull request #310 from alleyinteractive/collections-only
Browse files Browse the repository at this point in the history
Adding only_children() method to collections
  • Loading branch information
srtfisher authored Aug 9, 2022
2 parents 2ebf70a + a0b69d3 commit 2fbd4fa
Show file tree
Hide file tree
Showing 3 changed files with 224 additions and 141 deletions.
22 changes: 22 additions & 0 deletions src/mantle/support/class-collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -789,6 +789,28 @@ public function only( $keys ) {
return new static( Arr::only( $this->items, $keys ) );
}

/**
* Get the items in an collection of arrays with filtered child keys.
*
* @param mixed[]|mixed $keys The keys to filter by.
* @return static
*/
public function only_children( $keys ) {
if ( is_null( $keys ) ) {
return new static( $this->items );
}

if ( $keys instanceof Enumerable ) {
$keys = $keys->all();
}

$keys = is_array( $keys ) ? $keys : func_get_args();

return $this->map(
fn ( $item ) => Arr::only( $item, $keys ),
);
}

/**
* Get and remove the last item from the collection.
*
Expand Down
18 changes: 9 additions & 9 deletions src/mantle/support/traits/trait-enumerates-values.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,22 +68,22 @@ trait Enumerates_Values {
'every',
'filter',
'first',
'flatMap',
'groupBy',
'keyBy',
'flat_map',
'group_by',
'key_by',
'map',
'max',
'min',
'partition',
'reject',
'skipUntil',
'skipWhile',
'skip_until',
'skip_while',
'some',
'sortBy',
'sortByDesc',
'sort_by',
'sort_by_desc',
'sum',
'takeUntil',
'takeWhile',
'take_until',
'take_while',
'unique',
'until',
];
Expand Down
Loading

0 comments on commit 2fbd4fa

Please sign in to comment.