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

Fix retrieval of property to work for Astrotomic/translatable #883

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
18 changes: 10 additions & 8 deletions src/Normalizers/Normalized/NormalizedModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Spatie\LaravelData\Normalizers\Normalized;

use Illuminate\Database\Eloquent\MissingAttributeException;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Str;
use ReflectionProperty;
Expand Down Expand Up @@ -38,10 +39,6 @@ public function getProperty(string $name, DataProperty $dataProperty): mixed

protected function fetchNewProperty(string $name, DataProperty $dataProperty): mixed
{
if ($this->hasModelAttribute($name)) {
return $this->properties[$name] = $this->model->getAttributeValue($name);
}

$camelName = Str::camel($name);
Copy link
Contributor Author

@Tofandel Tofandel Oct 20, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should get rid of this in the automatic camel, snake casing in the next major, after looking back at it, we should not do this manually for relations and rely on the content mapping of the lib https://spatie.be/docs/laravel-data/v4/as-a-data-transfer-object/model-to-data-object#content-mapping-property-names

Copy link
Contributor Author

@Tofandel Tofandel Oct 20, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here is the commit for my proposal on how it should be handled in the next major 380942b

Attributes are still snake cased if necessary but not relations


if ($dataProperty->attributes->contains(fn (object $attribute) => $attribute::class === LoadRelation::class)) {
Expand All @@ -59,6 +56,14 @@ protected function fetchNewProperty(string $name, DataProperty $dataProperty): m
return $this->properties[$name] = $this->model->getRelation($camelName);
}

if (! $this->model->isRelation($name) && ! $this->model->isRelation($camelName)) {
try {
return $this->properties[$name] = $this->model->getAttribute($name);
} catch (MissingAttributeException) {
// Fallback if missing Attribute
}
}

return $this->properties[$name] = UnknownProperty::create();
}

Expand All @@ -68,16 +73,13 @@ protected function hasModelAttribute(string $name): bool
return $this->model->hasAttribute($name);
}

// TODO: to use that one once we stop supporting Laravel 10

// TODO: remove this once we stop supporting Laravel 10
if (! isset($this->attributesProperty)) {
$this->attributesProperty = new ReflectionProperty($this->model, 'attributes');
$this->attributesProperty->setAccessible(true);
Copy link
Contributor Author

@Tofandel Tofandel Oct 20, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line does nothing because: https://www.php.net/manual/en/reflectionproperty.setaccessible.php

Note: As of PHP 8.1.0, calling this method has no effect; all properties are accessible by default.

And we require php 8.1 in composer.json

}

if (! isset($this->castsProperty)) {
$this->castsProperty = new ReflectionProperty($this->model, 'casts');
$this->castsProperty->setAccessible(true);
}

return array_key_exists($name, $this->attributesProperty->getValue($this->model)) ||
Expand Down