From d02fd051f23d413a624d49effb051586916f6e84 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Tamarelle?= Date: Tue, 25 Jul 2023 17:46:58 +0200 Subject: [PATCH] PHPORM-67 Accept operators prefixed by $ in Query\Builder::orWhere --- CHANGELOG.md | 1 + src/Query/Builder.php | 4 ++-- tests/Query/BuilderTest.php | 13 +++++++++++++ 3 files changed, 16 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0932cb3..39e9875 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,7 @@ All notable changes to this project will be documented in this file. - Throw an exception when `Query\Builder::push()` is used incorrectly [#5](https://github.com/GromNaN/laravel-mongodb-private/pull/5) by [@GromNaN](https://github.com/GromNaN). - Remove public property `Query\Builder::$paginating` [#15](https://github.com/GromNaN/laravel-mongodb-private/pull/15) by [@GromNaN](https://github.com/GromNaN). - Remove call to deprecated `Collection::count` for `countDocuments` [#18](https://github.com/GromNaN/laravel-mongodb-private/pull/18) by [@GromNaN](https://github.com/GromNaN). +- Accept operators prefixed by `$` in `Query\Builder::orWhere` [#20](https://github.com/GromNaN/laravel-mongodb-private/pull/20) by [@GromNaN](https://github.com/GromNaN). ## [3.9.2] - 2022-09-01 diff --git a/src/Query/Builder.php b/src/Query/Builder.php index 4c699a8..4def945 100644 --- a/src/Query/Builder.php +++ b/src/Query/Builder.php @@ -917,10 +917,10 @@ public function where($column, $operator = null, $value = null, $boolean = 'and' $params = func_get_args(); // Remove the leading $ from operators. - if (func_num_args() == 3) { + if (func_num_args() >= 3) { $operator = &$params[1]; - if (Str::startsWith($operator, '$')) { + if (is_string($operator) && str_starts_with($operator, '$')) { $operator = substr($operator, 1); } } diff --git a/tests/Query/BuilderTest.php b/tests/Query/BuilderTest.php index 7cd6f05..fb5bc20 100644 --- a/tests/Query/BuilderTest.php +++ b/tests/Query/BuilderTest.php @@ -76,6 +76,19 @@ public static function provideQueryBuilderToMql(): iterable fn (Builder $builder) => $builder->limit(10)->offset(5)->select('foo', 'bar'), ]; + yield 'where accepts $ in operators' => [ + ['find' => [ + ['$or' => [ + ['foo' => ['$type' => 2]], + ['foo' => ['$type' => 4]], + ]], + [], // options + ]], + fn (Builder $builder) => $builder + ->where('foo', '$type', 2) + ->orWhere('foo', '$type', 4), + ]; + /** @see DatabaseQueryBuilderTest::testBasicWhereNot() */ yield 'whereNot (multiple)' => [ ['find' => [