Skip to content

Commit e9c904e

Browse files
committed
Splitsed with() into separate methods to add an item with or without a specific key.
1 parent df2856e commit e9c904e

File tree

3 files changed

+45
-27
lines changed

3 files changed

+45
-27
lines changed

src/AbstractCollection.php

+12-6
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,24 @@ public function __construct()
2020
/**
2121
* @inheritdoc
2222
*/
23-
public function with($item, $key = null)
23+
public function with($item)
2424
{
2525
$this->guardObjectType($item);
2626

2727
$copy = clone $this;
28+
$copy->items[] = $item;
29+
return $copy;
30+
}
2831

29-
if (is_null($key)) {
30-
$copy->items[] = $item;
31-
} else {
32-
$copy->items[$key] = $item;
33-
}
32+
/**
33+
* @inheritdoc
34+
*/
35+
public function withKey($key, $item)
36+
{
37+
$this->guardObjectType($item);
3438

39+
$copy = clone $this;
40+
$copy->items[$key] = $item;
3541
return $copy;
3642
}
3743

src/CollectionInterface.php

+14-2
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,29 @@ interface CollectionInterface extends \IteratorAggregate
1010
/**
1111
* @param mixed $item
1212
* Item to add to the Collection.
13-
* @param string|null $key
13+
*
14+
* @return static
15+
* Copy of the Collection, with the new item.
16+
*
17+
* @throws \InvalidArgumentException
18+
* When the provided item is of an incorrect type.
19+
*/
20+
public function with($item);
21+
22+
/**
23+
* @param string $key
1424
* Key to use for the new item. If not provided, the item will be added
1525
* to the end of the Collection.
26+
* @param mixed $item
27+
* Item to add to the Collection.
1628
*
1729
* @return static
1830
* Copy of the Collection, with the new item.
1931
*
2032
* @throws \InvalidArgumentException
2133
* When the provided item is of an incorrect type.
2234
*/
23-
public function with($item, $key = null);
35+
public function withKey($key, $item);
2436

2537
/**
2638
* @param mixed $item

tests/CollectionTest.php

+19-19
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ public function setUp()
4646
public function it_can_create_a_copy_with_a_new_item_for_a_specific_key()
4747
{
4848
$collection = (new FooCollection())
49-
->with($this->foo1, 'foo1')
50-
->with($this->foo2, 'foo2');
49+
->withKey('foo1', $this->foo1)
50+
->withKey('foo2', $this->foo2);
5151

5252
$expected = [
5353
'foo1' => $this->foo1,
@@ -110,7 +110,7 @@ public function it_guards_the_object_type_of_items_when_initializing_from_array(
110110
public function it_allows_sub_classes_when_guarding_the_object_type_of_new_items()
111111
{
112112
$collection = (new FooCollection())
113-
->with($this->fooExtended, 'fooExtended');
113+
->withKey('fooExtended', $this->fooExtended);
114114

115115
$expected = [
116116
'fooExtended' => $this->fooExtended,
@@ -143,7 +143,7 @@ public function it_can_remove_an_item()
143143
public function it_throws_an_exception_when_removing_an_unknown_item()
144144
{
145145
$collection = (new FooCollection())
146-
->with($this->foo1, 'foo1');
146+
->withKey('foo1', $this->foo1);
147147

148148
$this->setExpectedException(CollectionItemNotFoundException::class);
149149
$collection->without($this->foo2);
@@ -155,8 +155,8 @@ public function it_throws_an_exception_when_removing_an_unknown_item()
155155
public function it_can_remove_an_item_by_key()
156156
{
157157
$collection = (new FooCollection())
158-
->with($this->foo1, 'foo1')
159-
->with($this->foo2, 'foo2');
158+
->withKey('foo1', $this->foo1)
159+
->withKey('foo2', $this->foo2);
160160

161161
$collection = $collection->withoutKey('foo1');
162162

@@ -173,8 +173,8 @@ public function it_can_remove_an_item_by_key()
173173
public function it_throws_an_exception_when_removing_by_unknown_key()
174174
{
175175
$collection = (new FooCollection())
176-
->with($this->foo1, 'foo1')
177-
->with($this->foo2, 'foo2');
176+
->withKey('foo1', $this->foo1)
177+
->withKey('foo2', $this->foo2);
178178

179179
$this->setExpectedException(CollectionKeyNotFoundException::class);
180180
$collection->withoutKey('foo3');
@@ -186,8 +186,8 @@ public function it_throws_an_exception_when_removing_by_unknown_key()
186186
public function it_can_return_an_item_by_key()
187187
{
188188
$collection = (new FooCollection())
189-
->with($this->foo1, 'foo1')
190-
->with($this->foo2, 'foo2');
189+
->withKey('foo1', $this->foo1)
190+
->withKey('foo2', $this->foo2);
191191

192192
$this->assertEquals(
193193
$this->foo1,
@@ -206,7 +206,7 @@ public function it_can_return_an_item_by_key()
206206
public function it_throws_an_exception_when_looking_for_an_item_with_an_unknown_key()
207207
{
208208
$collection = (new FooCollection())
209-
->with($this->foo1, 'foo1');
209+
->withKey('foo1', $this->foo1);
210210

211211
$this->setExpectedException(CollectionKeyNotFoundException::class);
212212
$collection->getByKey('foo2');
@@ -218,8 +218,8 @@ public function it_throws_an_exception_when_looking_for_an_item_with_an_unknown_
218218
public function it_can_return_the_key_for_an_item()
219219
{
220220
$collection = (new FooCollection())
221-
->with($this->foo1, 'foo1')
222-
->with($this->foo2, 'foo2');
221+
->withKey('foo1', $this->foo1)
222+
->withKey('foo2', $this->foo2);
223223

224224
$this->assertEquals(
225225
'foo1',
@@ -238,7 +238,7 @@ public function it_can_return_the_key_for_an_item()
238238
public function it_throws_an_exception_when_looking_for_the_key_of_an_unknown_item()
239239
{
240240
$collection = (new FooCollection())
241-
->with($this->foo1, 'foo1');
241+
->withKey('foo1', $this->foo1);
242242

243243
$this->setExpectedException(CollectionItemNotFoundException::class);
244244
$collection->getKeyFor($this->foo2);
@@ -250,8 +250,8 @@ public function it_throws_an_exception_when_looking_for_the_key_of_an_unknown_it
250250
public function it_can_return_a_list_of_keys()
251251
{
252252
$collection = (new FooCollection())
253-
->with($this->foo1, 'foo1')
254-
->with($this->foo2, 'foo2');
253+
->withKey('foo1', $this->foo1)
254+
->withKey('foo2', $this->foo2);
255255

256256
$expected = [
257257
'foo1',
@@ -297,9 +297,9 @@ public function it_can_be_converted_from_and_to_an_array()
297297
public function it_can_be_looped_over_like_an_array()
298298
{
299299
$collection = (new FooCollection())
300-
->with($this->foo1, 'foo1')
301-
->with($this->foo2, 'foo2')
302-
->with($this->fooExtended, 'fooExtended');
300+
->withKey('foo1', $this->foo1)
301+
->withKey('foo2', $this->foo2)
302+
->withKey('fooExtended', $this->fooExtended);
303303

304304
$expected = [
305305
'foo1' => $this->foo1,

0 commit comments

Comments
 (0)