Skip to content

Commit

Permalink
PHPORM-207 Convert arrow notation -> to dot . (#3170)
Browse files Browse the repository at this point in the history
  • Loading branch information
GromNaN authored Oct 7, 2024
1 parent a964156 commit 3955807
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
20 changes: 19 additions & 1 deletion src/Query/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
use function property_exists;
use function serialize;
use function sprintf;
use function str_contains;
use function str_ends_with;
use function str_replace;
use function str_starts_with;
Expand Down Expand Up @@ -1616,7 +1617,24 @@ private function aliasIdForQuery(array $values): array
}

foreach ($values as $key => $value) {
if (is_string($key) && str_ends_with($key, '.id')) {
if (! is_string($key)) {
continue;
}

// "->" arrow notation for subfields is an alias for "." dot notation
if (str_contains($key, '->')) {
$newkey = str_replace('->', '.', $key);
if (array_key_exists($newkey, $values) && $value !== $values[$newkey]) {
throw new InvalidArgumentException(sprintf('Cannot have both "%s" and "%s" fields.', $key, $newkey));
}

$values[$newkey] = $value;
unset($values[$key]);
$key = $newkey;
}

// ".id" subfield are alias for "._id"
if (str_ends_with($key, '.id')) {
$newkey = substr($key, 0, -3) . '._id';
if (array_key_exists($newkey, $values) && $value !== $values[$newkey]) {
throw new InvalidArgumentException(sprintf('Cannot have both "%s" and "%s" fields.', $key, $newkey));
Expand Down
10 changes: 10 additions & 0 deletions tests/Query/BuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1406,6 +1406,16 @@ function (Builder $elemMatchQuery): void {
],
])->where('age', 15),
];

yield 'arrow notation' => [
['find' => [['data.format' => 1], []]],
fn (Builder $builder) => $builder->where('data->format', 1),
];

yield 'arrow notation with id' => [
['find' => [['embedded._id' => 1], []]],
fn (Builder $builder) => $builder->where('embedded->id', 1),
];
}

#[DataProvider('provideExceptions')]
Expand Down

0 comments on commit 3955807

Please sign in to comment.