Skip to content

Commit e51e651

Browse files
bug #41072 [VarExporter] Add support of PHP enumerations (alexandre-daubois)
This PR was merged into the 4.4 branch. Discussion ---------- [VarExporter] Add support of PHP enumerations | Q | A | ------------- | --- | Branch? | 4.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | Fix #40237 | License | MIT | Doc PR | _none_ Commits ------- 4b0f299 [VarExporter] Add support of PHP enumerations
2 parents 72b6001 + 4b0f299 commit e51e651

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)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy