From a9c431bd50bd7233471240eec9278677443e2911 Mon Sep 17 00:00:00 2001 From: Jason Varga Date: Thu, 10 Mar 2022 19:20:03 -0500 Subject: [PATCH 1/3] Values can get the array --- src/Fields/Values.php | 5 +++++ tests/Fields/ValuesTest.php | 8 ++++++++ 2 files changed, 13 insertions(+) diff --git a/src/Fields/Values.php b/src/Fields/Values.php index 8a7436e2397..f2019e365b1 100644 --- a/src/Fields/Values.php +++ b/src/Fields/Values.php @@ -104,4 +104,9 @@ public function toArray() { return $this->getProxiedInstance()->toArray(); } + + public function all() + { + return $this->getProxiedInstance()->all(); + } } diff --git a/tests/Fields/ValuesTest.php b/tests/Fields/ValuesTest.php index b1bc6f12cec..64a3ad3e426 100644 --- a/tests/Fields/ValuesTest.php +++ b/tests/Fields/ValuesTest.php @@ -56,6 +56,14 @@ public function collection_is_not_converted() $this->assertEquals(['foo' => 'bar'], $collection->all()); } + /** @test */ + public function it_can_get_itself_as_an_array() + { + $values = new Values(collect(['foo' => 'bar'])); + + $this->assertEquals(['foo' => 'bar'], $values->all()); + } + /** @test */ public function its_arrayable() { From a74414c364bbbdb296d30b924f3ace9493e40895 Mon Sep 17 00:00:00 2001 From: Jason Varga Date: Thu, 10 Mar 2022 19:20:27 -0500 Subject: [PATCH 2/3] modifier will convert values instances to array --- src/Modifiers/Modify.php | 9 ++++++++- tests/Modifiers/FluentModifyTest.php | 29 ++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/src/Modifiers/Modify.php b/src/Modifiers/Modify.php index ff79c028236..c77c21d7555 100644 --- a/src/Modifiers/Modify.php +++ b/src/Modifiers/Modify.php @@ -4,6 +4,7 @@ use ArrayIterator; use Exception; +use Statamic\Fields\Values; use Statamic\Support\Arr; class Modify implements \IteratorAggregate @@ -189,6 +190,12 @@ protected function runModifier($modifier, $params) { [$class, $method] = $this->loader->load($modifier); - return $class->$method($this->value, $params, $this->context); + $value = $this->value; + + if ($value instanceof Values) { + $value = $value->all(); + } + + return $class->$method($value, $params, $this->context); } } diff --git a/tests/Modifiers/FluentModifyTest.php b/tests/Modifiers/FluentModifyTest.php index adea32ca498..2f513a739cc 100644 --- a/tests/Modifiers/FluentModifyTest.php +++ b/tests/Modifiers/FluentModifyTest.php @@ -2,6 +2,8 @@ namespace Tests\Modifiers; +use Statamic\Fields\Values; +use Statamic\Modifiers\Modifier; use Statamic\Modifiers\Modify; use Tests\TestCase; @@ -24,4 +26,31 @@ public function it_can_explicitly_fetch_result() $this->assertTrue(is_string($result)); $this->assertEquals("I LOVE NACHO LIBRE, IT'S THE BESSS!!!", $result); } + + /** @test */ + public function passing_a_values_instance_into_it_will_not_convert_it_to_an_array() + { + $values = new Values(['foo' => 'bar']); + + $result = Modify::value($values)->fetch(); + + $this->assertSame($values, $result); + } + + /** @test */ + public function values_instances_get_converted_to_an_array_when_passing_to_a_modifier() + { + (new class extends Modifier { + public static $handle = 'to_values'; + + public function index($value) + { + return new Values($value); + } + })::register(); + + $result = Modify::value(['foo' => 'bar'])->toValues()->typeOf()->fetch(); + + $this->assertEquals('array', $result); + } } From 1beee74db454b94ff612aad232ef8385f268caf2 Mon Sep 17 00:00:00 2001 From: StyleCI Bot Date: Fri, 11 Mar 2022 00:33:44 +0000 Subject: [PATCH 3/3] Apply fixes from StyleCI --- tests/Modifiers/FluentModifyTest.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/Modifiers/FluentModifyTest.php b/tests/Modifiers/FluentModifyTest.php index 2f513a739cc..d2fcbb400a4 100644 --- a/tests/Modifiers/FluentModifyTest.php +++ b/tests/Modifiers/FluentModifyTest.php @@ -40,7 +40,8 @@ public function passing_a_values_instance_into_it_will_not_convert_it_to_an_arra /** @test */ public function values_instances_get_converted_to_an_array_when_passing_to_a_modifier() { - (new class extends Modifier { + (new class extends Modifier + { public static $handle = 'to_values'; public function index($value)