Skip to content
This repository has been archived by the owner on Aug 22, 2023. It is now read-only.

Commit

Permalink
Create UTCDateTime from DateTimeInterface
Browse files Browse the repository at this point in the history
  • Loading branch information
GromNaN committed Jul 11, 2023
1 parent c566ba0 commit b13e7e0
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 14 deletions.
8 changes: 4 additions & 4 deletions src/Query/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -984,19 +984,19 @@ 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'])) {
if (is_array($where['values'])) {
array_walk_recursive($where['values'], function (&$item) {
if ($item instanceof DateTimeInterface) {
$item = new UTCDateTime($item->format('Uv'));
$item = new UTCDateTime($item);
}
});
}
Expand Down Expand Up @@ -1174,7 +1174,7 @@ protected function compileWhereBetween(array $where): array
extract($where);

if ($values instanceof CarbonPeriod) {
$values = [new UTCDateTime($values->start->format('Uv')), new UTCDateTime($values->end->format('Uv'))];
$values = [new UTCDateTime($values->start), new UTCDateTime($values->end)];
} else {
$values = Arr::flatten($values);
}
Expand Down
20 changes: 10 additions & 10 deletions tests/QueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -596,19 +596,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']);

Expand All @@ -625,8 +625,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();
Expand Down Expand Up @@ -1103,7 +1103,7 @@ public function testWhereBetweens()
$period = now()->toPeriod(now()->addDay());
$builder->whereBetween('created_at', $period);
$this->assertEquals(['find' => [
['created_at' => ['$gte' => new UTCDateTime($period->start->format('Uv')), '$lte' => new UTCDateTime($period->end->format('Uv'))]],
['created_at' => ['$gte' => new UTCDateTime($period->start), '$lte' => new UTCDateTime($period->end)]],
['typeMap' => ['root' => 'array', 'document' => 'array']]],
], $builder->toMql());

Expand All @@ -1112,7 +1112,7 @@ public function testWhereBetweens()
$period = now()->toPeriod(now()->addMonth());
$builder->whereBetween('created_at', $period);
$this->assertEquals(['find' => [
['created_at' => ['$gte' => new UTCDateTime($period->start->format('Uv')), '$lte' => new UTCDateTime($period->end->format('Uv'))]],
['created_at' => ['$gte' => new UTCDateTime($period->start), '$lte' => new UTCDateTime($period->end)]],
['typeMap' => ['root' => 'array', 'document' => 'array']]],
], $builder->toMql());

Expand Down

0 comments on commit b13e7e0

Please sign in to comment.