Skip to content

Commit

Permalink
Stop trying to initialize normalizedModel but lazyily load to propert…
Browse files Browse the repository at this point in the history
…y when required
  • Loading branch information
rubenvanassche committed Oct 4, 2024
1 parent 53770f0 commit d017301
Showing 1 changed file with 2 additions and 34 deletions.
36 changes: 2 additions & 34 deletions src/Normalizers/Normalized/NormalizedModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ class NormalizedModel implements Normalized
public function __construct(
protected Model $model
) {
$this->initialize($this->model);
}

public function getProperty(string $name, DataProperty $dataProperty): mixed
Expand All @@ -32,41 +31,10 @@ public function getProperty(string $name, DataProperty $dataProperty): mixed
return $value;
}

protected function initialize(Model $model): void
{
$this->properties = $model->attributesToArray();

foreach ($model->getDates() as $key) {
if (isset($this->properties[$key])) {
$this->properties[$key] = $model->getAttribute($key);
}
}

foreach ($model->getCasts() as $key => $cast) {
if ($this->isDateCast($cast)) {
if (isset($this->properties[$key])) {
$this->properties[$key] = $model->getAttribute($key);
}
}
}
}

protected function isDateCast(string $cast): bool
{
return in_array($cast, [
'date',
'datetime',
'immutable_date',
'immutable_datetime',
'custom_datetime',
'immutable_custom_datetime',
]);
}

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

$camelName = Str::camel($name);
Expand Down

2 comments on commit d017301

@Tofandel
Copy link
Contributor

@Tofandel Tofandel commented on d017301 Oct 4, 2024

Choose a reason for hiding this comment

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

hasAttribute is unfortunately a bit too recent laravel/framework@92beae3

Laravel >=11.3 only

@rubenvanassche
Copy link
Member Author

Choose a reason for hiding this comment

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

Pffff

Did something ugly to fix it, but probably will kick out Laravel 10 as a dependency within the next weeks.

Please sign in to comment.