From b676dcdc5285565f6ad7599462d2f08e31b1fa64 Mon Sep 17 00:00:00 2001 From: Oluwatobi Omisakin Date: Tue, 30 Apr 2024 00:20:31 +0300 Subject: [PATCH 1/7] feat: Set minimum php to php 7.4 --- composer.json | 5 +++-- src/Arrayed.php | 12 +++++++++--- src/Traits/ArrayPrefix.php | 2 ++ tests/ArrayPrefixTraitTest.php | 2 ++ tests/ArrayedTest.php | 6 ++++-- 5 files changed, 20 insertions(+), 7 deletions(-) diff --git a/composer.json b/composer.json index d2e24da..916c702 100644 --- a/composer.json +++ b/composer.json @@ -22,13 +22,14 @@ } ], "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": "*" }, "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..d22eacf 100644 --- a/src/Traits/ArrayPrefix.php +++ b/src/Traits/ArrayPrefix.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))); } From cf20a5c80e9880c2d136f58bddf07f5f8de16f62 Mon Sep 17 00:00:00 2001 From: Oluwatobi Omisakin Date: Tue, 30 Apr 2024 00:33:04 +0300 Subject: [PATCH 2/7] feat: Declare pop method block --- composer.json | 3 ++- src/Traits/ArrayPrefix.php | 1 + tests/ArrayPrefixTraitTest.php | 14 ++++++++++++++ 3 files changed, 17 insertions(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 916c702..fda227f 100644 --- a/composer.json +++ b/composer.json @@ -29,7 +29,8 @@ "phpunit/phpunit": ">=8.0", "squizlabs/php_codesniffer": "3.*", "illuminate/support": ">=5.5", - "ext-json": "*" + "ext-json": "*", + "symfony/var-dumper": "^6.4" }, "autoload": { "psr-4": { diff --git a/src/Traits/ArrayPrefix.php b/src/Traits/ArrayPrefix.php index d22eacf..f3cae8e 100644 --- a/src/Traits/ArrayPrefix.php +++ b/src/Traits/ArrayPrefix.php @@ -13,6 +13,7 @@ * * @method self combine(array $values) * @method mixed shift() + * @method mixed pop() * @method self slice(int $offset, int $length = null, bool $preserve_keys = false) */ trait ArrayPrefix diff --git a/tests/ArrayPrefixTraitTest.php b/tests/ArrayPrefixTraitTest.php index 304b8e9..5ec6b96 100644 --- a/tests/ArrayPrefixTraitTest.php +++ b/tests/ArrayPrefixTraitTest.php @@ -161,5 +161,19 @@ public function testUnImplementedArrayPrefixFunction() array_combine($keys, $values), arrayed($keys)->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(), + ); } } From a9e4d95ba80dc68b42657c47084dd74df47b0432 Mon Sep 17 00:00:00 2001 From: Oluwatobi Omisakin Date: Tue, 30 Apr 2024 00:39:36 +0300 Subject: [PATCH 3/7] feat: Implement head method --- src/Traits/ArrayPrefix.php | 5 +++++ tests/ArrayPrefixTraitTest.php | 17 +++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/src/Traits/ArrayPrefix.php b/src/Traits/ArrayPrefix.php index f3cae8e..bbd6977 100644 --- a/src/Traits/ArrayPrefix.php +++ b/src/Traits/ArrayPrefix.php @@ -80,6 +80,11 @@ public function keysExists(array $needles, bool $all = true): bool : (!$intersect->empty()); } + public function head(bool $preserveKeys = true): ArrayedInterface + { + return $this->slice(0, 1, $preserveKeys); + } + /** * Forward the calls to `array_*` that is not yet implemented *
diff --git a/tests/ArrayPrefixTraitTest.php b/tests/ArrayPrefixTraitTest.php index 5ec6b96..d209fab 100644 --- a/tests/ArrayPrefixTraitTest.php +++ b/tests/ArrayPrefixTraitTest.php @@ -176,4 +176,21 @@ public function testUnImplementedArrayPrefixFunction() $arr = arrayed($data)->shift(), ); } + + public function testHead(): void + { + $data = ['a', 'b', 'c', 'd']; + + $this->assertEquals( + ['a'], + arrayed($data)->head()->result(), + ); + + $data = ['a' => 'b', 'c' => 'd']; + + $this->assertEquals( + ['a' => 'b'], + arrayed($data)->head()->result(), + ); + } } From 740ffd394cd8cb7dc8603e4512f76a41ca783906 Mon Sep 17 00:00:00 2001 From: Oluwatobi Omisakin Date: Tue, 30 Apr 2024 00:41:46 +0300 Subject: [PATCH 4/7] feat: Implement head method --- src/Traits/ArrayPrefix.php | 2 +- tests/ArrayPrefixTraitTest.php | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Traits/ArrayPrefix.php b/src/Traits/ArrayPrefix.php index bbd6977..271db29 100644 --- a/src/Traits/ArrayPrefix.php +++ b/src/Traits/ArrayPrefix.php @@ -80,7 +80,7 @@ public function keysExists(array $needles, bool $all = true): bool : (!$intersect->empty()); } - public function head(bool $preserveKeys = true): ArrayedInterface + public function head(bool $preserveKeys = false): ArrayedInterface { return $this->slice(0, 1, $preserveKeys); } diff --git a/tests/ArrayPrefixTraitTest.php b/tests/ArrayPrefixTraitTest.php index d209fab..b0cf812 100644 --- a/tests/ArrayPrefixTraitTest.php +++ b/tests/ArrayPrefixTraitTest.php @@ -181,14 +181,14 @@ public function testHead(): void { $data = ['a', 'b', 'c', 'd']; - $this->assertEquals( + $this->assertSame( ['a'], arrayed($data)->head()->result(), ); $data = ['a' => 'b', 'c' => 'd']; - $this->assertEquals( + $this->assertSame( ['a' => 'b'], arrayed($data)->head()->result(), ); From 1e96128c42008a020cce3e3baa3272680694333c Mon Sep 17 00:00:00 2001 From: Oluwatobi Omisakin Date: Tue, 30 Apr 2024 00:43:56 +0300 Subject: [PATCH 5/7] feat: Implement tail method --- src/Traits/ArrayPrefix.php | 5 +++++ tests/ArrayPrefixTraitTest.php | 17 +++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/src/Traits/ArrayPrefix.php b/src/Traits/ArrayPrefix.php index 271db29..88b6de0 100644 --- a/src/Traits/ArrayPrefix.php +++ b/src/Traits/ArrayPrefix.php @@ -85,6 +85,11 @@ public function head(bool $preserveKeys = false): ArrayedInterface return $this->slice(0, 1, $preserveKeys); } + public function tail(): ArrayedInterface + { + return $this->slice(1); + } + /** * Forward the calls to `array_*` that is not yet implemented *
diff --git a/tests/ArrayPrefixTraitTest.php b/tests/ArrayPrefixTraitTest.php index b0cf812..39e2770 100644 --- a/tests/ArrayPrefixTraitTest.php +++ b/tests/ArrayPrefixTraitTest.php @@ -193,4 +193,21 @@ public function testHead(): void arrayed($data)->head()->result(), ); } + + 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(), + ); + } } From e53c7654128496f5ff0a99810990106b506731a8 Mon Sep 17 00:00:00 2001 From: Oluwatobi Omisakin Date: Tue, 30 Apr 2024 01:19:32 +0300 Subject: [PATCH 6/7] feat: Fix head to return one element --- src/Traits/ArrayPrefix.php | 27 +++++++++++++++++++++++++-- tests/ArrayPrefixTraitTest.php | 13 +++++++++---- 2 files changed, 34 insertions(+), 6 deletions(-) diff --git a/src/Traits/ArrayPrefix.php b/src/Traits/ArrayPrefix.php index 88b6de0..a1f372d 100644 --- a/src/Traits/ArrayPrefix.php +++ b/src/Traits/ArrayPrefix.php @@ -6,6 +6,7 @@ use Transprime\Arrayed\Exceptions\ArrayedException; use Transprime\Arrayed\Interfaces\ArrayedInterface; +use Transprime\Arrayed\Types\Undefined; /** * Trait ArrayPrefix @@ -80,9 +81,18 @@ public function keysExists(array $needles, bool $all = true): bool : (!$intersect->empty()); } - public function head(bool $preserveKeys = false): ArrayedInterface + /** + * @throws ArrayedException + * @return ArrayedInterface|mixed + */ + public function head(bool $preserveKeys = false) { - return $this->slice(0, 1, $preserveKeys); + return self::makeArrayed( + $this->when($this->getWorkableItem()) + ->slice(0, 1, $preserveKeys) + ->values() + ->offsetGet(0) + ); } public function tail(): ArrayedInterface @@ -90,6 +100,19 @@ public function tail(): ArrayedInterface return $this->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 39e2770..91cd397 100644 --- a/tests/ArrayPrefixTraitTest.php +++ b/tests/ArrayPrefixTraitTest.php @@ -182,16 +182,21 @@ public function testHead(): void $data = ['a', 'b', 'c', 'd']; $this->assertSame( - ['a'], - arrayed($data)->head()->result(), + 'a', + arrayed($data)->head(), ); $data = ['a' => 'b', 'c' => 'd']; $this->assertSame( - ['a' => 'b'], - arrayed($data)->head()->result(), + 'b', + arrayed($data)->head(), ); + + // Test empty. + $this->expectException(\InvalidArgumentException::class); + + arrayed([])->head(); } public function testTail(): void From c8433ce0449cadfad736c4fbdebb5b8e842c282e Mon Sep 17 00:00:00 2001 From: Oluwatobi Omisakin Date: Tue, 30 Apr 2024 01:21:49 +0300 Subject: [PATCH 7/7] feat: Ensure tail must have array values to work with --- src/Traits/ArrayPrefix.php | 3 ++- tests/ArrayPrefixTraitTest.php | 5 +++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/src/Traits/ArrayPrefix.php b/src/Traits/ArrayPrefix.php index a1f372d..0d9cb4f 100644 --- a/src/Traits/ArrayPrefix.php +++ b/src/Traits/ArrayPrefix.php @@ -97,7 +97,8 @@ public function head(bool $preserveKeys = false) public function tail(): ArrayedInterface { - return $this->slice(1); + return $this->when($this->getWorkableItem()) + ->slice(1); } private function when($truthyValue, $default = Undefined::class) diff --git a/tests/ArrayPrefixTraitTest.php b/tests/ArrayPrefixTraitTest.php index 91cd397..bb2f701 100644 --- a/tests/ArrayPrefixTraitTest.php +++ b/tests/ArrayPrefixTraitTest.php @@ -214,5 +214,10 @@ public function testTail(): void ['c' => 'd'], arrayed($data)->tail()->result(), ); + + // Test empty. + $this->expectException(\InvalidArgumentException::class); + + arrayed([])->tail(); } }