Skip to content

Commit

Permalink
Merge v1.x into v2.x (#1594)
Browse files Browse the repository at this point in the history
  • Loading branch information
mongodb-php-bot authored Feb 10, 2025
2 parents a39bb72 + e352fb3 commit 9411fff
Show file tree
Hide file tree
Showing 136 changed files with 2,183 additions and 1,066 deletions.
3 changes: 1 addition & 2 deletions generator/config/accumulator/accumulator.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,7 @@ tests:
}
return state;
}
accumulateArgs:
- '$name'
accumulateArgs: ['$name']
merge:
$code: |-
function(state1, state2) {
Expand Down
12 changes: 12 additions & 0 deletions generator/src/OperatorClassGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(<<<PHP
if (is_string(\${$argument->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');
Expand Down
18 changes: 17 additions & 1 deletion generator/src/OperatorGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
use function ltrim;
use function sort;
use function sprintf;
use function str_starts_with;
use function ucfirst;
use function usort;

Expand Down Expand Up @@ -71,13 +72,19 @@ final protected function getType(string $type): ExpressionDefinition
* Expression types can contain class names, interface, native types or "list".
* PHPDoc types are more precise than native types, so we use them systematically even if redundant.
*
* @return object{native:string,doc:string,use:list<class-string>,list:bool,query:bool,javascript:bool}
* @return object{native:string,doc:string,use:list<class-string>,list:bool,query:bool,javascript:bool,dollarPrefixedString:bool}
*/
final protected function getAcceptedTypes(ArgumentDefinition $arg): stdClass
{
$nativeTypes = [];

$dollarPrefixedString = false;

foreach ($arg->type as $type) {
if (str_starts_with($type, 'resolvesTo')) {
$dollarPrefixedString = true;
}

$type = $this->getType($type);
$nativeTypes = array_merge($nativeTypes, $type->acceptedTypes);

Expand All @@ -91,6 +98,14 @@ final protected function getAcceptedTypes(ArgumentDefinition $arg): stdClass
$nativeTypes[] = Optional::class;
}

// If the argument accepts an expression, a $-prefixed string is accepted (field path or variable)
// Checked only if the argument does not already accept a string
if (in_array('string', $nativeTypes, true)) {
$dollarPrefixedString = false;
} elseif ($dollarPrefixedString) {
$nativeTypes[] = 'string';
}

$docTypes = $nativeTypes = array_unique($nativeTypes);
$use = [];

Expand Down Expand Up @@ -131,6 +146,7 @@ final protected function getAcceptedTypes(ArgumentDefinition $arg): stdClass
'list' => $listCheck,
'query' => $isQuery,
'javascript' => $isJavascript,
'dollarPrefixedString' => $dollarPrefixedString,
];
}

Expand Down
25 changes: 17 additions & 8 deletions src/Builder/Accumulator/AccumulatorAccumulator.php

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 12 additions & 4 deletions src/Builder/Accumulator/AvgAccumulator.php

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 12 additions & 4 deletions src/Builder/Accumulator/BottomNAccumulator.php

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 20 additions & 8 deletions src/Builder/Accumulator/CovariancePopAccumulator.php

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 20 additions & 8 deletions src/Builder/Accumulator/CovarianceSampAccumulator.php

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 9411fff

Please sign in to comment.