Skip to content

Commit

Permalink
Adjust attribute cast determination
Browse files Browse the repository at this point in the history
  • Loading branch information
taylorotwell committed Jan 4, 2022
1 parent a6b3868 commit c0d9735
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 9 deletions.
41 changes: 34 additions & 7 deletions src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,13 @@ trait HasAttributes
*/
protected static $attributeMutatorCache = [];

/**
* The cache of the "Attribute" return type marked mutated, gettable attributes for each class.
*
* @var array
*/
protected static $getAttributeMutatorCache = [];

/**
* The cache of the "Attribute" return type marked mutated, settable attributes for each class.
*
Expand Down Expand Up @@ -418,7 +425,7 @@ public function getAttribute($key)
if (array_key_exists($key, $this->attributes) ||
array_key_exists($key, $this->casts) ||
$this->hasGetMutator($key) ||
$this->hasAttributeGetMutator($key) ||
$this->hasAttributeMutator($key) ||
$this->isClassCastable($key)) {
return $this->getAttributeValue($key);
}
Expand Down Expand Up @@ -552,12 +559,12 @@ public function hasGetMutator($key)
}

/**
* Determine if a "Attribute" return type marked get mutator exists for an attribute.
* Determine if a "Attribute" return type marked mutator exists for an attribute.
*
* @param string $key
* @return bool
*/
public function hasAttributeGetMutator($key)
public function hasAttributeMutator($key)
{
if (isset(static::$attributeMutatorCache[get_class($this)][$key])) {
return static::$attributeMutatorCache[get_class($this)][$key];
Expand All @@ -574,6 +581,25 @@ public function hasAttributeGetMutator($key)
$returnType->getName() === Attribute::class;
}

/**
* Determine if a "Attribute" return type marked get mutator exists for an attribute.
*
* @param string $key
* @return bool
*/
public function hasAttributeGetMutator($key)
{
if (isset(static::$getAttributeMutatorCache[get_class($this)][$key])) {
return static::$getAttributeMutatorCache[get_class($this)][$key];
}

if (! $this->hasAttributeMutator($key)) {
return static::$getAttributeMutatorCache[get_class($this)][$key] = false;
}

return static::$getAttributeMutatorCache[get_class($this)][$key] = is_callable($this->{Str::camel($key)}()->get);
}

/**
* Get the value of an attribute using its mutator.
*
Expand Down Expand Up @@ -623,8 +649,8 @@ protected function mutateAttributeForArray($key, $value)
{
if ($this->isClassCastable($key)) {
$value = $this->getClassCastableAttributeValue($key, $value);
} elseif (isset(static::$attributeMutatorCache[get_class($this)][$key]) &&
static::$attributeMutatorCache[get_class($this)][$key] === true) {
} elseif (isset(static::$getAttributeMutatorCache[get_class($this)][$key]) &&
static::$getAttributeMutatorCache[get_class($this)][$key] === true) {
$value = $this->mutateAttributeMarkedAttribute($key, $value);

$value = $value instanceof DateTimeInterface
Expand Down Expand Up @@ -948,7 +974,8 @@ public function hasAttributeSetMutator($key)

return static::$setAttributeMutatorCache[$class][$key] = $returnType &&
$returnType instanceof ReflectionNamedType &&
$returnType->getName() === Attribute::class;
$returnType->getName() === Attribute::class &&
is_callable($this->{$method}()->set);
}

/**
Expand Down Expand Up @@ -2029,7 +2056,7 @@ public function getMutatedAttributes()
*/
public static function cacheMutatedAttributes($class)
{
static::$attributeMutatorCache[$class] =
static::$getAttributeMutatorCache[$class] =
collect($attributeMutatorMethods = static::getAttributeMarkedMutatorMethods($class))
->mapWithKeys(function ($match) {
return [lcfirst(static::$snakeAttributes ? Str::snake($match) : $match) => true];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@ public function testModelsAreProperlyMatchedToParents()
$model1->shouldReceive('getAttribute')->with('parent_key')->andReturn(1);
$model1->shouldReceive('getAttribute')->with('foo')->passthru();
$model1->shouldReceive('hasGetMutator')->andReturn(false);
$model1->shouldReceive('hasAttributeGetMutator')->andReturn(false);
$model1->shouldReceive('hasAttributeMutator')->andReturn(false);
$model1->shouldReceive('getCasts')->andReturn([]);
$model1->shouldReceive('getRelationValue', 'relationLoaded', 'setRelation', 'isRelation')->passthru();

$model2 = m::mock(Model::class);
$model2->shouldReceive('getAttribute')->with('parent_key')->andReturn(2);
$model2->shouldReceive('getAttribute')->with('foo')->passthru();
$model2->shouldReceive('hasGetMutator')->andReturn(false);
$model2->shouldReceive('hasAttributeGetMutator')->andReturn(false);
$model2->shouldReceive('hasAttributeMutator')->andReturn(false);
$model2->shouldReceive('getCasts')->andReturn([]);
$model2->shouldReceive('getRelationValue', 'relationLoaded', 'setRelation', 'isRelation')->passthru();

Expand Down

0 comments on commit c0d9735

Please sign in to comment.