Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
13 changes: 13 additions & 0 deletions src/Illuminate/Contracts/Database/Eloquent/HasCasterClass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Illuminate\Contracts\Database\Eloquent;

interface HasCasterClass
{
/**
* Get the caster class for this class
*
* @return string
*/
public static function getCasterClass();
}
9 changes: 8 additions & 1 deletion src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Carbon\CarbonInterface;
use DateTimeInterface;
use Illuminate\Contracts\Database\Eloquent\CastsInboundAttributes;
use Illuminate\Contracts\Database\Eloquent\HasCasterClass;
use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Database\Eloquent\JsonEncodingException;
use Illuminate\Database\Eloquent\Relations\Relation;
Expand Down Expand Up @@ -1061,7 +1062,13 @@ class_exists($class = $this->parseCasterClass($this->getCasts()[$key])) &&
*/
protected function resolveCasterClass($key)
{
if (strpos($castType = $this->getCasts()[$key], ':') === false) {
$castType = $this->getCasts()[$key];

if (is_subclass_of($castType, HasCasterClass::class)) {
$castType = $castType::getCasterClass();
}

if (strpos($castType, ':') === false) {
return new $castType;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Illuminate\Contracts\Database\Eloquent\CastsAttributes;
use Illuminate\Contracts\Database\Eloquent\CastsInboundAttributes;
use Illuminate\Contracts\Database\Eloquent\HasCasterClass;
use Illuminate\Database\Eloquent\Model;

/**
Expand Down Expand Up @@ -73,6 +74,33 @@ public function testBasicCustomCasting()
$model->syncOriginal();
$model->options = ['foo' => 'bar'];
$this->assertTrue($model->isDirty('options'));

$model = new TestEloquentModelWithCustomCast;

$model->setRawAttributes([
'address_line_one' => '110 Kingsbrook St.',
'address_line_two' => 'My Childhood House',
]);

$this->assertSame('110 Kingsbrook St.', $model->address_with_caster->lineOne);
$this->assertSame('My Childhood House', $model->address_with_caster->lineTwo);

$this->assertSame('110 Kingsbrook St.', $model->toArray()['address_line_one']);
$this->assertSame('My Childhood House', $model->toArray()['address_line_two']);

$model->address_with_caster->lineOne = '117 Spencer St.';

$this->assertFalse(isset($model->toArray()['address']));
$this->assertSame('117 Spencer St.', $model->toArray()['address_line_one']);
$this->assertSame('My Childhood House', $model->toArray()['address_line_two']);

$this->assertSame('117 Spencer St.', json_decode($model->toJson(), true)['address_line_one']);
$this->assertSame('My Childhood House', json_decode($model->toJson(), true)['address_line_two']);

$model->address_with_caster = null;

$this->assertNull($model->toArray()['address_line_one']);
$this->assertNull($model->toArray()['address_line_two']);
}

public function testOneWayCasting()
Expand Down Expand Up @@ -135,6 +163,7 @@ class TestEloquentModelWithCustomCast extends Model
'other_password' => HashCaster::class.':md5',
'uppercase' => UppercaseCaster::class,
'options' => JsonCaster::class,
'address_with_caster' => AddressWithCaster::class,
];
}

Expand Down Expand Up @@ -201,3 +230,11 @@ public function __construct($lineOne, $lineTwo)
$this->lineTwo = $lineTwo;
}
}

class AddressWithCaster extends Address implements HasCasterClass
{
public static function getCasterClass()
{
return AddressCaster::class;
}
}