Skip to content

Commit 78e94ce

Browse files
committed
Support various time formats in whereTime
1 parent 1eb42f8 commit 78e94ce

File tree

3 files changed

+66
-2
lines changed

3 files changed

+66
-2
lines changed

src/Query/Builder.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1273,11 +1273,21 @@ protected function compileWhereYear(array $where): array
12731273
*/
12741274
protected function compileWhereTime(array $where): array
12751275
{
1276+
if (! is_string($where['value']) || ! preg_match('/^[0-2][0-9](:[0-6][0-9](:[0-6][0-9])?)?$/', $where['value'], $matches)) {
1277+
throw new \InvalidArgumentException(sprintf('Invalid time format, expected HH:MM:SS, HH:MM or HH, got "%s"', is_string($where['value']) ? $where['value'] : get_debug_type($where['value'])));
1278+
}
1279+
1280+
$format = match (count($matches)) {
1281+
1 => '%H',
1282+
2 => '%H:%M',
1283+
3 => '%H:%M:%S',
1284+
};
1285+
12761286
return [
12771287
'$expr' => [
12781288
'$'.$where['operator'] => [
12791289
[
1280-
'$dateToString' => ['date' => '$'.$where['column'], 'format' => '%H:%M:%S'],
1290+
'$dateToString' => ['date' => '$'.$where['column'], 'format' => $format],
12811291
],
12821292
$where['value'],
12831293
],

tests/Query/BuilderTest.php

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -760,7 +760,7 @@ function (Builder $builder) {
760760
fn (Builder $builder) => $builder->whereYear('created_at', '>', '2023'),
761761
];
762762

763-
yield 'where time' => [
763+
yield 'where time HH:MM:SS' => [
764764
['find' => [['$expr' => [
765765
'$eq' => [
766766
['$dateToString' => ['date' => '$created_at', 'format' => '%H:%M:%S']],
@@ -770,6 +770,36 @@ function (Builder $builder) {
770770
fn (Builder $builder) => $builder->whereTime('created_at', '10:11:12'),
771771
];
772772

773+
yield 'where time HH:MM' => [
774+
['find' => [['$expr' => [
775+
'$eq' => [
776+
['$dateToString' => ['date' => '$created_at', 'format' => '%H:%M']],
777+
'10:11',
778+
],
779+
]], []]],
780+
fn (Builder $builder) => $builder->whereTime('created_at', '10:11'),
781+
];
782+
783+
yield 'where time HH' => [
784+
['find' => [['$expr' => [
785+
'$eq' => [
786+
['$dateToString' => ['date' => '$created_at', 'format' => '%H']],
787+
'10',
788+
],
789+
]], []]],
790+
fn (Builder $builder) => $builder->whereTime('created_at', '10'),
791+
];
792+
793+
yield 'where time DateTime' => [
794+
['find' => [['$expr' => [
795+
'$eq' => [
796+
['$dateToString' => ['date' => '$created_at', 'format' => '%H:%M:%S']],
797+
'10:11:12',
798+
],
799+
]], []]],
800+
fn (Builder $builder) => $builder->whereTime('created_at', new \DateTimeImmutable('2023-08-22 10:11:12')),
801+
];
802+
773803
yield 'where time >' => [
774804
['find' => [['$expr' => [
775805
'$gt' => [
@@ -908,6 +938,24 @@ public static function provideExceptions(): iterable
908938
'Missing expected ending delimiter "/" in regular expression "/foo#bar"',
909939
fn (Builder $builder) => $builder->where('name', 'regex', '/foo#bar'),
910940
];
941+
942+
yield 'whereTime with invalid time' => [
943+
\InvalidArgumentException::class,
944+
'Invalid time format, expected HH:MM:SS, HH:MM or HH, got "10:11:12:13"',
945+
fn (Builder $builder) => $builder->whereTime('created_at', '10:11:12:13'),
946+
];
947+
948+
yield 'whereTime out of range' => [
949+
\InvalidArgumentException::class,
950+
'Invalid time format, expected HH:MM:SS, HH:MM or HH, got "23:70"',
951+
fn (Builder $builder) => $builder->whereTime('created_at', '23:70'),
952+
];
953+
954+
yield 'whereTime invalid type' => [
955+
\InvalidArgumentException::class,
956+
'Invalid time format, expected HH:MM:SS, HH:MM or HH, got "stdClass"',
957+
fn (Builder $builder) => $builder->whereTime('created_at', new \stdClass()),
958+
];
911959
}
912960

913961
/** @dataProvider getEloquentMethodsNotSupported */

tests/QueryTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,12 @@ public function testWhereTime(): void
270270
$time = Birthday::whereTime('birthday', '10:53:11')->get();
271271
$this->assertCount(1, $time);
272272

273+
$time = Birthday::whereTime('birthday', '10:53')->get();
274+
$this->assertCount(6, $time);
275+
276+
$time = Birthday::whereTime('birthday', '10')->get();
277+
$this->assertCount(6, $time);
278+
273279
$time = Birthday::whereTime('birthday', '>=', '10:53:14')->get();
274280
$this->assertCount(3, $time);
275281

0 commit comments

Comments
 (0)