Skip to content

Commit 3d3869f

Browse files
committed
Adding the clear method to container classes
1 parent 6cf26b9 commit 3d3869f

File tree

9 files changed

+37
-8
lines changed

9 files changed

+37
-8
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ At any given time it is possible with each of these objects to:
148148
- tell whether the container is empty via an `isEmpty` method;
149149
- know the number of elements contained in the container via the `Countable` interface;
150150
- merge multiple instance of **the same type** using the `merge` method;
151+
- clear the container using the `clear` method;
151152

152153
```php
153154
use Bakame\Http\StructuredFields\Parameters;

src/Dictionary.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,11 @@ public function delete(string ...$keys): void
225225
}
226226
}
227227

228+
public function clear(): void
229+
{
230+
$this->elements = [];
231+
}
232+
228233
public function append(string $key, InnerList|Item|ByteSequence|Token|bool|int|float|string $element): void
229234
{
230235
self::validateKey($key);

src/DictionaryTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ public function it_can_be_instantiated_with_an_collection_of_item_or_inner_list(
3030
);
3131

3232
self::assertEquals($arrayParams, iterator_to_array($instance, true));
33+
$instance->clear();
34+
self::assertTrue($instance->isEmpty());
3335
}
3436

3537
/**

src/InnerList.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,11 @@ public function remove(int ...$indexes): void
194194
$this->elements = array_values($this->elements);
195195
}
196196

197+
public function clear(): void
198+
{
199+
$this->elements = [];
200+
}
201+
197202
public function merge(self ...$others): void
198203
{
199204
foreach ($others as $other) {

src/InnerListTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ public function it_can_be_instantiated_with_an_collection_of_item(): void
2626
self::assertFalse($instance->isEmpty());
2727

2828
self::assertEquals($arrayParams, iterator_to_array($instance, true));
29+
$instance->clear();
30+
self::assertTrue($instance->isEmpty());
2931
}
3032

3133
/**

src/OrderedList.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,11 @@ public function remove(int ...$indexes): void
164164
$this->elements = array_values($this->elements);
165165
}
166166

167+
public function clear(): void
168+
{
169+
$this->elements = [];
170+
}
171+
167172
public function merge(self ...$others): void
168173
{
169174
foreach ($others as $other) {

src/OrderedListTest.php

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -67,15 +67,17 @@ public function it_can_add_or_remove_elements(): void
6767
*/
6868
public function it_can_unshift_insert_and_replace(): void
6969
{
70-
$container = OrderedList::fromElements();
71-
$container->unshift(Item::from('42'));
72-
$container->push(Item::from(42));
73-
$container->insert(1, Item::from(42.0));
74-
$container->replace(0, Item::from(ByteSequence::fromDecoded('Hello World')));
70+
$instance = OrderedList::fromElements();
71+
$instance->unshift(Item::from('42'));
72+
$instance->push(Item::from(42));
73+
$instance->insert(1, Item::from(42.0));
74+
$instance->replace(0, Item::from(ByteSequence::fromDecoded('Hello World')));
7575

76-
self::assertCount(3, $container);
77-
self::assertFalse($container->isEmpty());
78-
self::assertSame(':SGVsbG8gV29ybGQ=:, 42.0, 42', $container->toHttpValue());
76+
self::assertCount(3, $instance);
77+
self::assertFalse($instance->isEmpty());
78+
self::assertSame(':SGVsbG8gV29ybGQ=:, 42.0, 42', $instance->toHttpValue());
79+
$instance->clear();
80+
self::assertTrue($instance->isEmpty());
7981
}
8082

8183
/**

src/Parameters.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,11 @@ public function delete(string ...$keys): void
205205
}
206206
}
207207

208+
public function clear(): void
209+
{
210+
$this->elements = [];
211+
}
212+
208213
public function append(string $key, Item|ByteSequence|Token|bool|int|float|string $element): void
209214
{
210215
$element = self::filterElement($element);

src/ParametersTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ public function it_can_be_instantiated_with_an_collection_of_item(): void
3131
self::assertTrue($instance->has('string'));
3232

3333
self::assertEquals($arrayParams, iterator_to_array($instance, true));
34+
$instance->clear();
35+
self::assertTrue($instance->isEmpty());
3436
}
3537

3638
/**

0 commit comments

Comments
 (0)