From bcba0f920b4f4113bc0e5f7fb209318de25e5db6 Mon Sep 17 00:00:00 2001 From: Marc Jauvin Date: Mon, 15 Dec 2025 17:39:21 -0500 Subject: [PATCH 1/4] stay closer to Laravel model --- src/Database/Model.php | 53 ++++++------------------------------------ 1 file changed, 7 insertions(+), 46 deletions(-) diff --git a/src/Database/Model.php b/src/Database/Model.php index ae8a5d25..0dcd5d3d 100644 --- a/src/Database/Model.php +++ b/src/Database/Model.php @@ -1184,55 +1184,16 @@ public function attributesToArray() } } - /* - * Dates - */ - foreach ($this->getDates() as $key) { - if (!isset($attributes[$key])) { - continue; - } - - $attributes[$key] = $this->serializeDate( - $this->asDateTime($attributes[$key]) - ); - } - - /* - * Mutate - */ - $mutatedAttributes = $this->getMutatedAttributes(); - - foreach ($mutatedAttributes as $key) { - if (!array_key_exists($key, $attributes)) { - continue; - } - - $attributes[$key] = $this->mutateAttributeForArray( - $key, - $attributes[$key] - ); - } + $attributes = $this->addDateAttributesToArray($attributes); - /* - * Casts - */ - foreach ($this->casts as $key => $value) { - if ( - !array_key_exists($key, $attributes) || - in_array($key, $mutatedAttributes) - ) { - continue; - } + $attributes = $this->addMutatedAttributesToArray( + $attributes, $mutatedAttributes = $this->getMutatedAttributes() + ); - $attributes[$key] = $this->castAttribute( - $key, - $attributes[$key] - ); - } + $attributes = $this->addCastAttributesToArray( + $attributes, $mutatedAttributes + ); - /* - * Appends - */ foreach ($this->getArrayableAppends() as $key) { $attributes[$key] = $this->mutateAttributeForArray($key, null); } From b52bc00af69dc407dcc14802d26c7d8b7e702b3f Mon Sep 17 00:00:00 2001 From: Marc Jauvin Date: Mon, 15 Dec 2025 17:55:50 -0500 Subject: [PATCH 2/4] improve code readability --- src/Database/Model.php | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/src/Database/Model.php b/src/Database/Model.php index 0dcd5d3d..0ba5402e 100644 --- a/src/Database/Model.php +++ b/src/Database/Model.php @@ -1174,6 +1174,7 @@ public function hasGetMutator($key) public function attributesToArray() { $attributes = $this->getArrayableAttributes(); + $mutatedAttributes = $this->getMutatedAttributes(); /* * Before Event @@ -1186,13 +1187,9 @@ public function attributesToArray() $attributes = $this->addDateAttributesToArray($attributes); - $attributes = $this->addMutatedAttributesToArray( - $attributes, $mutatedAttributes = $this->getMutatedAttributes() - ); + $attributes = $this->addMutatedAttributesToArray($attributes, $mutatedAttributes); - $attributes = $this->addCastAttributesToArray( - $attributes, $mutatedAttributes - ); + $attributes = $this->addCastAttributesToArray($attributes, $mutatedAttributes); foreach ($this->getArrayableAppends() as $key) { $attributes[$key] = $this->mutateAttributeForArray($key, null); From 89a8a362981a79cf76247740cf0577f289d94303 Mon Sep 17 00:00:00 2001 From: Marc Jauvin Date: Tue, 16 Dec 2025 00:45:54 -0500 Subject: [PATCH 3/4] add unit test for date casts --- tests/Database/ModelTest.php | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/tests/Database/ModelTest.php b/tests/Database/ModelTest.php index a5cb2021..60b9e087 100644 --- a/tests/Database/ModelTest.php +++ b/tests/Database/ModelTest.php @@ -470,6 +470,34 @@ public function testAddCasts() $this->assertEquals(['id' => 'int', 'foo' => 'int'], $model->getCasts()); } + public function testAddDatesCasts() + { + $model = new TestModelGuarded(); + $model->timestamps = false; + + $model->addCasts([ + 'created_at' => 'date:Y/m/d', + 'updated_at' => 'datetime:Y_m_d @ H:i', + 'deleted_at' => 'date', + ]); + + $model->created_at = '2025-10-31 22:50:55'; + $model->updated_at = '2025-12-31 23:59:59'; + $model->deleted_at = '2026-01-01 12:13:14'; + + $this->assertInstanceOf(\Winter\Storm\Argon\Argon::class, $model->created_at); + $this->assertInstanceOf(\Winter\Storm\Argon\Argon::class, $model->updated_at); + $this->assertInstanceOf(\Winter\Storm\Argon\Argon::class, $model->deleted_at); + + $this->assertEquals([ + 'created_at' => '2025/10/31', + 'updated_at' => '2025_12_31 @ 23:59', + 'deleted_at' => '2026-01-01T00:00:00.000000Z', + ], + $model->attributesToArray() + ); + } + public function testStringIsTrimmed() { $name = "Name"; From 80ca50f5ba68a02735486b323326544b16b39c99 Mon Sep 17 00:00:00 2001 From: Marc Jauvin Date: Tue, 16 Dec 2025 00:48:29 -0500 Subject: [PATCH 4/4] fix for code quality check --- tests/Database/ModelTest.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/Database/ModelTest.php b/tests/Database/ModelTest.php index 60b9e087..63f7422d 100644 --- a/tests/Database/ModelTest.php +++ b/tests/Database/ModelTest.php @@ -489,7 +489,8 @@ public function testAddDatesCasts() $this->assertInstanceOf(\Winter\Storm\Argon\Argon::class, $model->updated_at); $this->assertInstanceOf(\Winter\Storm\Argon\Argon::class, $model->deleted_at); - $this->assertEquals([ + $this->assertEquals( + [ 'created_at' => '2025/10/31', 'updated_at' => '2025_12_31 @ 23:59', 'deleted_at' => '2026-01-01T00:00:00.000000Z',