From c8382fab8da3bb6d89e37d0a9bf1597acf9c35a2 Mon Sep 17 00:00:00 2001 From: Rahul Kadyan Date: Sat, 10 Oct 2015 23:08:09 +0530 Subject: [PATCH] Merge pull request #24 from znck:master Fixes #21: Immutable array could be modified with some methods --- src/ImmutableArray.php | 58 ++++++++++++++++++++++++++++++++++++ tests/ImmutableArrayTest.php | 10 ++++--- 2 files changed, 64 insertions(+), 4 deletions(-) diff --git a/src/ImmutableArray.php b/src/ImmutableArray.php index 2c51baa..9464fae 100644 --- a/src/ImmutableArray.php +++ b/src/ImmutableArray.php @@ -127,6 +127,35 @@ public function mergeTo(array $array, $recursively = false) return new static(array_merge($array, $this->elements)); } + /** + * Push one or more values onto the end of array at once. + * + * @param mixed $element The pushed element + * @param mixed $_ [optional] Multiple arguments allowed + * + * @return static The new instance with pushed elements to the end of array + */ + public function push($element, $_ = null) + { + $elements = $this->elements; + if (func_num_args()) { + $args = array_merge([&$elements], func_get_args()); + call_user_func_array('array_push', $args); + } + + return new static($elements); + } + + /** + * Pop a specified value off the end of array. + * + * @return mixed The popped element + */ + public function pop() + { + return $this->count() ? $this->last() : null; + } + /** * Replace array with given one * @@ -197,6 +226,35 @@ public function diffWith(array $array) return new static(array_diff($this->elements, $array)); } + /** + * Unshift array + * + * @param mixed $element The element for prepend + * @param mixed $_ [optional] Multiple arguments allowed + * + * @return static The new instance with prepended elements to the beginning of array + */ + public function unshift($element, $_ = null) + { + $elements = $this->elements; + if (func_num_args()) { + $args = array_merge([&$elements], func_get_args()); + call_user_func_array('array_unshift', $args); + } + + return new static($elements); + } + + /** + * Shifts a specified value off the beginning of array. + * + * @return mixed The shifted element + */ + public function shift() + { + return $this->count() ? $this->first() : null; + } + /** * Shuffle array * diff --git a/tests/ImmutableArrayTest.php b/tests/ImmutableArrayTest.php index 40b5ba6..09f385b 100644 --- a/tests/ImmutableArrayTest.php +++ b/tests/ImmutableArrayTest.php @@ -557,10 +557,11 @@ public function testReduce(array $array) public function testShift(array $array) { $ma = new ImmutableArray($array); + $copyMa = new ImmutableArray($array); $shiftedValue = $ma->shift(); $shiftedArrayValue = array_shift($array); - $this->assertTrue($array === $ma->toArray()); + $this->assertTrue($copyMa->toArray() === $ma->toArray()); $this->assertTrue($shiftedArrayValue === $shiftedValue); } @@ -575,7 +576,7 @@ public function testUnshift(array $array) $copiedMa = $ma->unshift($newElement1, $newElement2); array_unshift($array, $newElement1, $newElement2); - $this->assertTrue($copiedMa === $ma); + $this->assertTrue($copiedMa->toArray() !== $ma->toArray()); $this->assertTrue($array === $copiedMa->toArray()); } @@ -585,10 +586,11 @@ public function testUnshift(array $array) public function testPop(array $array) { $ma = new ImmutableArray($array); + $copyMa = new ImmutableArray($array); $poppedValue = $ma->pop(); $poppedArrayValue = array_pop($array); - $this->assertTrue($array === $ma->toArray()); + $this->assertTrue($copyMa->toArray() === $ma->toArray()); $this->assertTrue($poppedArrayValue === $poppedValue); } @@ -603,7 +605,7 @@ public function testPush(array $array) $copiedMa = $ma->push($newElement1, $newElement2); array_push($array, $newElement1, $newElement2); - $this->assertTrue($copiedMa === $ma); + $this->assertTrue($copiedMa->toArray() !== $ma->toArray()); $this->assertTrue($array === $copiedMa->toArray()); }