Skip to content

Commit 317ba9f

Browse files
committed
Support various time formats in whereTime
1 parent 6e102fd commit 317ba9f

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
@@ -1282,11 +1282,21 @@ protected function compileWhereYear(array $where): array
12821282
*/
12831283
protected function compileWhereTime(array $where): array
12841284
{
1285+
if (! is_string($where['value']) || ! preg_match('/^[0-2][0-9](:[0-6][0-9](:[0-6][0-9])?)?$/', $where['value'], $matches)) {
1286+
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'])));
1287+
}
1288+
1289+
$format = match (count($matches)) {
1290+
1 => '%H',
1291+
2 => '%H:%M',
1292+
3 => '%H:%M:%S',
1293+
};
1294+
12851295
return [
12861296
'$expr' => [
12871297
'$'.$where['operator'] => [
12881298
[
1289-
'$dateToString' => ['date' => '$'.$where['column'], 'format' => '%H:%M:%S'],
1299+
'$dateToString' => ['date' => '$'.$where['column'], 'format' => $format],
12901300
],
12911301
$where['value'],
12921302
],

tests/Query/BuilderTest.php

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

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

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

912960
/** @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)