Skip to content

Commit

Permalink
formatting and performance fix
Browse files Browse the repository at this point in the history
  • Loading branch information
taylorotwell committed Feb 2, 2018
1 parent 1e4bca5 commit 1f902c8
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
16 changes: 14 additions & 2 deletions src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ protected function addCastAttributesToArray(array $attributes, array $mutatedAtt
$attributes[$key] = $this->serializeDate($attributes[$key]);
}

if ($attributes[$key] && Str::startsWith($value, ['date:', 'datetime:'])) {
if ($attributes[$key] && $this->isCustomDateTimeCast($value)) {
$attributes[$key] = $attributes[$key]->format(explode(':', $value, 2)[1]);
}
}
Expand Down Expand Up @@ -509,13 +509,25 @@ protected function castAttribute($key, $value)
*/
protected function getCastType($key)
{
if (Str::startsWith($this->getCasts()[$key], ['date:', 'datetime:'])) {
if ($this->isCustomDateTimeCast($this->getCasts()[$key])) {
return 'custom_datetime';
}

return trim(strtolower($this->getCasts()[$key]));
}

/**
* Determine if the cast type is a custom date time cast.
*
* @param string $cast
* @return bool
*/
protected function isCustomDateTimeCast($cast)
{
return strncmp($cast, 'date:', 5) === 0 ||
strncmp($cast, 'datetime:', 9) === 0;
}

/**
* Set a given attribute on the model.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public function setUp()
});
}

public function test_user_can_update_nullable_date()
public function test_dates_are_custom_castable()
{
$user = TestModel1::create([
'date_field' => '2019-10-01',
Expand Down

0 comments on commit 1f902c8

Please sign in to comment.