Skip to content
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
9 changes: 9 additions & 0 deletions src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use DateTimeImmutable;
use DateTimeInterface;
use Illuminate\Contracts\Database\Eloquent\Castable;
use Illuminate\Contracts\Database\Eloquent\CastsAttributes;
use Illuminate\Contracts\Database\Eloquent\CastsInboundAttributes;
use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Database\Eloquent\Casts\AsArrayObject;
Expand Down Expand Up @@ -41,6 +42,7 @@
use ReflectionMethod;
use ReflectionNamedType;
use RuntimeException;
use Stringable;
use ValueError;

use function Illuminate\Support\enum_value;
Expand Down Expand Up @@ -790,6 +792,13 @@ protected function ensureCastsAreStringValues($casts)
{
foreach ($casts as $attribute => $cast) {
$casts[$attribute] = match (true) {
is_object($cast) => value(function () use ($cast, $attribute) {
return $cast instanceof Stringable
? (string) $cast
: throw new InvalidArgumentException(
"The cast object for the {$attribute} attribute must implement Stringable."
);
}),
is_array($cast) => value(function () use ($cast) {
if (count($cast) === 1) {
return $cast[0];
Expand Down
44 changes: 44 additions & 0 deletions tests/Database/DatabaseEloquentModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Illuminate\Tests\Database;

use Closure;
use DateTime;
use DateTimeImmutable;
use DateTimeInterface;
Expand Down Expand Up @@ -59,6 +60,7 @@
use PHPUnit\Framework\TestCase;
use ReflectionClass;
use stdClass;
use Stringable as NativeStringable;

include_once 'Enums.php';

Expand Down Expand Up @@ -3350,6 +3352,37 @@ public function testCastOnArrayFormatWithOneElement()
$this->assertEquals(['bar' => 'foo'], $model->getAttribute('singleElementInArrayAttribute')->toArray());
}

public function testUsingStringableObjectCastUsesStringRepresentation()
{
$model = new EloquentModelCastingStub;

$this->assertEquals('int', $model->getCasts()['castStringableObject']);
}

public function testMergeingStringableObjectCastUSesStringRepresentation()
{
$stringable = new StringableCastBuilder();
$stringable->cast = 'test';

$model = (new EloquentModelCastingStub)->mergeCasts([
'something' => $stringable
]);

$this->assertEquals('test', $model->getCasts()['something']);
}

public function testUsingPlainObjectAsCastThrowsException()
{
$model = new EloquentModelCastingStub;

$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('The cast object for the something attribute must implement Stringable.');

$model->mergeCasts([
'something' => (object) [],
]);
}

public function testUnsavedModel()
{
$user = new UnsavedModel;
Expand Down Expand Up @@ -3906,6 +3939,7 @@ protected function casts(): array
'singleElementInArrayAttribute' => [AsCollection::class],
'duplicatedAttribute' => 'int',
'asToObjectCast' => TestCast::class,
'castStringableObject' => new StringableCastBuilder(),
];
}

Expand Down Expand Up @@ -4372,3 +4406,13 @@ class EloquentChildModelBootingCallbackTestStub extends EloquentModelBootingCall
{
public static bool $bootHasFinished = false;
}

class StringableCastBuilder implements NativeStringable
{
public $cast = 'int';

public function __toString()
{
return $this->cast;
}
}
Loading