Skip to content

Commit

Permalink
Add union, intersection and difference to AbstractCollection
Browse files Browse the repository at this point in the history
  • Loading branch information
giuscris committed Sep 21, 2024
1 parent 1acd27a commit 352a464
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions formwork/src/Data/AbstractCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,48 @@ public function everyItem(): CollectionDataProxy
return new CollectionDataProxy($this);
}

/**
* Return a copy of the collection with the union of the values of the given collections
*/
public function union(self ...$collections): static
{
$collection = $this->clone();

foreach ($collections as $otherCollection) {
$collection->data += $otherCollection->data;
}

return $collection;
}

/**
* Return a copy of the collection with the intersection of the values of the given collections
*/
public function intersection(self ...$collections): static
{
$collection = $this->clone();

foreach ($collections as $otherCollection) {
$collection->data = array_intersect($collection->data, $otherCollection->data);
}

return $collection;
}

/**
* Return a copy of the collection with the values of the given collections removed
*/
public function difference(self ...$collections): static
{
$collection = $this->clone();

foreach ($collections as $otherCollection) {
$collection->data = array_diff($collection->data, $otherCollection->data);
}

return $collection;
}

/**
* Add the given value to the collection
*/
Expand Down

0 comments on commit 352a464

Please sign in to comment.