From 88f02fbbe01e80a3cef4727cfac1abbfe856745a Mon Sep 17 00:00:00 2001 From: Daniel Weaver Date: Thu, 23 May 2024 16:03:07 -0400 Subject: [PATCH 1/4] Add values and keys modifiers --- src/Modifiers/CoreModifiers.php | 20 ++++++++++++++++++++ tests/Modifiers/KeysTest.php | 30 ++++++++++++++++++++++++++++++ tests/Modifiers/ValuesTest.php | 30 ++++++++++++++++++++++++++++++ 3 files changed, 80 insertions(+) create mode 100644 tests/Modifiers/KeysTest.php create mode 100644 tests/Modifiers/ValuesTest.php diff --git a/src/Modifiers/CoreModifiers.php b/src/Modifiers/CoreModifiers.php index 36f9ff6bf0..aeeaf5ac62 100644 --- a/src/Modifiers/CoreModifiers.php +++ b/src/Modifiers/CoreModifiers.php @@ -784,6 +784,26 @@ public function get($value, $params) return $value; } + /** + * Get the keys of an array. + * + * @return array + */ + public function keys($value, $params) + { + return array_keys($value); + } + + /** + * Get the values of an array. + * + * @return array + */ + public function values($value, $params) + { + return array_values($value); + } + /** * Get a Gravatar image URL from an email. * diff --git a/tests/Modifiers/KeysTest.php b/tests/Modifiers/KeysTest.php new file mode 100644 index 0000000000..35460f23b3 --- /dev/null +++ b/tests/Modifiers/KeysTest.php @@ -0,0 +1,30 @@ + 'nuggets', + 'nuggets' => 'Denver', + ]; + + $expected = [ + 'chicken', + 'nuggets', + ]; + $modified = $this->modify($input); + $this->assertEquals($expected, $modified); + } + + private function modify(array $value) + { + return Modify::value($value)->keys()->fetch(); + } +} diff --git a/tests/Modifiers/ValuesTest.php b/tests/Modifiers/ValuesTest.php new file mode 100644 index 0000000000..bc3a5c23ba --- /dev/null +++ b/tests/Modifiers/ValuesTest.php @@ -0,0 +1,30 @@ + 'nuggets', + 'nuggets' => 'Denver', + ]; + + $expected = [ + 'nuggets', + 'Denver', + ]; + $modified = $this->modify($input); + $this->assertEquals($expected, $modified); + } + + private function modify(array $value) + { + return Modify::value($value)->values()->fetch(); + } +} From b3b12876e00302dd6cb8457740fc9c7c387dae1d Mon Sep 17 00:00:00 2001 From: Daniel Weaver Date: Thu, 23 May 2024 16:12:26 -0400 Subject: [PATCH 2/4] Code styling --- src/Modifiers/CoreModifiers.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Modifiers/CoreModifiers.php b/src/Modifiers/CoreModifiers.php index aeeaf5ac62..c4f69dcded 100644 --- a/src/Modifiers/CoreModifiers.php +++ b/src/Modifiers/CoreModifiers.php @@ -786,7 +786,7 @@ public function get($value, $params) /** * Get the keys of an array. - * + * * @return array */ public function keys($value, $params) @@ -796,7 +796,7 @@ public function keys($value, $params) /** * Get the values of an array. - * + * * @return array */ public function values($value, $params) From 916797a053722a41bd9ac503099d6d2e8e99bcc8 Mon Sep 17 00:00:00 2001 From: Jason Varga Date: Thu, 23 May 2024 17:47:57 -0400 Subject: [PATCH 3/4] support collections --- src/Modifiers/CoreModifiers.php | 12 ++++++------ tests/Modifiers/KeysTest.php | 22 ++++++++++++++++------ tests/Modifiers/ValuesTest.php | 22 ++++++++++++++++------ 3 files changed, 38 insertions(+), 18 deletions(-) diff --git a/src/Modifiers/CoreModifiers.php b/src/Modifiers/CoreModifiers.php index c4f69dcded..99de3df779 100644 --- a/src/Modifiers/CoreModifiers.php +++ b/src/Modifiers/CoreModifiers.php @@ -787,21 +787,21 @@ public function get($value, $params) /** * Get the keys of an array. * - * @return array + * @return array|Collection */ - public function keys($value, $params) + public function keys($value) { - return array_keys($value); + return is_array($value) ? array_keys($value) : $value->keys(); } /** * Get the values of an array. * - * @return array + * @return array|Collection */ - public function values($value, $params) + public function values($value) { - return array_values($value); + return is_array($value) ? array_values($value) : $value->values(); } /** diff --git a/tests/Modifiers/KeysTest.php b/tests/Modifiers/KeysTest.php index 35460f23b3..b4692c1ce3 100644 --- a/tests/Modifiers/KeysTest.php +++ b/tests/Modifiers/KeysTest.php @@ -2,6 +2,7 @@ namespace Tests\Modifiers; +use Illuminate\Support\Collection; use Statamic\Modifiers\Modify; use Tests\TestCase; @@ -15,15 +16,24 @@ public function it_gets_the_keys_of_an_array(): void 'nuggets' => 'Denver', ]; - $expected = [ - 'chicken', - 'nuggets', - ]; $modified = $this->modify($input); - $this->assertEquals($expected, $modified); + $this->assertEquals(['chicken', 'nuggets'], $modified); + } + + /** @test */ + public function it_gets_the_keys_of_a_collection(): void + { + $input = collect([ + 'chicken' => 'nuggets', + 'nuggets' => 'Denver', + ]); + + $modified = $this->modify($input); + $this->assertInstanceOf(Collection::class, $modified); + $this->assertEquals(['chicken', 'nuggets'], $modified->all()); } - private function modify(array $value) + private function modify($value) { return Modify::value($value)->keys()->fetch(); } diff --git a/tests/Modifiers/ValuesTest.php b/tests/Modifiers/ValuesTest.php index bc3a5c23ba..f6f1d7cd88 100644 --- a/tests/Modifiers/ValuesTest.php +++ b/tests/Modifiers/ValuesTest.php @@ -2,6 +2,7 @@ namespace Tests\Modifiers; +use Illuminate\Support\Collection; use Statamic\Modifiers\Modify; use Tests\TestCase; @@ -15,15 +16,24 @@ public function it_gets_the_values_of_an_array(): void 'nuggets' => 'Denver', ]; - $expected = [ - 'nuggets', - 'Denver', - ]; $modified = $this->modify($input); - $this->assertEquals($expected, $modified); + $this->assertEquals(['nuggets', 'Denver'], $modified); + } + + /** @test */ + public function it_gets_the_values_of_a_collection(): void + { + $input = collect([ + 'chicken' => 'nuggets', + 'nuggets' => 'Denver', + ]); + + $modified = $this->modify($input); + $this->assertInstanceOf(Collection::class, $modified); + $this->assertEquals(['nuggets', 'Denver'], $modified->all()); } - private function modify(array $value) + private function modify($value) { return Modify::value($value)->values()->fetch(); } From 2383572943864f689b826c1c212b7e674d0ef7a3 Mon Sep 17 00:00:00 2001 From: Jason Varga Date: Thu, 23 May 2024 17:49:28 -0400 Subject: [PATCH 4/4] position alphabetically (even though not everything else is) --- src/Modifiers/CoreModifiers.php | 40 ++++++++++++++++----------------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/src/Modifiers/CoreModifiers.php b/src/Modifiers/CoreModifiers.php index 99de3df779..86a73bb5e7 100644 --- a/src/Modifiers/CoreModifiers.php +++ b/src/Modifiers/CoreModifiers.php @@ -784,26 +784,6 @@ public function get($value, $params) return $value; } - /** - * Get the keys of an array. - * - * @return array|Collection - */ - public function keys($value) - { - return is_array($value) ? array_keys($value) : $value->keys(); - } - - /** - * Get the values of an array. - * - * @return array|Collection - */ - public function values($value) - { - return is_array($value) ? array_values($value) : $value->values(); - } - /** * Get a Gravatar image URL from an email. * @@ -1348,6 +1328,16 @@ public function keyBy($value, $params) return is_array($value) ? $rekeyed->all() : $rekeyed; } + /** + * Get the keys of an array. + * + * @return array|Collection + */ + public function keys($value) + { + return is_array($value) ? array_keys($value) : $value->keys(); + } + /** * Returns the last $params[0] characters of a string, or the last element of an array. * @@ -2788,6 +2778,16 @@ public function parse_url($value, $params) return parse_url($value, $component); } + /** + * Get the values of an array. + * + * @return array|Collection + */ + public function values($value) + { + return is_array($value) ? array_values($value) : $value->values(); + } + /** * Get the date difference in weeks. *