Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding only_children() method to collections #310

Merged
merged 4 commits into from
Aug 9, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 $keys
* @return static
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

spacing and label for $keys

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