Skip to content

Commit 5fe0894

Browse files
committed
PHPORM-230 Convert DateTimeInterface to UTCDateTime in queries
1 parent a453f8a commit 5fe0894

File tree

2 files changed

+14
-26
lines changed

2 files changed

+14
-26
lines changed

src/Query/Builder.php

+8-26
Original file line numberDiff line numberDiff line change
@@ -1191,32 +1191,12 @@ protected function compileWheres(): array
11911191
}
11921192
}
11931193

1194-
// Convert DateTime values to UTCDateTime.
1195-
if (isset($where['value'])) {
1196-
if (is_array($where['value'])) {
1197-
array_walk_recursive($where['value'], function (&$item, $key) {
1198-
if ($item instanceof DateTimeInterface) {
1199-
$item = new UTCDateTime($item);
1200-
}
1201-
});
1202-
} else {
1203-
if ($where['value'] instanceof DateTimeInterface) {
1204-
$where['value'] = new UTCDateTime($where['value']);
1205-
}
1206-
}
1207-
} elseif (isset($where['values'])) {
1208-
if (is_array($where['values'])) {
1209-
array_walk_recursive($where['values'], function (&$item, $key) {
1210-
if ($item instanceof DateTimeInterface) {
1211-
$item = new UTCDateTime($item);
1212-
}
1213-
});
1214-
} elseif ($where['values'] instanceof CarbonPeriod) {
1215-
$where['values'] = [
1216-
new UTCDateTime($where['values']->getStartDate()),
1217-
new UTCDateTime($where['values']->getEndDate()),
1218-
];
1219-
}
1194+
// Convert CarbonPeriod to UTCDateTime interval.
1195+
if (isset($where['values']) && $where['values'] instanceof CarbonPeriod) {
1196+
$where['values'] = [
1197+
new UTCDateTime($where['values']->getStartDate()),
1198+
new UTCDateTime($where['values']->getEndDate()),
1199+
];
12201200
}
12211201

12221202
// In a sequence of "where" clauses, the logical operator of the
@@ -1645,6 +1625,8 @@ private function aliasIdForQuery(array $values): array
16451625
foreach ($values as &$value) {
16461626
if (is_array($value)) {
16471627
$value = $this->aliasIdForQuery($value);
1628+
} elseif ($value instanceof DateTimeInterface) {
1629+
$value = new UTCDateTime($value);
16481630
}
16491631
}
16501632

tests/Query/BuilderTest.php

+6
Original file line numberDiff line numberDiff line change
@@ -566,6 +566,12 @@ function (Builder $builder) {
566566
fn (Builder $builder) => $builder->whereBetween('id', [[1], [2, 3]]),
567567
];
568568

569+
$date = new DateTimeImmutable('2018-09-30 15:00:00 +02:00');
570+
yield 'where $lt DateTimeInterface' => [
571+
['find' => [['created_at' => ['$lt' => new UTCDateTime($date)]], []]],
572+
fn (Builder $builder) => $builder->where('created_at', '<', $date),
573+
];
574+
569575
$period = now()->toPeriod(now()->addMonth());
570576
yield 'whereBetween CarbonPeriod' => [
571577
[

0 commit comments

Comments
 (0)