diff --git a/composer.json b/composer.json index d2e24da..fda227f 100644 --- a/composer.json +++ b/composer.json @@ -22,13 +22,15 @@ } ], "require": { - "php": ">=7.2", + "php": ">=7.4", "transprime-research/piper": "^2.0" }, "require-dev": { "phpunit/phpunit": ">=8.0", "squizlabs/php_codesniffer": "3.*", - "illuminate/support": ">=5.5" + "illuminate/support": ">=5.5", + "ext-json": "*", + "symfony/var-dumper": "^6.4" }, "autoload": { "psr-4": { diff --git a/src/Arrayed.php b/src/Arrayed.php index c93ef85..6a2001d 100644 --- a/src/Arrayed.php +++ b/src/Arrayed.php @@ -1,5 +1,7 @@ setResult(new Undefined()); } - private function argumentsToArray(...$values) + private function argumentsToArray(...$values): array { if (func_num_args() === 1 && is_array($values[0])) { return $values[0]; @@ -186,7 +188,11 @@ private function getWorkableItem(bool $asArray = false) return $this->raw; } - return ($asArray && !is_array($this->result)) ? (array)$this->result : $this->result; + if ($asArray && !is_array($this->result)) { + return (array)$this->result; + } + + return $this->result; } private static function makeArrayed($data) diff --git a/src/Traits/ArrayPrefix.php b/src/Traits/ArrayPrefix.php index b3c6967..0d9cb4f 100644 --- a/src/Traits/ArrayPrefix.php +++ b/src/Traits/ArrayPrefix.php @@ -1,9 +1,12 @@ empty()); } + /** + * @throws ArrayedException + * @return ArrayedInterface|mixed + */ + public function head(bool $preserveKeys = false) + { + return self::makeArrayed( + $this->when($this->getWorkableItem()) + ->slice(0, 1, $preserveKeys) + ->values() + ->offsetGet(0) + ); + } + + public function tail(): ArrayedInterface + { + return $this->when($this->getWorkableItem()) + ->slice(1); + } + + private function when($truthyValue, $default = Undefined::class) + { + if ($truthyValue) { + return $this; + } + + if ($default === Undefined::class || $default instanceof Undefined) { + throw new \InvalidArgumentException('Value cannot be resolved'); + } + + return $this->setResult($default); + } + /** * Forward the calls to `array_*` that is not yet implemented *
diff --git a/tests/ArrayPrefixTraitTest.php b/tests/ArrayPrefixTraitTest.php index 1451c35..bb2f701 100644 --- a/tests/ArrayPrefixTraitTest.php +++ b/tests/ArrayPrefixTraitTest.php @@ -1,5 +1,7 @@ combine($values)->result() ); + + // array_pop + $data = ['a' => 'b', 'c' => 'd']; + + $this->assertEquals( + 'd', + arrayed($data)->pop(), + ); + + // array_shift. + $this->assertEquals( + 'b', + $arr = arrayed($data)->shift(), + ); + } + + public function testHead(): void + { + $data = ['a', 'b', 'c', 'd']; + + $this->assertSame( + 'a', + arrayed($data)->head(), + ); + + $data = ['a' => 'b', 'c' => 'd']; + + $this->assertSame( + 'b', + arrayed($data)->head(), + ); + + // Test empty. + $this->expectException(\InvalidArgumentException::class); + + arrayed([])->head(); + } + + public function testTail(): void + { + $data = ['a', 'b', 'c', 'd']; + + $this->assertSame( + ['b', 'c', 'd'], + arrayed($data)->tail()->result(), + ); + + $data = ['a' => 'b', 'c' => 'd']; + + $this->assertSame( + ['c' => 'd'], + arrayed($data)->tail()->result(), + ); + + // Test empty. + $this->expectException(\InvalidArgumentException::class); + + arrayed([])->tail(); } } diff --git a/tests/ArrayedTest.php b/tests/ArrayedTest.php index 2840523..0b39132 100644 --- a/tests/ArrayedTest.php +++ b/tests/ArrayedTest.php @@ -1,5 +1,7 @@ 1, 'b' => 2]; - $this->assertJson(arrayed($data)); - $this->assertStringContainsString(arrayed($data), json_encode($data)); + $this->assertJson((string) arrayed($data)); + $this->assertStringContainsString((string) arrayed($data), json_encode($data)); $this->assertEquals(json_encode($data), arrayed($data)); $this->assertSame(json_encode($data), json_encode(arrayed($data))); }