Skip to content

Commit dfa1f8b

Browse files
committed
Check dates against DateTimeInterface instead of DateTime
1 parent 09fcda8 commit dfa1f8b

File tree

4 files changed

+36
-4
lines changed

4 files changed

+36
-4
lines changed

src/Jenssegers/Mongodb/Eloquent/Model.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Jenssegers\Mongodb\Eloquent;
44

55
use DateTime;
6+
use DateTimeInterface;
67
use Illuminate\Contracts\Queue\QueueableCollection;
78
use Illuminate\Contracts\Queue\QueueableEntity;
89
use Illuminate\Database\Eloquent\Model as BaseModel;
@@ -85,7 +86,7 @@ public function fromDateTime($value)
8586
}
8687

8788
// Let Eloquent convert the value to a DateTime instance.
88-
if (!$value instanceof DateTime) {
89+
if (!$value instanceof DateTimeInterface) {
8990
$value = parent::asDateTime($value);
9091
}
9192

src/Jenssegers/Mongodb/Query/Builder.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Closure;
66
use DateTime;
7+
use DateTimeInterface;
78
use Illuminate\Database\Query\Builder as BaseBuilder;
89
use Illuminate\Database\Query\Expression;
910
use Illuminate\Support\Arr;
@@ -929,18 +930,18 @@ protected function compileWheres()
929930
if (isset($where['value'])) {
930931
if (is_array($where['value'])) {
931932
array_walk_recursive($where['value'], function (&$item, $key) {
932-
if ($item instanceof DateTime) {
933+
if ($item instanceof DateTimeInterface) {
933934
$item = new UTCDateTime($item->format('Uv'));
934935
}
935936
});
936937
} else {
937-
if ($where['value'] instanceof DateTime) {
938+
if ($where['value'] instanceof DateTimeInterface) {
938939
$where['value'] = new UTCDateTime($where['value']->format('Uv'));
939940
}
940941
}
941942
} elseif (isset($where['values'])) {
942943
array_walk_recursive($where['values'], function (&$item, $key) {
943-
if ($item instanceof DateTime) {
944+
if ($item instanceof DateTimeInterface) {
944945
$item = new UTCDateTime($item->format('Uv'));
945946
}
946947
});

tests/ModelTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -497,6 +497,9 @@ public function testDates(): void
497497
$user->setAttribute('birthday', new DateTime('1965-08-08 04.08.37.324'));
498498
$this->assertInstanceOf(Carbon::class, $user->birthday);
499499

500+
$user->setAttribute('birthday', new DateTimeImmutable('1965-08-08 04.08.37.324'));
501+
$this->assertInstanceOf(Carbon::class, $user->birthday);
502+
500503
//Test with create and array property
501504
$user = User::create(['name' => 'Jane Doe', 'entry' => ['date' => time()]]);
502505
$this->assertInstanceOf(Carbon::class, $user->getAttribute('entry.date'));

tests/QueryBuilderTest.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -574,6 +574,33 @@ public function testDates()
574574
$this->assertCount(2, $users);
575575
}
576576

577+
public function testImmutableDates()
578+
{
579+
DB::collection('users')->insert([
580+
['name' => 'John Doe', 'birthday' => new UTCDateTime(Date::parse("1980-01-01 00:00:00")->format('Uv'))],
581+
['name' => 'Robert Roe', 'birthday' => new UTCDateTime(Date::parse("1982-01-01 00:00:00")->format('Uv'))],
582+
]);
583+
584+
$user = DB::collection('users')->where('birthday', '=', new DateTimeImmutable("1980-01-01 00:00:00"))->first();
585+
$this->assertNotNull($user);
586+
587+
$user = DB::collection('users')->where('birthday', new DateTimeImmutable("1980-01-01 00:00:00"))->first();
588+
$this->assertNotNull($user);
589+
590+
$users = DB::collection('users')->whereIn('birthday', [
591+
new DateTimeImmutable("1980-01-01 00:00:00"),
592+
new DateTimeImmutable("1982-01-01 00:00:00")
593+
])->get();
594+
$this->assertCount(2, $users);
595+
596+
$users = DB::collection('users')->whereBetween('birthday', [
597+
new DateTimeImmutable("1979-01-01 00:00:00"),
598+
new DateTimeImmutable("1983-01-01 00:00:00")
599+
])->get();
600+
601+
$this->assertCount(2, $users);
602+
}
603+
577604
public function testOperators()
578605
{
579606
DB::collection('users')->insert([

0 commit comments

Comments
 (0)