Skip to content

Commit

Permalink
[5.x] Allow Values object and Group fieldtype to be iterated (#11182)
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonvarga authored Nov 27, 2024
1 parent 9db1705 commit aef574f
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 5 deletions.
9 changes: 4 additions & 5 deletions src/Fields/Value.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Statamic\Facades\Compare;
use Statamic\Support\Str;
use Statamic\View\Antlers\Language\Parser\DocumentTransformer;
use Traversable;

class Value implements ArrayAccess, IteratorAggregate, JsonSerializable
{
Expand Down Expand Up @@ -96,10 +97,6 @@ private function iteratorValue()
$value = $value->get();
}

if ($value instanceof Collection) {
$value = $value->all();
}

return $value;
}

Expand Down Expand Up @@ -127,7 +124,9 @@ public function jsonSerialize($options = 0)
#[\ReturnTypeWillChange]
public function getIterator()
{
return new ArrayIterator($this->iteratorValue());
$value = $this->iteratorValue();

return $value instanceof Traversable ? $value : new ArrayIterator($value);
}

public function shouldParseAntlers()
Expand Down
21 changes: 21 additions & 0 deletions tests/Fields/ValueTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Statamic\Fields\Field;
use Statamic\Fields\Fieldtype;
use Statamic\Fields\Value;
use Statamic\Fields\Values;
use Statamic\Query\Builder;
use Tests\TestCase;

Expand Down Expand Up @@ -301,6 +302,26 @@ public function it_can_iterate_over_query_builder()
], $arr);
}

#[Test]
public function it_can_iterate_over_values()
{
$val = new Value(new Values([
'a' => 'alfa',
'b' => 'bravo',
]));

$arr = [];

foreach ($val as $key => $value) {
$arr[$key] = $value;
}

$this->assertEquals([
'a' => 'alfa',
'b' => 'bravo',
], $arr);
}

#[Test]
public function it_can_proxy_methods_to_value()
{
Expand Down
51 changes: 51 additions & 0 deletions tests/Tags/IterateTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

namespace Tests\Tags;

use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Test;
use Statamic\Facades\Parse;
use Statamic\Fields\Value;
use Statamic\Fields\Values;
use Tests\TestCase;

class IterateTest extends TestCase
{
#[Test]
#[DataProvider('iterateProvider')]
public function it_iterates($value)
{
$template = '{{ foreach:fieldname }}<{{ key }},{{ value }}>{{ /foreach:fieldname }}';

$this->assertSame('<alfa,one><bravo,two>', $this->tag($template, ['fieldname' => $value]));
}

public static function iterateProvider()
{
return [
'array' => [
['alfa' => 'one', 'bravo' => 'two'],
],
'collection' => [
collect(['alfa' => 'one', 'bravo' => 'two']),
],
'values' => [
new Values(['alfa' => 'one', 'bravo' => 'two']),
],
'value with array' => [
new Value(['alfa' => 'one', 'bravo' => 'two']),
],
'value with collection' => [
new Value(collect(['alfa' => 'one', 'bravo' => 'two'])),
],
'value with values' => [
new Value(new Values(['alfa' => 'one', 'bravo' => 'two'])),
],
];
}

private function tag($tag, $context = [])
{
return (string) Parse::template($tag, $context);
}
}

0 comments on commit aef574f

Please sign in to comment.