Skip to content

Commit

Permalink
Merge pull request mongodb#2239 from jeromegamez/date-time-interface
Browse files Browse the repository at this point in the history
Check dates against `DateTimeInterface` instead of `DateTime`
  • Loading branch information
mnphpexpert committed Apr 30, 2021
2 parents fd0a837 + f0db3ee commit f78cb25
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 6 deletions.
4 changes: 2 additions & 2 deletions src/Jenssegers/Mongodb/Eloquent/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,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 +85,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
8 changes: 4 additions & 4 deletions src/Jenssegers/Mongodb/Query/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace Jenssegers\Mongodb\Query;

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

$users = DB::collection('users')->where('birthday', '=', new DateTimeImmutable("1980-01-01 00:00:00"))->get();
$this->assertCount(1, $users);

$users = DB::collection('users')->where('birthday', new DateTimeImmutable("1980-01-01 00:00:00"))->get();
$this->assertCount(1, $users);

$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 f78cb25

Please sign in to comment.