From e050eb5824ffd1242d0df518dc80f9348bf49423 Mon Sep 17 00:00:00 2001 From: edalzell Date: Tue, 15 Oct 2024 10:21:53 -0700 Subject: [PATCH 1/6] test existing functionality --- tests/Tags/IterateTest.php | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 tests/Tags/IterateTest.php diff --git a/tests/Tags/IterateTest.php b/tests/Tags/IterateTest.php new file mode 100644 index 0000000000..98ae6a4ff9 --- /dev/null +++ b/tests/Tags/IterateTest.php @@ -0,0 +1,35 @@ + [ + 'first' => 'One', + 'second' => 'Two', + ], + ]; + + private function tag($tag, $context = []) + { + return (string) Parse::template($tag, $context); + } + + #[Test] + public function arrays_work() + { + $template = <<<'EOT' +{{ foreach:group_field }}{{ key }} - {{ value }} {{ /foreach:group_field }} +EOT; + + $this->assertSame( + 'first - One second - Two ', + $this->tag($template, $this->data) + ); + } +} From 9b611b363c02bc923637d88b73362a9b13c26ecf Mon Sep 17 00:00:00 2001 From: edalzell Date: Tue, 15 Oct 2024 10:28:51 -0700 Subject: [PATCH 2/6] tests --- tests/Tags/IterateTest.php | 42 +++++++++++++++++++++++++------------- 1 file changed, 28 insertions(+), 14 deletions(-) diff --git a/tests/Tags/IterateTest.php b/tests/Tags/IterateTest.php index 98ae6a4ff9..ad4f1b6bd4 100644 --- a/tests/Tags/IterateTest.php +++ b/tests/Tags/IterateTest.php @@ -8,28 +8,42 @@ class IterateTest extends TestCase { - protected $data = [ - 'group_field' => [ - 'first' => 'One', - 'second' => 'Two', - ], - ]; - - private function tag($tag, $context = []) + #[Test] + public function arrays_work() { - return (string) Parse::template($tag, $context); + $template = '{{ foreach:group_field }}{{ key }} - {{ value }} {{ /foreach:group_field }}'; + + $this->assertSame( + 'first - One second - Two ', + $this->tag($template, [ + 'group_field' => [ + 'first' => 'One', + 'second' => 'Two', + ], + ]) + ); } #[Test] - public function arrays_work() + public function values_work() { - $template = <<<'EOT' -{{ foreach:group_field }}{{ key }} - {{ value }} {{ /foreach:group_field }} -EOT; + $this->markTestSkipped('needs implementation'); + + $template = '{{ foreach:group_field }}{{ key }} - {{ value }} {{ /foreach:group_field }}'; $this->assertSame( 'first - One second - Two ', - $this->tag($template, $this->data) + $this->tag($template, [ + 'group_field' => [ + 'first' => 'One', + 'second' => 'Two', + ], + ]) ); } + + private function tag($tag, $context = []) + { + return (string) Parse::template($tag, $context); + } } From 4155878da2093c2292c00bd0e12148bff7f66d55 Mon Sep 17 00:00:00 2001 From: edalzell Date: Fri, 18 Oct 2024 11:38:08 -0700 Subject: [PATCH 3/6] fix --- src/Tags/Iterate.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/Tags/Iterate.php b/src/Tags/Iterate.php index 8898da2892..d4d917e8d6 100644 --- a/src/Tags/Iterate.php +++ b/src/Tags/Iterate.php @@ -39,6 +39,8 @@ protected function iterate($items) { [$keyKey, $valueKey] = $this->getKeyNames(); + $items = $items instanceof Value ? $items->value() : $items; + $items = collect($items) ->map(function ($value, $key) use ($keyKey, $valueKey) { return [$keyKey => $key, $valueKey => $value]; From 1cea1fbd088e3db19f3ebea73a2d403f42f13f4c Mon Sep 17 00:00:00 2001 From: Jason Varga Date: Wed, 27 Nov 2024 16:52:44 -0500 Subject: [PATCH 4/6] simplify --- tests/Tags/IterateTest.php | 35 ++++++----------------------------- 1 file changed, 6 insertions(+), 29 deletions(-) diff --git a/tests/Tags/IterateTest.php b/tests/Tags/IterateTest.php index ad4f1b6bd4..72a6e15d9a 100644 --- a/tests/Tags/IterateTest.php +++ b/tests/Tags/IterateTest.php @@ -9,37 +9,14 @@ class IterateTest extends TestCase { #[Test] - public function arrays_work() + public function it_iterates() { - $template = '{{ foreach:group_field }}{{ key }} - {{ value }} {{ /foreach:group_field }}'; + $template = '{{ foreach:fieldname }}<{{ key }},{{ value }}>{{ /foreach:fieldname }}'; - $this->assertSame( - 'first - One second - Two ', - $this->tag($template, [ - 'group_field' => [ - 'first' => 'One', - 'second' => 'Two', - ], - ]) - ); - } - - #[Test] - public function values_work() - { - $this->markTestSkipped('needs implementation'); - - $template = '{{ foreach:group_field }}{{ key }} - {{ value }} {{ /foreach:group_field }}'; - - $this->assertSame( - 'first - One second - Two ', - $this->tag($template, [ - 'group_field' => [ - 'first' => 'One', - 'second' => 'Two', - ], - ]) - ); + $this->assertSame('', $this->tag($template, ['fieldname' => [ + 'alfa' => 'one', + 'bravo' => 'two', + ]])); } private function tag($tag, $context = []) From 06440e245c53119b4d57b57d16c97882d5c57615 Mon Sep 17 00:00:00 2001 From: Jason Varga Date: Wed, 27 Nov 2024 16:53:14 -0500 Subject: [PATCH 5/6] add tests for actual issue --- tests/Fields/ValueTest.php | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/tests/Fields/ValueTest.php b/tests/Fields/ValueTest.php index 5a3e128c6f..416ab82cf8 100644 --- a/tests/Fields/ValueTest.php +++ b/tests/Fields/ValueTest.php @@ -213,6 +213,36 @@ public function augment($value) $this->assertSame(false, $value->value()); }); } + + #[Test] + public function it_can_iterate() + { + $value = new Value(['alfa' => 'one', 'bravo' => 'two']); + + $result = []; + + foreach ($value as $key => $item) { + $result[] = $key.','.$item; + } + + $this->assertEquals(['alfa,one', 'bravo,two'], $result); + } + + #[Test] + public function it_can_iterate_if_value_is_already_iterable() + { + $value = new Value( + collect(['alfa' => 'one', 'bravo' => 'two']) + ); + + $result = []; + + foreach ($value as $key => $item) { + $result[] = $key.','.$item; + } + + $this->assertEquals(['alfa,one', 'bravo,two'], $result); + } } class DummyAugmentable implements \Statamic\Contracts\Data\Augmentable From 87401bd1d9f50f269cce0f62b1a167843e8bf52d Mon Sep 17 00:00:00 2001 From: Jason Varga Date: Wed, 27 Nov 2024 16:53:20 -0500 Subject: [PATCH 6/6] replace with actual fix --- src/Fields/Value.php | 5 ++++- src/Tags/Iterate.php | 2 -- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/src/Fields/Value.php b/src/Fields/Value.php index dd71177f58..6eb147f70e 100644 --- a/src/Fields/Value.php +++ b/src/Fields/Value.php @@ -11,6 +11,7 @@ use Statamic\Facades\Compare; use Statamic\Support\Str; use Statamic\View\Antlers\Language\Parser\DocumentTransformer; +use Traversable; class Value implements IteratorAggregate, JsonSerializable { @@ -111,7 +112,9 @@ public function jsonSerialize($options = 0) #[\ReturnTypeWillChange] public function getIterator() { - return new ArrayIterator($this->value()); + $value = $this->value(); + + return $value instanceof Traversable ? $value : new ArrayIterator($value); } public function shouldParseAntlers() diff --git a/src/Tags/Iterate.php b/src/Tags/Iterate.php index d4d917e8d6..8898da2892 100644 --- a/src/Tags/Iterate.php +++ b/src/Tags/Iterate.php @@ -39,8 +39,6 @@ protected function iterate($items) { [$keyKey, $valueKey] = $this->getKeyNames(); - $items = $items instanceof Value ? $items->value() : $items; - $items = collect($items) ->map(function ($value, $key) use ($keyKey, $valueKey) { return [$keyKey => $key, $valueKey => $value];