diff --git a/src/Interfaces/WritableCollection.php b/src/Interfaces/WritableCollection.php index 327a214..f0e187f 100644 --- a/src/Interfaces/WritableCollection.php +++ b/src/Interfaces/WritableCollection.php @@ -47,4 +47,9 @@ public function take($key, $default = null); * @param mixed $key The key to remove */ public function remove($key); + + /** + * Clears the collection + */ + public function clear(); } diff --git a/src/Traits/WritableCollectionTrait.php b/src/Traits/WritableCollectionTrait.php index e9d51fd..271576b 100644 --- a/src/Traits/WritableCollectionTrait.php +++ b/src/Traits/WritableCollectionTrait.php @@ -92,4 +92,11 @@ public function take($key, $default = null) { public function remove($key) { unset($this->collection[$key]); } + + /** + * {@inheritdoc} + */ + public function clear() { + $this->collection = []; + } } diff --git a/tests/WritableCollectionTraitTest.php b/tests/WritableCollectionTraitTest.php index 39cf623..84f20b6 100644 --- a/tests/WritableCollectionTraitTest.php +++ b/tests/WritableCollectionTraitTest.php @@ -67,6 +67,14 @@ public function testRemove() { $this->assertSame(['key2' => 'val2'], $this->getCollection()); } + public function testClear() { + $this->setCollection(['key1' => 'val1', 'key2' => 'val2']); + + $this->trait->clear(); + + $this->assertEmpty($this->getCollection()); + } + private function getCollection() { return $this->collection->getValue($this->trait); }