Skip to content
This repository was archived by the owner on Feb 28, 2025. It is now read-only.

Commit 2134961

Browse files
committed
Simplify encoding of BackedEnum into an array
1 parent b923b24 commit 2134961

File tree

1 file changed

+8
-14
lines changed

1 file changed

+8
-14
lines changed

src/Builder/BuilderEncoder.php

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -102,10 +102,6 @@ public function encode($value): stdClass|array|string
102102
throw new LogicException(sprintf('Class "%s" does not implement OperatorInterface.', $value::class));
103103
}
104104

105-
if ($value instanceof BackedEnum) {
106-
return $this->encodeAsEnum($value);
107-
}
108-
109105
// The generic but incomplete encoding code
110106
switch ($value::ENCODE) {
111107
case Encode::Single:
@@ -129,21 +125,15 @@ public function encode($value): stdClass|array|string
129125
throw new LogicException(sprintf('Class "%s" does not have a valid ENCODE constant.', $value::class));
130126
}
131127

132-
private function encodeAsEnum(OperatorInterface&BackedEnum $value): stdClass
133-
{
134-
$val = $value->value;
135-
if ($value::ENCODE === Encode::Array) {
136-
$val = [$val];
137-
}
138-
139-
return $this->wrap($value, $val);
140-
}
141-
142128
/**
143129
* Encode the value as an array of properties, in the order they are defined in the class.
144130
*/
145131
private function encodeAsArray(OperatorInterface $value): stdClass
146132
{
133+
if ($value instanceof BackedEnum) {
134+
return $this->wrap($value, [$value->value]);
135+
}
136+
147137
$result = [];
148138
/** @var mixed $val */
149139
foreach (get_object_vars($value) as $val) {
@@ -221,6 +211,10 @@ private function encodeAsDollarObject(OperatorInterface $value): stdClass
221211
*/
222212
private function encodeAsSingle(OperatorInterface $value): stdClass
223213
{
214+
if ($value instanceof BackedEnum) {
215+
return $this->wrap($value, $value->value);
216+
}
217+
224218
foreach (get_object_vars($value) as $val) {
225219
$result = $this->recursiveEncode($val);
226220

0 commit comments

Comments
 (0)