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

[7.x] Augmentation improvements #481

Merged
merged 3 commits into from
May 3, 2024
Merged
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
107 changes: 68 additions & 39 deletions src/Data/AugmentedModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
use Illuminate\Support\Collection;
use Illuminate\Support\Str;
use Statamic\Data\AbstractAugmented;
use Statamic\Facades\Blink;
use Statamic\Fields\Field;
use Statamic\Fields\Value;
use Statamic\Statamic;
Expand Down Expand Up @@ -84,7 +83,7 @@ protected function appendedAttributes(): Collection
return collect($this->data->getAppends());
}

protected function blueprintFields(): Collection
public function blueprintFields(): Collection
{
$fields = $this->resource->blueprint()->fields()->all();

Expand All @@ -102,19 +101,6 @@ protected function eloquentRelationships()
return $this->resource->eloquentRelationships()->reject(fn ($relationship) => $relationship === 'runwayUri');
}

public function get($handle): Value
{
// When the $handle is an Eloquent relationship, ensure we pass the models to
// augmentation, rather than the Relationship instance.
if ($this->resource->eloquentRelationships()->flip()->has($handle)) {
return Blink::once("Runway::AugmentedModel_Relationship::{$this->data->{$this->resource->primaryKey()}}::{$handle}", function () use ($handle) {
return $this->wrapValue($this->getFromData($handle), $handle);
});
}

return parent::get($handle);
}

protected function getFromData($handle)
{
if (Str::contains($handle, '->')) {
Expand All @@ -124,45 +110,88 @@ protected function getFromData($handle)
return $this->supplements->get($handle) ?? data_get($this->data, $handle);
}

protected function wrapValue($value, $handle)
public function get($handle): Value
{
$fields = $this->blueprintFields();

if ($this->resource->eloquentRelationships()->flip()->has($handle)) {
$relatedField = $this->resource->eloquentRelationships()->flip()->get($handle);

return new Value(
$value,
$handle,
optional($fields->get($relatedField))->fieldtype(),
$this->data
);
$value = $this->wrapEloquentRelationship($handle);

return $value->resolve();
}

if (in_array($handle, $this->nestedFields)) {
$value = collect($value)
->map(function ($value, $key) use ($handle, $fields) {
$field = $fields->get("{$handle}->{$key}");
$value = $this->wrapNestedFields($handle);

return $value->resolve();
}

if ($this->hasModelAccessor($handle)) {
$value = $this->wrapModelAccessor($handle);

return $value->resolve();
}

return parent::get($handle);
}

private function wrapEloquentRelationship(string $handle): Value
{
$relatedField = $this->resource->eloquentRelationships()->flip()->get($handle);

return new Value(
fn () => $this->getFromData($handle),
$handle,
$this->fieldtype($relatedField),
$this->data
);
}

private function wrapNestedFields(string $handle): Value
{
return new Value(
function () use ($handle) {
$value = $this->getFromData($handle);

return collect($value)->map(function ($value, $key) use ($handle) {
return new Value(
$value,
$handle,
$field?->fieldtype(),
$this->data,
$this->fieldtype("{$handle}->{$key}"),
$this->data
);
})
->toArray();
}
})->all();
},
$handle,
null,
$this->data
);
}

if ($value instanceof Attribute) {
$value = ($value->get)();
}
private function hasModelAccessor(string $handle): bool
{
$method = Str::camel($handle);

return method_exists($this->data, $method)
&& (new \ReflectionMethod($this->data, $method))->getReturnType()?->getName() === Attribute::class;
}

private function wrapModelAccessor(string $handle): Value
{
return new Value(
$value,
function () use ($handle) {
$method = Str::camel($handle);

$get = $this->data->$method()->get;

return $get();
},
$handle,
optional($fields->get($handle))->fieldtype(),
$this->fieldtype($handle),
$this->data
);
}

private function fieldtype($handle)
{
return optional($this->blueprintFields()->get($handle))->fieldtype();
}
}
Loading