Skip to content

Commit 3955807

Browse files
authored
PHPORM-207 Convert arrow notation -> to dot . (#3170)
1 parent a964156 commit 3955807

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

src/Query/Builder.php

+19-1
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@
6666
use function property_exists;
6767
use function serialize;
6868
use function sprintf;
69+
use function str_contains;
6970
use function str_ends_with;
7071
use function str_replace;
7172
use function str_starts_with;
@@ -1616,7 +1617,24 @@ private function aliasIdForQuery(array $values): array
16161617
}
16171618

16181619
foreach ($values as $key => $value) {
1619-
if (is_string($key) && str_ends_with($key, '.id')) {
1620+
if (! is_string($key)) {
1621+
continue;
1622+
}
1623+
1624+
// "->" arrow notation for subfields is an alias for "." dot notation
1625+
if (str_contains($key, '->')) {
1626+
$newkey = str_replace('->', '.', $key);
1627+
if (array_key_exists($newkey, $values) && $value !== $values[$newkey]) {
1628+
throw new InvalidArgumentException(sprintf('Cannot have both "%s" and "%s" fields.', $key, $newkey));
1629+
}
1630+
1631+
$values[$newkey] = $value;
1632+
unset($values[$key]);
1633+
$key = $newkey;
1634+
}
1635+
1636+
// ".id" subfield are alias for "._id"
1637+
if (str_ends_with($key, '.id')) {
16201638
$newkey = substr($key, 0, -3) . '._id';
16211639
if (array_key_exists($newkey, $values) && $value !== $values[$newkey]) {
16221640
throw new InvalidArgumentException(sprintf('Cannot have both "%s" and "%s" fields.', $key, $newkey));

tests/Query/BuilderTest.php

+10
Original file line numberDiff line numberDiff line change
@@ -1406,6 +1406,16 @@ function (Builder $elemMatchQuery): void {
14061406
],
14071407
])->where('age', 15),
14081408
];
1409+
1410+
yield 'arrow notation' => [
1411+
['find' => [['data.format' => 1], []]],
1412+
fn (Builder $builder) => $builder->where('data->format', 1),
1413+
];
1414+
1415+
yield 'arrow notation with id' => [
1416+
['find' => [['embedded._id' => 1], []]],
1417+
fn (Builder $builder) => $builder->where('embedded->id', 1),
1418+
];
14091419
}
14101420

14111421
#[DataProvider('provideExceptions')]

0 commit comments

Comments
 (0)