Skip to content

Commit

Permalink
wrapObject=false is only for encode object
Browse files Browse the repository at this point in the history
  • Loading branch information
GromNaN committed Dec 3, 2024
1 parent 2c805b6 commit 9586098
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 14 deletions.
4 changes: 4 additions & 0 deletions generator/src/Definition/OperatorDefinition.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ public function __construct(
default => throw new UnexpectedValueException(sprintf('Unexpected "encode" value for operator "%s". Got "%s"', $name, $encode)),
};

if (! $wrapObject && $this->encode !== Encode::Object) {
throw new UnexpectedValueException(sprintf('Operator "%s" cannot have wrapObject set to false when encode is not "object"', $name));
}

// Convert arguments to ArgumentDefinition objects
// Optional arguments must be after required arguments
$requiredArgs = $optionalArgs = [];
Expand Down
6 changes: 0 additions & 6 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,6 @@
<code><![CDATA[$val]]></code>
<code><![CDATA[$val]]></code>
</MixedAssignment>
<MixedInferredReturnType>
<code><![CDATA[stdClass]]></code>
</MixedInferredReturnType>
<MixedReturnStatement>
<code><![CDATA[$result]]></code>
</MixedReturnStatement>
<PossibleRawObjectIteration>
<code><![CDATA[$val]]></code>
</PossibleRawObjectIteration>
Expand Down
15 changes: 7 additions & 8 deletions src/Builder/Encoder/OperatorEncoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
use MongoDB\Exception\UnsupportedValueException;
use stdClass;

use function assert;
use function is_string;
use function sprintf;

/**
Expand Down Expand Up @@ -89,6 +91,10 @@ private function encodeAsObject(OperatorInterface $value): stdClass
}
}

if ($value::NAME === null) {
return $result;
}

return $this->wrap($value, $result);
}

Expand All @@ -106,16 +112,9 @@ private function encodeAsSingle(OperatorInterface $value): stdClass
throw new LogicException(sprintf('Class "%s" does not have a single property.', $value::class));
}

/**
* Wrap the result in an object if the operator has a name.
* The operator name is NULL, and the result is returned as is
* when wrapObject is false in the YAML configuration of the operator.
*/
private function wrap(OperatorInterface $value, mixed $result): stdClass
{
if ($value::NAME === null) {
return $result;
}
assert(is_string($value::NAME));

$object = new stdClass();
$object->{$value::NAME} = $result;
Expand Down

0 comments on commit 9586098

Please sign in to comment.