diff --git a/src/Collection/Set.php b/src/Collection/Set.php index e2d14d0..6c2d736 100644 --- a/src/Collection/Set.php +++ b/src/Collection/Set.php @@ -88,6 +88,23 @@ public function add($element): self return self::fromPointer($elements); } + /** + * @param Set $elements + * + * @return Set + */ + public function addAll(Set $elements): self + { + $new = $this->elements; + foreach ($elements->elements as $current) { + if (!$this->contains($current)) { + $new[] = $current; + } + } + + return self::fromPointer($new); + } + /** * @param T $element * diff --git a/tests/Collection/SetTest.php b/tests/Collection/SetTest.php index 0dd5290..e12ab31 100644 --- a/tests/Collection/SetTest.php +++ b/tests/Collection/SetTest.php @@ -275,4 +275,12 @@ public function testSetDisjoint() self::assertFalse(Set::of('a', 'b', 'c')->disjoint(Set::of('d', 'b', 'f'))); self::assertFalse(Set::of('a', 'b', 'c')->disjoint(Set::of('d', 'e', 'c'))); } + + public function testSetAddAll() + { + self::assertTrue(Set::of('a', 'b', 'c')->equals(Set::of('a')->addAll(Set::of('b', 'c')))); + self::assertTrue(Set::of('a', 'b', 'c')->equals(Set::empty()->addAll(Set::of('a', 'b', 'c')))); + self::assertTrue(Set::empty()->equals(Set::empty()->addAll(Set::empty()))); + self::assertTrue(Set::of('a', 'b', 'c')->equals(Set::of('a')->addAll(Set::of('a', 'b', 'c')))); + } }