Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/View/Antlers/Language/Runtime/PathDataManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -863,6 +863,21 @@ private function reduceVar($path, $processorData = [])
$this->reducedVar = call_user_func_array([$this->reducedVar, Str::camel($varPath)], []);
$this->resolvedPath[] = '{method:'.$varPath.'}';

if ($doCompact) {
$this->compact($path->isFinal);
}
} elseif (is_object($this->reducedVar) && property_exists($this->reducedVar, Str::camel($varPath))) {
$reflectionProperty = new \ReflectionProperty($this->reducedVar, Str::camel($varPath));

$this->reducedVar = $reflectionProperty->isPublic() ? $this->reducedVar->{Str::camel($varPath)} : null;
$this->resolvedPath[] = '{property:'.$varPath.'}';

if ($doCompact) {
$this->compact($path->isFinal);
}
} elseif (is_object($this->reducedVar)) {
$this->reducedVar = null;

if ($doCompact) {
$this->compact($path->isFinal);
}
Expand Down
82 changes: 82 additions & 0 deletions tests/Antlers/Runtime/DataRetrieverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Tests\Antlers\Runtime;

use Statamic\Facades\Antlers;
use Statamic\Facades\Cascade;
use Statamic\View\Antlers\Language\Runtime\PathDataManager;
use Tests\Antlers\ParserTestCase;

Expand Down Expand Up @@ -76,4 +78,84 @@ public function test_dynamic_keys_are_correctly_set()
$value = $this->getPathValue('page[view:nested:nested1:nested2]', $data);
$this->assertSame(12345, $value);
}

public function test_object_properties_are_retrieved()
{
$data = [
'view' => [
'object' => new class
{
public string $publicProperty = 'Hello Public World!';

protected string $protectedProperty = 'Hello Protected World!';

private string $privateProperty = 'Hello Private World!';
},
],
];

$value = $this->getPathValue('view.object.public_property', $data);
$this->assertSame('Hello Public World!', $value);

$value = $this->getPathValue('view.object.protected_property', $data);
$this->assertNull($value);

$value = $this->getPathValue('view.object.private_property', $data);
$this->assertNull($value);
}

public function test_object_properties_are_usable_in_antlers_conditions()
{
$object = new class
{
public string $truthyStringProperty = 'Hello Public World!';

public bool $truthyBoolProperty = true;

public string $falsyStringProperty = '';

public bool $falsyBoolProperty = false;

protected string $protectedProperty = 'Hello Protected World!';

private string $privateProperty = 'Hello Private World!';
};

Cascade::set('object', $object);

$value = (string) Antlers::parse('{{ if object:truthy_string_property }}yes{{ else }}no{{ /if }}');
$this->assertSame('yes', $value);

$value = (string) Antlers::parse('{{ if object:truthy_bool_property }}yes{{ else }}no{{ /if }}');
$this->assertSame('yes', $value);

$value = (string) Antlers::parse('{{ if object:falsy_string_property }}yes{{ else }}no{{ /if }}');
$this->assertSame('no', $value);

$value = (string) Antlers::parse('{{ if object:falsy_bool_property }}yes{{ else }}no{{ /if }}');
$this->assertSame('no', $value);

$value = (string) Antlers::parse('{{ if object:protected_property }}yes{{ else }}no{{ /if }}');
$this->assertSame('no', $value);

$value = (string) Antlers::parse('{{ if object:private_property }}yes{{ else }}no{{ /if }}');
$this->assertSame('no', $value);
}

public function test_objects_with_no_matching_property_or_method_are_returned_as_null()
{
$data = [
'object' => new class
{
},
];

$value = $this->getPathValue('object.no_existent', $data);
$this->assertNull($value);

Cascade::set('object', $data['object']);

$value = (string) Antlers::parse('{{ if object:no_existent }}yes{{ else }}no{{ /if }}');
$this->assertSame('no', $value);
}
}