From 779739e7c4cadcdfc287aa997f8b6b4605de23b6 Mon Sep 17 00:00:00 2001 From: Corey Frenette Date: Thu, 18 Feb 2016 19:41:14 -0400 Subject: [PATCH 1/2] Clear method (closes #3) --- src/Interfaces/WritableCollection.php | 5 +++++ src/Traits/WritableCollectionTrait.php | 7 +++++++ 2 files changed, 12 insertions(+) diff --git a/src/Interfaces/WritableCollection.php b/src/Interfaces/WritableCollection.php index b10ac8b..1da4a85 100644 --- a/src/Interfaces/WritableCollection.php +++ b/src/Interfaces/WritableCollection.php @@ -36,4 +36,9 @@ public function lazy($key, callable $initializer); * @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 055dff5..b3e4e41 100644 --- a/src/Traits/WritableCollectionTrait.php +++ b/src/Traits/WritableCollectionTrait.php @@ -61,4 +61,11 @@ public function lazy($key, callable $initializer) { public function remove($key) { unset($this->collection[$key]); } + + /** + * {@inheritdoc} + */ + public function clear() { + $this->collection = []; + } } From 6f92981f9ea04ccc7902c2bc9dec0ce36d6f98ed Mon Sep 17 00:00:00 2001 From: Corey Frenette Date: Thu, 18 Feb 2016 21:46:53 -0400 Subject: [PATCH 2/2] Test for clear method --- tests/WritableCollectionTraitTest.php | 8 ++++++++ 1 file changed, 8 insertions(+) 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); }