diff --git a/generator/mql-specifications b/generator/mql-specifications index e15b26d69..d23e9a860 160000 --- a/generator/mql-specifications +++ b/generator/mql-specifications @@ -1 +1 @@ -Subproject commit e15b26d690ddf44c7c8e352485ab6100a1d4af74 +Subproject commit d23e9a8603cb29c5ea90f571bdabcde1d834de4c diff --git a/src/Builder/Expression/FactoryTrait.php b/src/Builder/Expression/FactoryTrait.php index bdc0350e6..22956a9fe 100644 --- a/src/Builder/Expression/FactoryTrait.php +++ b/src/Builder/Expression/FactoryTrait.php @@ -1670,13 +1670,13 @@ public static function replaceAll( * New in MongoDB 4.4 * * @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/replaceOne/ - * @param ResolvesToNull|ResolvesToString|null|string $input The string on which you wish to apply the find. Can be any valid expression that resolves to a string or a null. If input refers to a field that is missing, $replaceAll returns null. - * @param ResolvesToNull|ResolvesToString|null|string $find The string to search for within the given input. Can be any valid expression that resolves to a string or a null. If find refers to a field that is missing, $replaceAll returns null. - * @param ResolvesToNull|ResolvesToString|null|string $replacement The string to use to replace all matched instances of find in input. Can be any valid expression that resolves to a string or a null. + * @param ResolvesToNull|ResolvesToString|null|string $input The string on which you wish to apply the find. Can be any valid expression that resolves to a string or a null. If input refers to a field that is missing, $replaceOne returns null. + * @param Regex|ResolvesToNull|ResolvesToRegex|ResolvesToString|null|string $find The string or regex to search for within the given input. Can be any valid expression that resolves to a string, a regex, or a null. If find refers to a field that is missing, $replaceOne returns null. + * @param ResolvesToNull|ResolvesToString|null|string $replacement The string to use to replace the first matched instance of find in input. Can be any valid expression that resolves to a string or a null. */ public static function replaceOne( ResolvesToNull|ResolvesToString|null|string $input, - ResolvesToNull|ResolvesToString|null|string $find, + Regex|ResolvesToNull|ResolvesToRegex|ResolvesToString|null|string $find, ResolvesToNull|ResolvesToString|null|string $replacement, ): ReplaceOneOperator { return new ReplaceOneOperator($input, $find, $replacement); diff --git a/src/Builder/Expression/ReplaceOneOperator.php b/src/Builder/Expression/ReplaceOneOperator.php index a66d30ff5..da11ce4ee 100644 --- a/src/Builder/Expression/ReplaceOneOperator.php +++ b/src/Builder/Expression/ReplaceOneOperator.php @@ -8,6 +8,7 @@ namespace MongoDB\Builder\Expression; +use MongoDB\BSON\Regex; use MongoDB\Builder\Type\Encode; use MongoDB\Builder\Type\OperatorInterface; @@ -25,23 +26,23 @@ final class ReplaceOneOperator implements ResolvesToString, OperatorInterface public const NAME = '$replaceOne'; public const PROPERTIES = ['input' => 'input', 'find' => 'find', 'replacement' => 'replacement']; - /** @var ResolvesToNull|ResolvesToString|null|string $input The string on which you wish to apply the find. Can be any valid expression that resolves to a string or a null. If input refers to a field that is missing, $replaceAll returns null. */ + /** @var ResolvesToNull|ResolvesToString|null|string $input The string on which you wish to apply the find. Can be any valid expression that resolves to a string or a null. If input refers to a field that is missing, $replaceOne returns null. */ public readonly ResolvesToNull|ResolvesToString|null|string $input; - /** @var ResolvesToNull|ResolvesToString|null|string $find The string to search for within the given input. Can be any valid expression that resolves to a string or a null. If find refers to a field that is missing, $replaceAll returns null. */ - public readonly ResolvesToNull|ResolvesToString|null|string $find; + /** @var Regex|ResolvesToNull|ResolvesToRegex|ResolvesToString|null|string $find The string or regex to search for within the given input. Can be any valid expression that resolves to a string, a regex, or a null. If find refers to a field that is missing, $replaceOne returns null. */ + public readonly Regex|ResolvesToNull|ResolvesToRegex|ResolvesToString|null|string $find; - /** @var ResolvesToNull|ResolvesToString|null|string $replacement The string to use to replace all matched instances of find in input. Can be any valid expression that resolves to a string or a null. */ + /** @var ResolvesToNull|ResolvesToString|null|string $replacement The string to use to replace the first matched instance of find in input. Can be any valid expression that resolves to a string or a null. */ public readonly ResolvesToNull|ResolvesToString|null|string $replacement; /** - * @param ResolvesToNull|ResolvesToString|null|string $input The string on which you wish to apply the find. Can be any valid expression that resolves to a string or a null. If input refers to a field that is missing, $replaceAll returns null. - * @param ResolvesToNull|ResolvesToString|null|string $find The string to search for within the given input. Can be any valid expression that resolves to a string or a null. If find refers to a field that is missing, $replaceAll returns null. - * @param ResolvesToNull|ResolvesToString|null|string $replacement The string to use to replace all matched instances of find in input. Can be any valid expression that resolves to a string or a null. + * @param ResolvesToNull|ResolvesToString|null|string $input The string on which you wish to apply the find. Can be any valid expression that resolves to a string or a null. If input refers to a field that is missing, $replaceOne returns null. + * @param Regex|ResolvesToNull|ResolvesToRegex|ResolvesToString|null|string $find The string or regex to search for within the given input. Can be any valid expression that resolves to a string, a regex, or a null. If find refers to a field that is missing, $replaceOne returns null. + * @param ResolvesToNull|ResolvesToString|null|string $replacement The string to use to replace the first matched instance of find in input. Can be any valid expression that resolves to a string or a null. */ public function __construct( ResolvesToNull|ResolvesToString|null|string $input, - ResolvesToNull|ResolvesToString|null|string $find, + Regex|ResolvesToNull|ResolvesToRegex|ResolvesToString|null|string $find, ResolvesToNull|ResolvesToString|null|string $replacement, ) { $this->input = $input; diff --git a/tests/Builder/Expression/Pipelines.php b/tests/Builder/Expression/Pipelines.php index 862087926..e37008cda 100644 --- a/tests/Builder/Expression/Pipelines.php +++ b/tests/Builder/Expression/Pipelines.php @@ -4520,6 +4520,32 @@ enum Pipelines: string ] JSON; + /** + * Replace Using Regex + * + * @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/replaceOne/#replace-using-regex + */ + case ReplaceOneReplaceUsingRegex = <<<'JSON' + [ + { + "$project": { + "item": { + "$replaceOne": { + "input": "$item", + "find": { + "$regularExpression": { + "pattern": "\\bblue paint\\b", + "options": "" + } + }, + "replacement": "red paint" + } + } + } + } + ] + JSON; + /** * Example * diff --git a/tests/Builder/Expression/ReplaceOneOperatorTest.php b/tests/Builder/Expression/ReplaceOneOperatorTest.php index 90bf8082b..4f8a69b3e 100644 --- a/tests/Builder/Expression/ReplaceOneOperatorTest.php +++ b/tests/Builder/Expression/ReplaceOneOperatorTest.php @@ -4,6 +4,7 @@ namespace MongoDB\Tests\Builder\Expression; +use MongoDB\BSON\Regex; use MongoDB\Builder\Expression; use MongoDB\Builder\Pipeline; use MongoDB\Builder\Stage; @@ -28,4 +29,19 @@ public function testExample(): void $this->assertSamePipeline(Pipelines::ReplaceOneExample, $pipeline); } + + public function testReplaceUsingRegex(): void + { + $pipeline = new Pipeline( + Stage::project( + item: Expression::replaceOne( + input: Expression::stringFieldPath('item'), + find: new Regex('\bblue paint\b'), + replacement: 'red paint', + ), + ), + ); + + $this->assertSamePipeline(Pipelines::ReplaceOneReplaceUsingRegex, $pipeline); + } }