From 843904b21afc48cac6d96d6fa077a15f0586efe0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Tamarelle?= Date: Wed, 12 Jul 2023 13:59:05 +0200 Subject: [PATCH] Create UTCDateTime from DateTimeInterface objects (#8) --- src/Auth/DatabaseTokenRepository.php | 2 +- src/Eloquent/Model.php | 2 +- src/Query/Builder.php | 8 ++++---- tests/QueryBuilderTest.php | 16 ++++++++-------- 4 files changed, 14 insertions(+), 14 deletions(-) diff --git a/src/Auth/DatabaseTokenRepository.php b/src/Auth/DatabaseTokenRepository.php index cf0f89ea..4574cf61 100644 --- a/src/Auth/DatabaseTokenRepository.php +++ b/src/Auth/DatabaseTokenRepository.php @@ -18,7 +18,7 @@ protected function getPayload($email, $token) return [ 'email' => $email, 'token' => $this->hasher->make($token), - 'created_at' => new UTCDateTime(Date::now()->format('Uv')), + 'created_at' => new UTCDateTime(Date::now()), ]; } diff --git a/src/Eloquent/Model.php b/src/Eloquent/Model.php index 2d938b74..2d985f62 100644 --- a/src/Eloquent/Model.php +++ b/src/Eloquent/Model.php @@ -134,7 +134,7 @@ public function getDateFormat() */ public function freshTimestamp() { - return new UTCDateTime(Date::now()->format('Uv')); + return new UTCDateTime(Date::now()); } /** diff --git a/src/Query/Builder.php b/src/Query/Builder.php index 893de033..22d933fe 100644 --- a/src/Query/Builder.php +++ b/src/Query/Builder.php @@ -882,7 +882,7 @@ protected function performUpdate($query, array $options = []) $options = $this->inheritConnectionOptions($options); $wheres = $this->compileWheres(); - $result = $this->collection->UpdateMany($wheres, $query, $options); + $result = $this->collection->updateMany($wheres, $query, $options); if (1 == (int) $result->isAcknowledged()) { return $result->getModifiedCount() ? $result->getModifiedCount() : $result->getUpsertedCount(); } @@ -981,18 +981,18 @@ protected function compileWheres(): array if (is_array($where['value'])) { array_walk_recursive($where['value'], function (&$item, $key) { if ($item instanceof DateTimeInterface) { - $item = new UTCDateTime($item->format('Uv')); + $item = new UTCDateTime($item); } }); } else { if ($where['value'] instanceof DateTimeInterface) { - $where['value'] = new UTCDateTime($where['value']->format('Uv')); + $where['value'] = new UTCDateTime($where['value']); } } } elseif (isset($where['values'])) { array_walk_recursive($where['values'], function (&$item, $key) { if ($item instanceof DateTimeInterface) { - $item = new UTCDateTime($item->format('Uv')); + $item = new UTCDateTime($item); } }); } diff --git a/tests/QueryBuilderTest.php b/tests/QueryBuilderTest.php index d2356d2f..5dbc67cc 100644 --- a/tests/QueryBuilderTest.php +++ b/tests/QueryBuilderTest.php @@ -600,19 +600,19 @@ public function testUpdateSubdocument() public function testDates() { DB::collection('users')->insert([ - ['name' => 'John Doe', 'birthday' => new UTCDateTime(Date::parse('1980-01-01 00:00:00')->format('Uv'))], - ['name' => 'Robert Roe', 'birthday' => new UTCDateTime(Date::parse('1982-01-01 00:00:00')->format('Uv'))], - ['name' => 'Mark Moe', 'birthday' => new UTCDateTime(Date::parse('1983-01-01 00:00:00.1')->format('Uv'))], - ['name' => 'Frank White', 'birthday' => new UTCDateTime(Date::parse('1960-01-01 12:12:12.1')->format('Uv'))], + ['name' => 'John Doe', 'birthday' => new UTCDateTime(Date::parse('1980-01-01 00:00:00'))], + ['name' => 'Robert Roe', 'birthday' => new UTCDateTime(Date::parse('1982-01-01 00:00:00'))], + ['name' => 'Mark Moe', 'birthday' => new UTCDateTime(Date::parse('1983-01-01 00:00:00.1'))], + ['name' => 'Frank White', 'birthday' => new UTCDateTime(Date::parse('1960-01-01 12:12:12.1'))], ]); $user = DB::collection('users') - ->where('birthday', new UTCDateTime(Date::parse('1980-01-01 00:00:00')->format('Uv'))) + ->where('birthday', new UTCDateTime(Date::parse('1980-01-01 00:00:00'))) ->first(); $this->assertEquals('John Doe', $user['name']); $user = DB::collection('users') - ->where('birthday', new UTCDateTime(Date::parse('1960-01-01 12:12:12.1')->format('Uv'))) + ->where('birthday', new UTCDateTime(Date::parse('1960-01-01 12:12:12.1'))) ->first(); $this->assertEquals('Frank White', $user['name']); @@ -629,8 +629,8 @@ public function testDates() public function testImmutableDates() { DB::collection('users')->insert([ - ['name' => 'John Doe', 'birthday' => new UTCDateTime(Date::parse('1980-01-01 00:00:00')->format('Uv'))], - ['name' => 'Robert Roe', 'birthday' => new UTCDateTime(Date::parse('1982-01-01 00:00:00')->format('Uv'))], + ['name' => 'John Doe', 'birthday' => new UTCDateTime(Date::parse('1980-01-01 00:00:00'))], + ['name' => 'Robert Roe', 'birthday' => new UTCDateTime(Date::parse('1982-01-01 00:00:00'))], ]); $users = DB::collection('users')->where('birthday', '=', new DateTimeImmutable('1980-01-01 00:00:00'))->get();