From 1f902c84b25f8799cc4f781ad549158db4167110 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Fri, 2 Feb 2018 16:28:32 -0600 Subject: [PATCH] formatting and performance fix --- .../Database/Eloquent/Concerns/HasAttributes.php | 16 ++++++++++++++-- .../Database/EloquentModelDateCastingTest.php | 2 +- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php b/src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php index 227f6df328e0..e86991bf7088 100644 --- a/src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php +++ b/src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php @@ -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]); } } @@ -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. * diff --git a/tests/Integration/Database/EloquentModelDateCastingTest.php b/tests/Integration/Database/EloquentModelDateCastingTest.php index ec7a41241245..4819eedaee2b 100644 --- a/tests/Integration/Database/EloquentModelDateCastingTest.php +++ b/tests/Integration/Database/EloquentModelDateCastingTest.php @@ -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',