Skip to content

[12.x] feat: Make custom eloquent castings comparable for more granular isDirty check #55945

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

Merged
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Illuminate\Contracts\Database\Eloquent;

use Illuminate\Database\Eloquent\Model;

interface ComparesCastableAttributes
{
/**
* Determine if the given values are equal.
*
* @param \Illuminate\Database\Eloquent\Model $model
* @param string $key
* @param mixed $firstValue
* @param mixed $secondValue
* @return bool
*/
public function compare(Model $model, string $key, mixed $firstValue, mixed $secondValue);
}
30 changes: 30 additions & 0 deletions src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -990,6 +990,21 @@ protected function serializeClassCastableAttribute($key, $value)
);
}

/**
* Compare two values for the given attribute using the custom cast class.
*
* @param string $key
* @param mixed $original
* @param mixed $value
* @return bool
*/
protected function compareClassCastableAttribute($key, $original, $value)
{
return $this->resolveCasterClass($key)->compare(
$this, $key, $original, $value
);
}

/**
* Determine if the cast type is a custom date time cast.
*
Expand Down Expand Up @@ -1800,6 +1815,19 @@ protected function isClassSerializable($key)
method_exists($this->resolveCasterClass($key), 'serialize');
}

/**
* Determine if the key is comparable using a custom class.
*
* @param string $key
* @return bool
*/
protected function isClassComparable($key)
{
return ! $this->isEnumCastable($key) &&
$this->isClassCastable($key) &&
method_exists($this->resolveCasterClass($key), 'compare');
}

/**
* Resolve the custom caster class for a given key.
*
Expand Down Expand Up @@ -2265,6 +2293,8 @@ public function originalIsEquivalent($key)
}

return false;
} elseif ($this->isClassComparable($key)) {
return $this->compareClassCastableAttribute($key, $original, $attribute);
}

return is_numeric($attribute) && is_numeric($original)
Expand Down
53 changes: 53 additions & 0 deletions tests/Database/EloquentModelCustomCastingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use GMP;
use Illuminate\Contracts\Database\Eloquent\Castable;
use Illuminate\Contracts\Database\Eloquent\CastsAttributes;
use Illuminate\Contracts\Database\Eloquent\ComparesCastableAttributes;
use Illuminate\Contracts\Database\Eloquent\SerializesCastableAttributes;
use Illuminate\Database\Capsule\Manager as DB;
use Illuminate\Database\Eloquent\Model;
Expand Down Expand Up @@ -53,6 +54,11 @@ public function createSchema()
$table->increments('id');
$table->decimal('amount', 4, 2);
});

$this->schema()->create('documents', function (Blueprint $table) {
$table->increments('id');
$table->json('document');
});
}

/**
Expand All @@ -64,6 +70,7 @@ protected function tearDown(): void
{
$this->schema()->drop('casting_table');
$this->schema()->drop('members');
$this->schema()->drop('documents');
}

#[RequiresPhpExtension('gmp')]
Expand Down Expand Up @@ -176,6 +183,25 @@ public function testModelWithCustomCastsWorkWithCustomIncrementDecrement()
$this->assertEquals('3.00', $model->amount->value);
}

public function test_model_with_custom_casts_compare_function()
{
// Set raw attribute, this is an example of how we would receive JSON string from the database.
// Note the spaces after the colon.
$model = new Document();
$model->setRawAttributes(['document' => '{"content": "content", "title": "hello world"}']);
$model->save();

// Inverse title and content this would result in a different JSON string when json_encode is used
$document = new \stdClass();
$document->title = 'hello world';
$document->content = 'content';
$model->document = $document;

$this->assertFalse($model->isDirty('document'));
$document->title = 'hello world 2';
$this->assertTrue($model->isDirty('document'));
}

/**
* Get a database connection instance.
*
Expand Down Expand Up @@ -410,3 +436,30 @@ class Member extends Model
'amount' => Euro::class,
];
}

class Document extends Model
{
public $timestamps = false;

protected $casts = [
'document' => StructuredDocumentCaster::class,
];
}

class StructuredDocumentCaster implements CastsAttributes, ComparesCastableAttributes
{
public function get($model, $key, $value, $attributes)
{
return json_decode($value);
}

public function set($model, $key, $value, $attributes)
{
return json_encode($value);
}

public function compare($model, $key, $value1, $value2)
{
return json_decode($value1) == json_decode($value2);
}
}