8000 [5.3] Custom casting of eloquent properties by browner12 · Pull Request #16305 · laravel/framework · GitHub
[go: up one dir, main page]

Skip to content

[5.3] Custom casting of eloquent properties #16305

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

Closed
wants to merge 1 commit into from
Closed
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
8 changes: 7 additions & 1 deletion src/Illuminate/Database/Eloquent/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -2818,7 +2818,13 @@ protected function castAttribute($key, $value)
return $value;
}

switch ($this->getCastType($key)) {
$castType = $this->getCastType($key);

if (method_exists($this, 'castTo' . strtoupper($castType))) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might be worth to tweak that a bit:

if (method_exists($this, $method = 'castTo'.ucfirst(Str::camel($castType)))) {
    return $this->$method($value);
}

So that 'foo_bar_baz' type could be handled by castToFooBarBaz($value) ?

return $this->{'castTo' . $castType}($value);
}

switch ($castType) {
case 'int':
case 'integer':
return (int) $value;
Expand Down
22 changes: 22 additions & 0 deletions tests/Database/DatabaseEloquentModelTest.php
7E9D
Original file line number Diff line number Diff line change
Expand Up @@ -1282,6 +1282,7 @@ public function testModelAttributesAreCastedWhenPresentInCastsArray()
$model->dateAttribute = '1969-07-20';
$model->datetimeAttribute = '1969-07-20 22:56:00';
$model->timestampAttribute = '1969-07-20 22:56:00';
$model->customAttribute = 'value';

$this->assertInternalType('int', $model->intAttribute);
$this->assertInternalType('float', $model->floatAttribute);
Expand All @@ -1302,6 +1303,8 @@ public function testModelAttributesAreCastedWhenPresentInCastsArray()
$this->assertEquals('1969-07-20', $model->dateAttribute->toDateString());
$this->assertEquals('1969-07-20 22:56:00', $model->datetimeAttribute->toDateTimeString());
$this->assertEquals(-14173440, $model->timestampAttribute);
$this->assertInstanceOf('CustomCastingStub', $model->customAttribute);
$this->assertEquals('value', $model->customAttribute->key);

$arr = $model->toArray();
$this->assertInternalType('int', $arr['intAttrib 8000 ute']);
Expand Down Expand Up @@ -1336,6 +1339,7 @@ public function testModelAttributeCastingPreservesNull()
$model->dateAttribute = null;
$model->datetimeAttribute = null;
$model->timestampAttribute = null;
$model->customAttribute = null;

$attributes = $model->getAttributes();

Expand All @@ -1350,6 +1354,7 @@ public function testModelAttributeCastingPreservesNull()
$this->assertNull($attributes['dateAttribute']);
$this->assertNull($attributes['datetimeAttribute']);
$this->assertNull($attributes['timestampAttribute']);
$this->assertNull($attributes['customAttribute']);

$this->assertNull($model->intAttribute);
$this->assertNull($model->floatAttribute);
Expand All @@ -1362,6 +1367,7 @@ public function testModelAttributeCastingPreservesNull()
$this->assertNull($model->dateAttribute);
$this->assertNull($model->datetimeAttribute);
$this->assertNull($model->timestampAttribute);
$this->assertNull($model->customAttribute);

$array = $model->toArray();

Expand All @@ -1376,6 +1382,7 @@ public function testModelAttributeCastingPreservesNull()
$this->assertNull($array['dateAttribute']);
$this->assertNull($array['datetimeAttribute']);
$this->assertNull($array['timestampAttribute']);
$this->assertNull($array['customAttribute']);
}

public function testUpdatingNonExistentModelFails()
Expand Down Expand Up @@ -1788,12 +1795,27 @@ class EloquentModelCastingStub extends Model
'dateAttribute' => 'date',
'datetimeAttribute' => 'datetime',
'timestampAttribute' => 'timestamp',
'customAttribute' => 'custom'
];

public function jsonAttributeValue()
{
return $this->attributes['jsonAttribute'];
}

public function castToCustom($value){
return new CustomCastingStub($value);
}
}

class CustomCastingStub
{
public $key;

public function __construct($value)
{
$this->key = $value;
}
}

class EloquentModelDynamicHiddenStub extends Illuminate\Database\Eloquent\Model
Expand Down
0