Skip to content

Commit

Permalink
Simplify encoding of BackedEnum into an array
Browse files Browse the repository at this point in the history
  • Loading branch information
GromNaN committed Jan 18, 2024
1 parent b923b24 commit 9443998
Showing 1 changed file with 4 additions and 14 deletions.
18 changes: 4 additions & 14 deletions src/Builder/BuilderEncoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,6 @@ public function encode($value): stdClass|array|string
throw new LogicException(sprintf('Class "%s" does not implement OperatorInterface.', $value::class));
}

if ($value instanceof BackedEnum) {
return $this->encodeAsEnum($value);
}

// The generic but incomplete encoding code
switch ($value::ENCODE) {

Check failure on line 106 in src/Builder/BuilderEncoder.php

View workflow job for this annotation

GitHub Actions / Psalm

UndefinedConstant

src/Builder/BuilderEncoder.php:106:17: UndefinedConstant: Constant MongoDB\Builder\Type\OperatorInterface::ENCODE is not defined (see https://psalm.dev/020)
case Encode::Single:
Expand All @@ -129,21 +125,15 @@ public function encode($value): stdClass|array|string
throw new LogicException(sprintf('Class "%s" does not have a valid ENCODE constant.', $value::class));
}

private function encodeAsEnum(OperatorInterface&BackedEnum $value): stdClass
{
$val = $value->value;
if ($value::ENCODE === Encode::Array) {
$val = [$val];
}

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

/**
* Encode the value as an array of properties, in the order they are defined in the class.
*/
private function encodeAsArray(OperatorInterface $value): stdClass
{
if ($value instanceof BackedEnum) {
return $this->wrap($value, [$value->value]);

Check failure on line 134 in src/Builder/BuilderEncoder.php

View workflow job for this annotation

GitHub Actions / Psalm

NoInterfaceProperties

src/Builder/BuilderEncoder.php:134:41: NoInterfaceProperties: Interfaces cannot have properties (see https://psalm.dev/028)
}

$result = [];
/** @var mixed $val */
foreach (get_object_vars($value) as $val) {
Expand Down

0 comments on commit 9443998

Please sign in to comment.