From 4b0f2997a5ac0aac67beaa47e2bbd9210e70bb5d Mon Sep 17 00:00:00 2001 From: Alexandre Daubois Date: Fri, 30 Apr 2021 17:29:27 +0200 Subject: [PATCH] [VarExporter] Add support of PHP enumerations --- src/Symfony/Component/VarExporter/Internal/Exporter.php | 4 ++-- .../Component/VarExporter/Tests/Fixtures/FooUnitEnum.php | 8 ++++++++ .../Component/VarExporter/Tests/Fixtures/unit-enum.php | 5 +++++ .../Component/VarExporter/Tests/VarExporterTest.php | 5 +++++ src/Symfony/Component/VarExporter/VarExporter.php | 2 +- 5 files changed, 21 insertions(+), 3 deletions(-) create mode 100644 src/Symfony/Component/VarExporter/Tests/Fixtures/FooUnitEnum.php create mode 100644 src/Symfony/Component/VarExporter/Tests/Fixtures/unit-enum.php diff --git a/src/Symfony/Component/VarExporter/Internal/Exporter.php b/src/Symfony/Component/VarExporter/Internal/Exporter.php index 74b81d5cf6948..d15126a155e4e 100644 --- a/src/Symfony/Component/VarExporter/Internal/Exporter.php +++ b/src/Symfony/Component/VarExporter/Internal/Exporter.php @@ -62,7 +62,7 @@ public static function prepare($values, $objectsPool, &$refsPool, &$objectsCount $value = self::prepare($value, $objectsPool, $refsPool, $objectsCount, $valueIsStatic); } goto handle_value; - } elseif (!\is_object($value) && !$value instanceof \__PHP_Incomplete_Class) { + } elseif (!\is_object($value) && !$value instanceof \__PHP_Incomplete_Class || $value instanceof \UnitEnum) { goto handle_value; } @@ -190,7 +190,7 @@ public static function prepare($values, $objectsPool, &$refsPool, &$objectsCount public static function export($value, $indent = '') { switch (true) { - case \is_int($value) || \is_float($value): return var_export($value, true); + case \is_int($value) || \is_float($value) || $value instanceof \UnitEnum: return var_export($value, true); case [] === $value: return '[]'; case false === $value: return 'false'; case true === $value: return 'true'; diff --git a/src/Symfony/Component/VarExporter/Tests/Fixtures/FooUnitEnum.php b/src/Symfony/Component/VarExporter/Tests/Fixtures/FooUnitEnum.php new file mode 100644 index 0000000000000..0f569de8328b2 --- /dev/null +++ b/src/Symfony/Component/VarExporter/Tests/Fixtures/FooUnitEnum.php @@ -0,0 +1,8 @@ += 80100) { + yield ['unit-enum', [FooUnitEnum::Bar], true]; + } } } diff --git a/src/Symfony/Component/VarExporter/VarExporter.php b/src/Symfony/Component/VarExporter/VarExporter.php index f1260d152dd87..a5cacb087572f 100644 --- a/src/Symfony/Component/VarExporter/VarExporter.php +++ b/src/Symfony/Component/VarExporter/VarExporter.php @@ -43,7 +43,7 @@ public static function export($value, bool &$isStaticValue = null): string { $isStaticValue = true; - if (!\is_object($value) && !(\is_array($value) && $value) && !$value instanceof \__PHP_Incomplete_Class && !\is_resource($value)) { + if (!\is_object($value) && !(\is_array($value) && $value) && !$value instanceof \__PHP_Incomplete_Class && !\is_resource($value) || $value instanceof \UnitEnum) { return Exporter::export($value); } 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