diff --git a/src/Internal/GenericExporter.php b/src/Internal/GenericExporter.php index e665fd2..19e8cc8 100644 --- a/src/Internal/GenericExporter.php +++ b/src/Internal/GenericExporter.php @@ -96,6 +96,10 @@ public function __construct(int $options, int $indentLevel = 0) $this->objectExporters[] = new ObjectExporter\SerializeExporter($this); } + if (! ($options & VarExporter::NO_ENUMS)) { + $this->objectExporters[] = new ObjectExporter\EnumExporter($this); + } + if (! ($options & VarExporter::NOT_ANY_OBJECT)) { $this->objectExporters[] = new ObjectExporter\AnyObjectExporter($this); } diff --git a/src/Internal/ObjectExporter/EnumExporter.php b/src/Internal/ObjectExporter/EnumExporter.php new file mode 100644 index 0000000..6c47358 --- /dev/null +++ b/src/Internal/ObjectExporter/EnumExporter.php @@ -0,0 +1,44 @@ +isEnum(); + } + + /** + * {@inheritDoc} + */ + public function export(object $object, \ReflectionObject $reflectionObject, array $path, array $parentIds) : array + { + assert($object instanceof UnitEnum); + + return [ + get_class($object) . '::' . $object->name + ]; + } +} diff --git a/src/VarExporter.php b/src/VarExporter.php index c271768..83b0892 100644 --- a/src/VarExporter.php +++ b/src/VarExporter.php @@ -63,6 +63,11 @@ final class VarExporter */ public const TRAILING_COMMA_IN_ARRAY = 1 << 9; + /** + * Disallows exporting enums. + */ + public const NO_ENUMS = 1 << 10; + /** * @param mixed $var The variable to export. * @param int $options A bitmask of options. Possible values are `VarExporter::*` constants. diff --git a/tests/Classes/Enum.php b/tests/Classes/Enum.php new file mode 100644 index 0000000..850403d --- /dev/null +++ b/tests/Classes/Enum.php @@ -0,0 +1,10 @@ +assertExportThrows($expectedMessage, $object, VarExporter::NOT_ANY_OBJECT); } + + /** + * @requires PHP 8.1 + */ + public function testExportEnum(): void + { + $object = Enum::TEST; + + $expected = <<<'PHP' +Brick\VarExporter\Tests\Classes\Enum::TEST +PHP; + + $this->assertExportEquals($expected, $object); + } }