Skip to content

Commit 8d4e507

Browse files
committed
Merge pull request bocharsky-bw#11 from bocharsky-bw/multiple-args
Add ability to push/unshift multiple values at once - thanks @pomaxa
2 parents 68c751d + ab0cb69 commit 8d4e507

File tree

4 files changed

+41
-25
lines changed

4 files changed

+41
-25
lines changed

README.md

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -611,11 +611,13 @@ $a->previous(); // 'b'
611611
### push
612612

613613
``` php
614-
$a = MutableArray::create(['a', 'b', 'c']);
615-
$a->push('d');
614+
$a = MutableArray::create(['a', 'b']);
615+
$a->push('c', 'd');
616616
$a->toArray(); // [0 => 'a', 1 => 'b', 2 => 'c', 3 => 'd']
617617
```
618618

619+
> Method `push()` allow multiple arguments.
620+
619621
### reduce
620622

621623
``` php
@@ -744,11 +746,13 @@ $a->toArray(); // [0 => 'a', 1 => 'b', 3 => 'c']
744746
### unshift
745747

746748
``` php
747-
$a = MutableArray::create(['b', 'c']);
748-
$a->unshift('a');
749-
$a->toArray(); // [0 => 'a', 1 => 'b', 2 => 'c']
749+
$a = MutableArray::create([('a', 'b']);
750+
$a->unshift('y', 'z');
751+
$a->toArray(); // [0 => 'y', 1 => 'z', 2 => 'a', 3 => 'b']
750752
```
751753

754+
> Method `unshift()` allow multiple arguments.
755+
752756
### walk
753757

754758
``` php

src/Interfaces/ModifiableInterface.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,14 @@ interface ModifiableInterface
1717
public function shift();
1818

1919
/**
20-
* Prepends a new value to the beginning of array.
20+
* Prepends one or more values to the beginning of array at once.
2121
*
2222
* @param mixed $element The element for prepend
23+
* @param mixed $_ [optional] Multiple arguments allowed
2324
*
24-
* @return $this The same instance with prepended element to the beginning of array
25+
* @return $this The same instance with prepended elements to the beginning of array
2526
*/
26-
public function unshift($element);
27+
public function unshift($element, $_ = null);
2728

2829
/**
2930
* Pop a specified value off the end of array.
@@ -33,11 +34,12 @@ public function unshift($element);
3334
public function pop();
3435

3536
/**
36-
* Push value onto the end of array.
37+
* Push one or more values onto the end of array at once.
3738
*
3839
* @param mixed $element The pushed element
40+
* @param mixed $_ [optional] Multiple arguments allowed
3941
*
40-
* @return $this The same instance with pushed element to the end of array
42+
* @return $this The same instance with pushed elements to the end of array
4143
*/
42-
public function push($element);
44+
public function push($element, $_ = null);
4345
}

src/Traits/ModifiableTrait.php

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,21 @@ public function shift()
2424
}
2525

2626
/**
27-
* Prepends a new value to the beginning of array.
27+
* Prepends one or more values to the beginning of array at once.
2828
*
2929
* @param mixed $element The element for prepend
30+
* @param mixed $_ [optional] Multiple arguments allowed
3031
*
31-
* @return $this The same instance with prepended element to the beginning of array
32+
* @return $this The same instance with prepended elements to the beginning of array
3233
*
3334
* @link http://php.net/manual/en/function.array-unshift.php
3435
*/
35-
public function unshift($element)
36+
public function unshift($element, $_ = null)
3637
{
37-
array_unshift($this->elements, $element);
38+
if (func_num_args()) {
39+
$args = array_merge([&$this->elements], func_get_args());
40+
call_user_func_array('array_unshift', $args);
41+
}
3842

3943
return $this;
4044
}
@@ -52,17 +56,21 @@ public function pop()
5256
}
5357

5458
/**
55-
* Push value onto the end of array.
59+
* Push one or more values onto the end of array at once.
5660
*
5761
* @param mixed $element The pushed element
62+
* @param mixed $_ [optional] Multiple arguments allowed
5863
*
59-
* @return $this The same instance with pushed element to the end of array
64+
* @return $this The same instance with pushed elements to the end of array
6065
*
6166
* @link http://php.net/manual/en/function.array-push.php
6267
*/
63-
public function push($element)
68+
public function push($element, $_ = null)
6469
{
65-
array_push($this->elements, $element);
70+
if (func_num_args()) {
71+
$args = array_merge([&$this->elements], func_get_args());
72+
call_user_func_array('array_push', $args);
73+
}
6674

6775
return $this;
6876
}

tests/ImmutableArrayTest.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -569,10 +569,11 @@ public function testShift(array $array)
569569
*/
570570
public function testUnshift(array $array)
571571
{
572-
$newElement = 5;
572+
$newElement1 = 5;
573+
$newElement2 = 10;
573574
$ma = new ImmutableArray($array);
574-
$copiedMa = $ma->unshift($newElement);
575-
array_unshift($array, $newElement);
575+
$copiedMa = $ma->unshift($newElement1, $newElement2);
576+
array_unshift($array, $newElement1, $newElement2);
576577

577578
$this->assertTrue($copiedMa === $ma);
578579
$this->assertTrue($array === $copiedMa->toArray());
@@ -596,10 +597,11 @@ public function testPop(array $array)
596597
*/
597598
public function testPush(array $array)
598599
{
599-
$newElement = 5;
600+
$newElement1 = 5;
601+
$newElement2 = 10;
600602
$ma = new ImmutableArray($array);
601-
$copiedMa = $ma->push($newElement);
602-
array_push($array, $newElement);
603+
$copiedMa = $ma->push($newElement1, $newElement2);
604+
array_push($array, $newElement1, $newElement2);
603605

604606
$this->assertTrue($copiedMa === $ma);
605607
$this->assertTrue($array === $copiedMa->toArray());

0 commit comments

Comments
 (0)