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

[9.x] Clear cast cache when setting attributes using arrow #42852

Merged
merged 1 commit into from
Jun 20, 2022
Merged
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
4 changes: 4 additions & 0 deletions src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -1061,6 +1061,10 @@ public function fillJsonAttribute($key, $value)
? $this->castAttributeAsEncryptedString($key, $value)
: $value;

if ($this->isClassCastable($key)) {
unset($this->classCastCache[$key]);
}

return $this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,18 @@ public function testSettingRawAttributesClearsTheCastCache()
$this->assertSame('117 Spencer St.', $model->address->lineOne);
}

public function testSettingAttributesUsingArrowClearsTheCastCache()
{
$model = new TestEloquentModelWithCustomCast;
$model->typed_settings = ['foo' => true];

$this->assertTrue($model->typed_settings->foo);

$model->setAttribute('typed_settings->foo', false);

$this->assertFalse($model->typed_settings->foo);
}

public function testWithCastableInterface()
{
$model = new TestEloquentModelWithCustomCast;
Expand Down Expand Up @@ -280,6 +292,7 @@ class TestEloquentModelWithCustomCast extends Model
'other_password' => HashCaster::class.':md5',
'uppercase' => UppercaseCaster::class,
'options' => JsonCaster::class,
'typed_settings' => JsonSettingsCaster::class,
'value_object_with_caster' => ValueObject::class,
'value_object_caster_with_argument' => ValueObject::class.':argument',
'value_object_caster_with_caster_instance' => ValueObjectWithCasterInstance::class,
Expand Down Expand Up @@ -351,6 +364,41 @@ public function set($model, $key, $value, $attributes)
}
}

class JsonSettingsCaster implements CastsAttributes
{
public function get($model, string $key, $value, array $attributes): ?Settings
{
if ($value === null) {
return null;
}

if ($value instanceof Settings) {
return $value;
}

$payload = json_decode($value, true, JSON_THROW_ON_ERROR);

return Settings::from($payload);
}

public function set($model, string $key, $value, array $attributes): ?string
{
if ($value === null) {
return null;
}

if (is_array($value)) {
$value = Settings::from($value);
}

if (! $value instanceof Settings) {
throw new \Exception("Attribute `{$key}` with JsonSettingsCaster should be a Settings object");
}

return $value->toJson();
}
}

class DecimalCaster implements CastsAttributes, DeviatesCastableAttributes, SerializesCastableAttributes
{
public function get($model, $key, $value, $attributes)
Expand Down Expand Up @@ -465,6 +513,31 @@ public function __construct($lineOne, $lineTwo)
}
}

class Settings
{
public ?bool $foo;
public ?bool $bar;

public function __construct(?bool $foo, ?bool $bar)
{
$this->foo = $foo;
$this->bar = $bar;
}

public static function from(array $data): Settings
{
return new self(
$data['foo'] ?? null,
$data['bar'] ?? null,
);
}

public function toJson($options = 0): string
{
return json_encode(['foo' => $this->foo, 'bar' => $this->bar], $options);
}
}

final class Decimal
{
private $value;
Expand Down