Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PHPORM-207 Convert arrow notation -> to dot . #3170

Merged
merged 1 commit into from
Oct 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading