Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.x] Support rendering model attributes in Antlers #10869

Open
wants to merge 3 commits into
base: 5.x
Choose a base branch
from
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
22 changes: 22 additions & 0 deletions src/View/Antlers/Language/Runtime/PathDataManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

use Exception;
use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Log;
Expand Down Expand Up @@ -1014,6 +1016,26 @@ public static function reduce($value, $isPair = true, $reduceBuildersAndAugmenta
$reductionStack[] = $reductionValue->all();
GlobalRuntimeState::$isEvaluatingData = false;

continue;
} elseif ($reductionValue instanceof Model) {
GlobalRuntimeState::$isEvaluatingData = true;
$data = $reductionValue->toArray();

foreach (get_class_methods($reductionValue) as $method) {
if ((new \ReflectionMethod($reductionValue, $method))->getReturnType()?->getName() === Attribute::class) {
$method = Str::snake($method);
$data[$method] = $reductionValue->$method;
}

if (Str::startsWith($method, 'get') && Str::endsWith($method, 'Attribute')) {
$method = Str::of($method)->after('get')->before('Attribute')->snake()->__toString();
$data[$method] = $reductionValue->getAttribute($method);
}
}

$reductionStack[] = $data;
GlobalRuntimeState::$isEvaluatingData = false;

continue;
} elseif ($reductionValue instanceof Arrayable) {
GlobalRuntimeState::$isEvaluatingData = true;
Expand Down
56 changes: 56 additions & 0 deletions tests/Antlers/Runtime/ModelTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

namespace Tests\Antlers\Runtime;

use Illuminate\Database\Eloquent\Casts\Attribute;
use Tests\Antlers\ParserTestCase;

class ModelTest extends ParserTestCase
{
public function test_model_attributes_are_returned()
{
$model = FakeModel::make();
$model->title = 'Title';

$data = [
'model' => $model,
];

$template = <<<'EOT'
{{ model:title }}{{ model:foo_bar }}
EOT;

$this->assertSame('TitleFooBar', $this->renderString($template, $data));
}

public function test_legacy_model_attributes_are_returned()
{
$model = FakeModel::make();
$model->title = 'Title';

$data = [
'model' => $model,
];

$template = <<<'EOT'
{{ model:title }}{{ model:bar_baz }}
EOT;

$this->assertSame('TitleBarBaz', $this->renderString($template, $data));
}
}

class FakeModel extends \Illuminate\Database\Eloquent\Model
{
public function fooBar(): Attribute
{
return Attribute::make(
get: fn () => 'FooBar',
);
}

public function getBarBazAttribute()
{
return 'BarBaz';
}
}
Loading