diff --git a/generator/src/OperatorClassGenerator.php b/generator/src/OperatorClassGenerator.php index c0ab405c8..6fd80cc41 100644 --- a/generator/src/OperatorClassGenerator.php +++ b/generator/src/OperatorClassGenerator.php @@ -135,6 +135,18 @@ public function createClass(GeneratorDefinition $definition, OperatorDefinition $constructorParam->setDefaultValue($argument->default); } + if ($type->dollarPrefixedString) { + $namespace->addUseFunction('is_string'); + $namespace->addUseFunction('str_starts_with'); + $namespace->addUse(InvalidArgumentException::class); + $constructor->addBody(<<propertyName}) && ! str_starts_with(\${$argument->propertyName}, '$')) { + throw new InvalidArgumentException('Argument \${$argument->propertyName} can be an expression, field paths and variable names must be prefixed by "$" or "$$".'); + } + + PHP); + } + // List type must be validated with array_is_list() if ($type->list) { $namespace->addUseFunction('is_array'); @@ -169,18 +181,6 @@ public function createClass(GeneratorDefinition $definition, OperatorDefinition PHP); } - - if ($type->dollarPrefixedString) { - $namespace->addUseFunction('is_string'); - $namespace->addUseFunction('str_starts_with'); - $namespace->addUse(InvalidArgumentException::class); - $constructor->addBody(<<propertyName}) && ! str_starts_with(\${$argument->propertyName}, '$')) { - throw new InvalidArgumentException('Argument \${$argument->propertyName} can be an expression, field paths and variable names must be prefixed by "$" or "$$".'); - } - - PHP); - } } // Set property from constructor argument diff --git a/src/Builder/Accumulator/AccumulatorAccumulator.php b/src/Builder/Accumulator/AccumulatorAccumulator.php index 94c503996..f26e5f7da 100644 --- a/src/Builder/Accumulator/AccumulatorAccumulator.php +++ b/src/Builder/Accumulator/AccumulatorAccumulator.php @@ -94,14 +94,14 @@ public function __construct( } $this->accumulate = $accumulate; - if (is_array($accumulateArgs) && ! array_is_list($accumulateArgs)) { - throw new InvalidArgumentException('Expected $accumulateArgs argument to be a list, got an associative array.'); - } - if (is_string($accumulateArgs) && ! str_starts_with($accumulateArgs, '$')) { throw new InvalidArgumentException('Argument $accumulateArgs can be an expression, field paths and variable names must be prefixed by "$" or "$$".'); } + if (is_array($accumulateArgs) && ! array_is_list($accumulateArgs)) { + throw new InvalidArgumentException('Expected $accumulateArgs argument to be a list, got an associative array.'); + } + $this->accumulateArgs = $accumulateArgs; if (is_string($merge)) { $merge = new Javascript($merge); @@ -109,14 +109,14 @@ public function __construct( $this->merge = $merge; $this->lang = $lang; - if (is_array($initArgs) && ! array_is_list($initArgs)) { - throw new InvalidArgumentException('Expected $initArgs argument to be a list, got an associative array.'); - } - if (is_string($initArgs) && ! str_starts_with($initArgs, '$')) { throw new InvalidArgumentException('Argument $initArgs can be an expression, field paths and variable names must be prefixed by "$" or "$$".'); } + if (is_array($initArgs) && ! array_is_list($initArgs)) { + throw new InvalidArgumentException('Expected $initArgs argument to be a list, got an associative array.'); + } + $this->initArgs = $initArgs; if (is_string($finalize)) { $finalize = new Javascript($finalize); diff --git a/src/Builder/Accumulator/LastNAccumulator.php b/src/Builder/Accumulator/LastNAccumulator.php index 67e1b0b0a..45a2a0f95 100644 --- a/src/Builder/Accumulator/LastNAccumulator.php +++ b/src/Builder/Accumulator/LastNAccumulator.php @@ -51,14 +51,14 @@ public function __construct( PackedArray|ResolvesToArray|BSONArray|array|string $input, ResolvesToInt|int|string $n, ) { - if (is_array($input) && ! array_is_list($input)) { - throw new InvalidArgumentException('Expected $input argument to be a list, got an associative array.'); - } - if (is_string($input) && ! str_starts_with($input, '$')) { throw new InvalidArgumentException('Argument $input can be an expression, field paths and variable names must be prefixed by "$" or "$$".'); } + if (is_array($input) && ! array_is_list($input)) { + throw new InvalidArgumentException('Expected $input argument to be a list, got an associative array.'); + } + $this->input = $input; if (is_string($n) && ! str_starts_with($n, '$')) { throw new InvalidArgumentException('Argument $n can be an expression, field paths and variable names must be prefixed by "$" or "$$".'); diff --git a/src/Builder/Accumulator/MaxNAccumulator.php b/src/Builder/Accumulator/MaxNAccumulator.php index e44e8953f..eafbfbcfe 100644 --- a/src/Builder/Accumulator/MaxNAccumulator.php +++ b/src/Builder/Accumulator/MaxNAccumulator.php @@ -49,14 +49,14 @@ public function __construct( PackedArray|ResolvesToArray|BSONArray|array|string $input, ResolvesToInt|int|string $n, ) { - if (is_array($input) && ! array_is_list($input)) { - throw new InvalidArgumentException('Expected $input argument to be a list, got an associative array.'); - } - if (is_string($input) && ! str_starts_with($input, '$')) { throw new InvalidArgumentException('Argument $input can be an expression, field paths and variable names must be prefixed by "$" or "$$".'); } + if (is_array($input) && ! array_is_list($input)) { + throw new InvalidArgumentException('Expected $input argument to be a list, got an associative array.'); + } + $this->input = $input; if (is_string($n) && ! str_starts_with($n, '$')) { throw new InvalidArgumentException('Argument $n can be an expression, field paths and variable names must be prefixed by "$" or "$$".'); diff --git a/src/Builder/Accumulator/MinNAccumulator.php b/src/Builder/Accumulator/MinNAccumulator.php index fc088a831..2d5eeab7e 100644 --- a/src/Builder/Accumulator/MinNAccumulator.php +++ b/src/Builder/Accumulator/MinNAccumulator.php @@ -49,14 +49,14 @@ public function __construct( PackedArray|ResolvesToArray|BSONArray|array|string $input, ResolvesToInt|int|string $n, ) { - if (is_array($input) && ! array_is_list($input)) { - throw new InvalidArgumentException('Expected $input argument to be a list, got an associative array.'); - } - if (is_string($input) && ! str_starts_with($input, '$')) { throw new InvalidArgumentException('Argument $input can be an expression, field paths and variable names must be prefixed by "$" or "$$".'); } + if (is_array($input) && ! array_is_list($input)) { + throw new InvalidArgumentException('Expected $input argument to be a list, got an associative array.'); + } + $this->input = $input; if (is_string($n) && ! str_starts_with($n, '$')) { throw new InvalidArgumentException('Argument $n can be an expression, field paths and variable names must be prefixed by "$" or "$$".'); diff --git a/src/Builder/Accumulator/PercentileAccumulator.php b/src/Builder/Accumulator/PercentileAccumulator.php index 7e6b4af67..030f13aab 100644 --- a/src/Builder/Accumulator/PercentileAccumulator.php +++ b/src/Builder/Accumulator/PercentileAccumulator.php @@ -73,14 +73,14 @@ public function __construct( } $this->input = $input; - if (is_array($p) && ! array_is_list($p)) { - throw new InvalidArgumentException('Expected $p argument to be a list, got an associative array.'); - } - if (is_string($p) && ! str_starts_with($p, '$')) { throw new InvalidArgumentException('Argument $p can be an expression, field paths and variable names must be prefixed by "$" or "$$".'); } + if (is_array($p) && ! array_is_list($p)) { + throw new InvalidArgumentException('Expected $p argument to be a list, got an associative array.'); + } + $this->p = $p; $this->method = $method; } diff --git a/src/Builder/Expression/AllElementsTrueOperator.php b/src/Builder/Expression/AllElementsTrueOperator.php index 4dd9c5b4b..282de6dfa 100644 --- a/src/Builder/Expression/AllElementsTrueOperator.php +++ b/src/Builder/Expression/AllElementsTrueOperator.php @@ -39,14 +39,14 @@ final class AllElementsTrueOperator implements ResolvesToBool, OperatorInterface */ public function __construct(PackedArray|ResolvesToArray|BSONArray|array|string $expression) { - if (is_array($expression) && ! array_is_list($expression)) { - throw new InvalidArgumentException('Expected $expression argument to be a list, got an associative array.'); - } - if (is_string($expression) && ! str_starts_with($expression, '$')) { throw new InvalidArgumentException('Argument $expression can be an expression, field paths and variable names must be prefixed by "$" or "$$".'); } + if (is_array($expression) && ! array_is_list($expression)) { + throw new InvalidArgumentException('Expected $expression argument to be a list, got an associative array.'); + } + $this->expression = $expression; } } diff --git a/src/Builder/Expression/AnyElementTrueOperator.php b/src/Builder/Expression/AnyElementTrueOperator.php index a37315e9d..c3566ca9e 100644 --- a/src/Builder/Expression/AnyElementTrueOperator.php +++ b/src/Builder/Expression/AnyElementTrueOperator.php @@ -39,14 +39,14 @@ final class AnyElementTrueOperator implements ResolvesToBool, OperatorInterface */ public function __construct(PackedArray|ResolvesToArray|BSONArray|array|string $expression) { - if (is_array($expression) && ! array_is_list($expression)) { - throw new InvalidArgumentException('Expected $expression argument to be a list, got an associative array.'); - } - if (is_string($expression) && ! str_starts_with($expression, '$')) { throw new InvalidArgumentException('Argument $expression can be an expression, field paths and variable names must be prefixed by "$" or "$$".'); } + if (is_array($expression) && ! array_is_list($expression)) { + throw new InvalidArgumentException('Expected $expression argument to be a list, got an associative array.'); + } + $this->expression = $expression; } } diff --git a/src/Builder/Expression/ArrayElemAtOperator.php b/src/Builder/Expression/ArrayElemAtOperator.php index 0f492ce0b..9cbca3cfe 100644 --- a/src/Builder/Expression/ArrayElemAtOperator.php +++ b/src/Builder/Expression/ArrayElemAtOperator.php @@ -45,14 +45,14 @@ public function __construct( PackedArray|ResolvesToArray|BSONArray|array|string $array, ResolvesToInt|int|string $idx, ) { - if (is_array($array) && ! array_is_list($array)) { - throw new InvalidArgumentException('Expected $array argument to be a list, got an associative array.'); - } - if (is_string($array) && ! str_starts_with($array, '$')) { throw new InvalidArgumentException('Argument $array can be an expression, field paths and variable names must be prefixed by "$" or "$$".'); } + if (is_array($array) && ! array_is_list($array)) { + throw new InvalidArgumentException('Expected $array argument to be a list, got an associative array.'); + } + $this->array = $array; if (is_string($idx) && ! str_starts_with($idx, '$')) { throw new InvalidArgumentException('Argument $idx can be an expression, field paths and variable names must be prefixed by "$" or "$$".'); diff --git a/src/Builder/Expression/ArrayToObjectOperator.php b/src/Builder/Expression/ArrayToObjectOperator.php index 77382c984..3a881363e 100644 --- a/src/Builder/Expression/ArrayToObjectOperator.php +++ b/src/Builder/Expression/ArrayToObjectOperator.php @@ -39,14 +39,14 @@ final class ArrayToObjectOperator implements ResolvesToObject, OperatorInterface */ public function __construct(PackedArray|ResolvesToArray|BSONArray|array|string $array) { - if (is_array($array) && ! array_is_list($array)) { - throw new InvalidArgumentException('Expected $array argument to be a list, got an associative array.'); - } - if (is_string($array) && ! str_starts_with($array, '$')) { throw new InvalidArgumentException('Argument $array can be an expression, field paths and variable names must be prefixed by "$" or "$$".'); } + if (is_array($array) && ! array_is_list($array)) { + throw new InvalidArgumentException('Expected $array argument to be a list, got an associative array.'); + } + $this->array = $array; } } diff --git a/src/Builder/Expression/FilterOperator.php b/src/Builder/Expression/FilterOperator.php index 62f3b8c6c..b2f2df99c 100644 --- a/src/Builder/Expression/FilterOperator.php +++ b/src/Builder/Expression/FilterOperator.php @@ -60,14 +60,14 @@ public function __construct( Optional|string $as = Optional::Undefined, Optional|ResolvesToInt|int|string $limit = Optional::Undefined, ) { - if (is_array($input) && ! array_is_list($input)) { - throw new InvalidArgumentException('Expected $input argument to be a list, got an associative array.'); - } - if (is_string($input) && ! str_starts_with($input, '$')) { throw new InvalidArgumentException('Argument $input can be an expression, field paths and variable names must be prefixed by "$" or "$$".'); } + if (is_array($input) && ! array_is_list($input)) { + throw new InvalidArgumentException('Expected $input argument to be a list, got an associative array.'); + } + $this->input = $input; if (is_string($cond) && ! str_starts_with($cond, '$')) { throw new InvalidArgumentException('Argument $cond can be an expression, field paths and variable names must be prefixed by "$" or "$$".'); diff --git a/src/Builder/Expression/FirstNOperator.php b/src/Builder/Expression/FirstNOperator.php index 19f90488a..9051441dc 100644 --- a/src/Builder/Expression/FirstNOperator.php +++ b/src/Builder/Expression/FirstNOperator.php @@ -50,14 +50,14 @@ public function __construct( } $this->n = $n; - if (is_array($input) && ! array_is_list($input)) { - throw new InvalidArgumentException('Expected $input argument to be a list, got an associative array.'); - } - if (is_string($input) && ! str_starts_with($input, '$')) { throw new InvalidArgumentException('Argument $input can be an expression, field paths and variable names must be prefixed by "$" or "$$".'); } + if (is_array($input) && ! array_is_list($input)) { + throw new InvalidArgumentException('Expected $input argument to be a list, got an associative array.'); + } + $this->input = $input; } }