8000 [VarExporter] Add support of PHP enumerations · symfony/symfony@4b0f299 · GitHub
[go: up one dir, main page]

Skip to content

Commit 4b0f299

Browse files
alexandre-dauboisnicolas-grekas
authored andcommitted
[VarExporter] Add support of PHP enumerations
1 parent c8cc4c3 commit 4b0f299

File tree

5 files changed

+21
-3
lines changed

5 files changed

+21
-3
lines changed

src/Symfony/Component/VarExporter/Internal/Exporter.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public static function prepare($values, $objectsPool, &$refsPool, &$objectsCount
6262
$value = self::prepare($value, $objectsPool, $refsPool, $objectsCount, $valueIsStatic);
6363
}
6464
goto handle_value;
65-
} elseif (!\is_object($value) && !$value instanceof \__PHP_Incomplete_Class) {
65+
} elseif (!\is_object($value) && !$value instanceof \__PHP_Incomplete_Class || $value instanceof \UnitEnum) {
6666
goto handle_value;
6767
}
6868

@@ -190,7 +190,7 @@ public static function prepare($values, $objectsPool, &$refsPool, &$objectsCount
190190
public static function export($value, $indent = '')
191191
{
192192
switch (true) {
193-
case \is_int($value) || \is_float($value): return var_export($value, true);
193+
case \is_int($value) || \is_float($value) || $value instanceof \UnitEnum: return var_export($value, true);
194194
case [] === $value: return '[]';
195195
case false === $value: return 'false';
196196
case true === $value: return 'true';
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace Symfony\Component\VarExporter\Tests\Fixtures;
4+
5+
enum FooUnitEnum
6+
{
7+
case Bar;
8+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
3+
return [
4+
Symfony\Component\VarExporter\Tests\Fixtures\FooUnitEnum::Bar,
5+
];

src/Symfony/Component/VarExporter/Tests/VarExporterTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Symfony\Component\VarExporter\Exception\ClassNotFoundException;
1717
use Symfony\Component\VarExporter\Exception\NotInstantiableTypeException;
1818
use Symfony\Component\VarExporter\Internal\Registry;
19+
use Symfony\Component\VarExporter\Tests\Fixtures\FooUnitEnum;
1920
use Symfony\Component\VarExporter\VarExporter;
2021

2122
class VarExporterTest extends TestCase
@@ -209,6 +210,10 @@ public function provideExport()
209210
yield ['private-constructor', PrivateConstructor::create('bar')];
210211

211212
yield ['php74-serializable', new Php74Serializable()];
213+
214+
if (\PHP_VERSION_ID >= 80100) {
215+
yield ['unit-enum', [FooUnitEnum::Bar], true];
216+
}
212217
}
213218
}
214219

src/Symfony/Component/VarExporter/VarExporter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public static function export($value, bool &$isStaticValue = null): string
4343
{
4444
$isStaticValue = true;
4545

46-
if (!\is_object($value) && !(\is_array($value) && $value) && !$value instanceof \__PHP_Incomplete_Class && !\is_resource($value)) {
46+
if (!\is_object($value) && !(\is_array($value) && $value) && !$value instanceof \__PHP_Incomplete_Class && !\is_resource($value) || $value instanceof \UnitEnum) {
4747
return Exporter::export($value);
4848
}
4949

0 commit comments

Comments
 (0)
0