Skip to content

Commit

Permalink
Add support for exporting Enums when using PHP8.1+
Browse files Browse the repository at this point in the history
  • Loading branch information
Jakub Potocký authored and BenMorel committed Jun 15, 2022
1 parent d6b1862 commit d1c6b6e
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/Internal/GenericExporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
44 changes: 44 additions & 0 deletions src/Internal/ObjectExporter/EnumExporter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

declare(strict_types=1);

namespace Brick\VarExporter\Internal\ObjectExporter;

use Brick\VarExporter\Internal\ObjectExporter;
use UnitEnum;

/**
* Handles enums.
*
* @internal This class is for internal use, and not part of the public API. It may change at any time without warning.
*/
class EnumExporter extends ObjectExporter
{
/**
* {@inheritDoc}
*
* See: https://github.com/vimeo/psalm/pull/8117
* @psalm-suppress MixedInferredReturnType
* @psalm-suppress MixedReturnStatement
*/
public function supports(\ReflectionObject $reflectionObject) : bool
{
if (! method_exists($reflectionObject, 'isEnum')) {
return false;
}

return $reflectionObject->isEnum();
}

/**
* {@inheritDoc}
*/
public function export(object $object, \ReflectionObject $reflectionObject, array $path, array $parentIds) : array
{
assert($object instanceof UnitEnum);

return [
get_class($object) . '::' . $object->name
];
}
}
5 changes: 5 additions & 0 deletions src/VarExporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
10 changes: 10 additions & 0 deletions tests/Classes/Enum.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

namespace Brick\VarExporter\Tests\Classes;

enum Enum
{
case TEST;
}
15 changes: 15 additions & 0 deletions tests/ExportObjectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Brick\VarExporter\Tests;

use Brick\VarExporter\Tests\Classes\ConstructorAndNoProperties;
use Brick\VarExporter\Tests\Classes\Enum;
use Brick\VarExporter\Tests\Classes\NoProperties;
use Brick\VarExporter\Tests\Classes\Hierarchy;
use Brick\VarExporter\Tests\Classes\PublicPropertiesWithConstructor;
Expand Down Expand Up @@ -544,4 +545,18 @@ public function testExportObjectWithRestrictiveOptions(): void

$this->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);
}
}

0 comments on commit d1c6b6e

Please sign in to comment.