Skip to content

Commit 1f902c8

Browse files
committed
formatting and performance fix
1 parent 1e4bca5 commit 1f902c8

2 files changed

Lines changed: 15 additions & 3 deletions

File tree

src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ protected function addCastAttributesToArray(array $attributes, array $mutatedAtt
190190
$attributes[$key] = $this->serializeDate($attributes[$key]);
191191
}
192192

193-
if ($attributes[$key] && Str::startsWith($value, ['date:', 'datetime:'])) {
193+
if ($attributes[$key] && $this->isCustomDateTimeCast($value)) {
194194
$attributes[$key] = $attributes[$key]->format(explode(':', $value, 2)[1]);
195195
}
196196
}
@@ -509,13 +509,25 @@ protected function castAttribute($key, $value)
509509
*/
510510
protected function getCastType($key)
511511
{
512-
if (Str::startsWith($this->getCasts()[$key], ['date:', 'datetime:'])) {
512+
if ($this->isCustomDateTimeCast($this->getCasts()[$key])) {
513513
return 'custom_datetime';
514514
}
515515

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

519+
/**
520+
* Determine if the cast type is a custom date time cast.
521+
*
522+
* @param string $cast
523+
* @return bool
524+
*/
525+
protected function isCustomDateTimeCast($cast)
526+
{
527+
return strncmp($cast, 'date:', 5) === 0 ||
528+
strncmp($cast, 'datetime:', 9) === 0;
529+
}
530+
519531
/**
520532
* Set a given attribute on the model.
521533
*

tests/Integration/Database/EloquentModelDateCastingTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public function setUp()
2323
});
2424
}
2525

26-
public function test_user_can_update_nullable_date()
26+
public function test_dates_are_custom_castable()
2727
{
2828
$user = TestModel1::create([
2929
'date_field' => '2019-10-01',

0 commit comments

Comments
 (0)