Skip to content

Commit

Permalink
Check dates against DateTimeInterface instead of DateTime
Browse files Browse the repository at this point in the history
  • Loading branch information
jeromegamez committed Apr 28, 2021
1 parent 09fcda8 commit dfa1f8b
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 4 deletions.
3 changes: 2 additions & 1 deletion src/Jenssegers/Mongodb/Eloquent/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Jenssegers\Mongodb\Eloquent;

use DateTime;
use DateTimeInterface;
use Illuminate\Contracts\Queue\QueueableCollection;
use Illuminate\Contracts\Queue\QueueableEntity;
use Illuminate\Database\Eloquent\Model as BaseModel;
Expand Down Expand Up @@ -85,7 +86,7 @@ public function fromDateTime($value)
}

// Let Eloquent convert the value to a DateTime instance.
if (!$value instanceof DateTime) {
if (!$value instanceof DateTimeInterface) {
$value = parent::asDateTime($value);
}

Expand Down
7 changes: 4 additions & 3 deletions src/Jenssegers/Mongodb/Query/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Closure;
use DateTime;
use DateTimeInterface;
use Illuminate\Database\Query\Builder as BaseBuilder;
use Illuminate\Database\Query\Expression;
use Illuminate\Support\Arr;
Expand Down Expand Up @@ -929,18 +930,18 @@ protected function compileWheres()
if (isset($where['value'])) {
if (is_array($where['value'])) {
array_walk_recursive($where['value'], function (&$item, $key) {
if ($item instanceof DateTime) {
if ($item instanceof DateTimeInterface) {
$item = new UTCDateTime($item->format('Uv'));
}
});
} else {
if ($where['value'] instanceof DateTime) {
if ($where['value'] instanceof DateTimeInterface) {
$where['value'] = new UTCDateTime($where['value']->format('Uv'));
}
}
} elseif (isset($where['values'])) {
array_walk_recursive($where['values'], function (&$item, $key) {
if ($item instanceof DateTime) {
if ($item instanceof DateTimeInterface) {
$item = new UTCDateTime($item->format('Uv'));
}
});
Expand Down
3 changes: 3 additions & 0 deletions tests/ModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -497,6 +497,9 @@ public function testDates(): void
$user->setAttribute('birthday', new DateTime('1965-08-08 04.08.37.324'));
$this->assertInstanceOf(Carbon::class, $user->birthday);

$user->setAttribute('birthday', new DateTimeImmutable('1965-08-08 04.08.37.324'));
$this->assertInstanceOf(Carbon::class, $user->birthday);

//Test with create and array property
$user = User::create(['name' => 'Jane Doe', 'entry' => ['date' => time()]]);
$this->assertInstanceOf(Carbon::class, $user->getAttribute('entry.date'));
Expand Down
27 changes: 27 additions & 0 deletions tests/QueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -574,6 +574,33 @@ public function testDates()
$this->assertCount(2, $users);
}

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'))],
]);

$user = DB::collection('users')->where('birthday', '=', new DateTimeImmutable("1980-01-01 00:00:00"))->first();
$this->assertNotNull($user);

$user = DB::collection('users')->where('birthday', new DateTimeImmutable("1980-01-01 00:00:00"))->first();
$this->assertNotNull($user);

$users = DB::collection('users')->whereIn('birthday', [
new DateTimeImmutable("1980-01-01 00:00:00"),
new DateTimeImmutable("1982-01-01 00:00:00")
])->get();
$this->assertCount(2, $users);

$users = DB::collection('users')->whereBetween('birthday', [
new DateTimeImmutable("1979-01-01 00:00:00"),
new DateTimeImmutable("1983-01-01 00:00:00")
])->get();

$this->assertCount(2, $users);
}

public function testOperators()
{
DB::collection('users')->insert([
Expand Down

0 comments on commit dfa1f8b

Please sign in to comment.