Skip to content

[12.x] Add AsHtmlString cast #55071

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 2 commits into from
Mar 18, 2025
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
32 changes: 32 additions & 0 deletions src/Illuminate/Database/Eloquent/Casts/AsHtmlString.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace Illuminate\Database\Eloquent\Casts;

use Illuminate\Contracts\Database\Eloquent\Castable;
use Illuminate\Contracts\Database\Eloquent\CastsAttributes;
use Illuminate\Support\HtmlString;

class AsHtmlString implements Castable
{
/**
* Get the caster class to use when casting from / to this cast target.
*
* @param array $arguments
* @return \Illuminate\Contracts\Database\Eloquent\CastsAttributes<\Illuminate\Support\HtmlString, string|HtmlString>
*/
public static function castUsing(array $arguments)
{
return new class implements CastsAttributes
{
public function get($model, $key, $value, $attributes)
{
return isset($value) ? new HtmlString($value) : null;
}

public function set($model, $key, $value, $attributes)
{
return isset($value) ? (string) $value : null;
}
};
}
}
21 changes: 21 additions & 0 deletions tests/Database/DatabaseEloquentModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
use Illuminate\Database\Eloquent\Casts\AsEncryptedCollection;
use Illuminate\Database\Eloquent\Casts\AsEnumArrayObject;
use Illuminate\Database\Eloquent\Casts\AsEnumCollection;
use Illuminate\Database\Eloquent\Casts\AsHtmlString;
use Illuminate\Database\Eloquent\Casts\AsStringable;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Collection;
Expand All @@ -45,6 +46,7 @@
use Illuminate\Support\Carbon;
use Illuminate\Support\Collection as BaseCollection;
use Illuminate\Support\Facades\Crypt;
use Illuminate\Support\HtmlString;
use Illuminate\Support\InteractsWithTime;
use Illuminate\Support\Stringable;
use InvalidArgumentException;
Expand Down Expand Up @@ -264,6 +266,24 @@ public function testDirtyOnCastedStringable()
$this->assertTrue($model->isDirty('asStringableAttribute'));
}

public function testDirtyOnCastedHtmlString()
{
$model = new EloquentModelCastingStub;
$model->setRawAttributes([
'asHtmlStringAttribute' => '<div>foo bar</div>',
]);
$model->syncOriginal();

$this->assertInstanceOf(HtmlString::class, $model->asHtmlStringAttribute);
$this->assertFalse($model->isDirty('asHtmlStringAttribute'));

$model->asHtmlStringAttribute = new HtmlString('<div>foo bar</div>');
$this->assertFalse($model->isDirty('asHtmlStringAttribute'));

$model->asHtmlStringAttribute = new Stringable('<div>foo baz</div>');
$this->assertTrue($model->isDirty('asHtmlStringAttribute'));
}

// public function testDirtyOnCastedEncryptedCollection()
// {
// $this->encrypter = m::mock(Encrypter::class);
Expand Down Expand Up @@ -3670,6 +3690,7 @@ protected function casts(): array
'datetimeAttribute' => 'datetime',
'asarrayobjectAttribute' => AsArrayObject::class,
'asStringableAttribute' => AsStringable::class,
'asHtmlStringAttribute' => AsHtmlString::class,
'asCustomCollectionAttribute' => AsCollection::using(CustomCollection::class),
'asEncryptedArrayObjectAttribute' => AsEncryptedArrayObject::class,
'asEncryptedCustomCollectionAttribute' => AsEncryptedCollection::using(CustomCollection::class),
Expand Down
Loading