Skip to content

Commit 6b13713

Browse files
committed
Support for BackedEnum
1 parent a13bc56 commit 6b13713

File tree

2 files changed

+13
-14
lines changed

2 files changed

+13
-14
lines changed

composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"swoole"
77
],
88
"require" : {
9-
"php" : ">=8.0",
9+
"php" : ">=8.1",
1010
"swango/environment" : "^1.0",
1111
"swlib/saber" : "^1.0"
1212
},

src/Xml.php

+12-13
Original file line numberDiff line numberDiff line change
@@ -57,18 +57,11 @@ public static function decodeAsObject(string $data, array $need_array = []): std
5757
$ret->{$name} = self::intoObject($xml, $need_array, $name);
5858
return $ret;
5959
}
60-
private static function toXml(&$data, \Psr\Http\Message\StreamInterface $result, string $outer_key,
60+
private static function toXml(&$data,
61+
\Psr\Http\Message\StreamInterface $result,
62+
string $outer_key,
6163
?int $level): void {
62-
$is_fixed_array = true;
63-
$i = 0;
64-
foreach ($data as $key => &$val) {
65-
if ($key !== $i) {
66-
$is_fixed_array = false;
67-
break;
68-
}
69-
++$i;
70-
}
71-
unset($val);
64+
$is_fixed_array = is_array($data) && array_is_list($data);
7265

7366
if (! $is_fixed_array && '' !== $outer_key) {
7467
if ($level) {
@@ -80,6 +73,9 @@ private static function toXml(&$data, \Psr\Http\Message\StreamInterface $result,
8073
}
8174
}
8275
foreach ($data as $key => &$val) {
76+
if ($val instanceof \BackedEnum) {
77+
$val = $val->value;
78+
}
8379
if (is_scalar($val)) {
8480
if (isset($level)) {
8581
$result->write(str_repeat(' ', $is_fixed_array ? $level : $level + 1));
@@ -97,8 +93,11 @@ private static function toXml(&$data, \Psr\Http\Message\StreamInterface $result,
9793
$result->write("\n");
9894
}
9995
} else {
100-
self::toXml($val, $result, $is_fixed_array ? $outer_key : $key,
101-
isset($level) ? ($is_fixed_array ? $level : $level + 1) : null);
96+
self::toXml($val,
97+
$result,
98+
$is_fixed_array ? $outer_key : $key,
99+
isset($level) ? ($is_fixed_array ? $level : $level + 1) : null
100+
);
102101
}
103102
}
104103
if (! $is_fixed_array && '' !== $outer_key) {

0 commit comments

Comments
 (0)