Skip to content

Commit

Permalink
ValueGenerator - added support for enumerations
Browse files Browse the repository at this point in the history
Signed-off-by: Rastusik <mfris@pixelfederation.com>
  • Loading branch information
Rastusik committed Aug 4, 2022
1 parent c92e17c commit bd480f6
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/Generator/ValueGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use ArrayObject as SplArrayObject;
use Laminas\Code\Exception\InvalidArgumentException;
use Laminas\Stdlib\ArrayObject as StdlibArrayObject;
use UnitEnum;

use function addcslashes;
use function array_keys;
Expand Down Expand Up @@ -43,6 +44,7 @@ class ValueGenerator extends AbstractGenerator
public const TYPE_ARRAY_LONG = 'array_long';
public const TYPE_CONSTANT = 'constant';
public const TYPE_NULL = 'null';
public const TYPE_ENUM = 'enum';
public const TYPE_OBJECT = 'object';
public const TYPE_OTHER = 'other';
/**#@-*/
Expand Down Expand Up @@ -264,6 +266,7 @@ protected function getValidatedType($type)
self::TYPE_ARRAY_LONG,
self::TYPE_CONSTANT,
self::TYPE_NULL,
self::TYPE_ENUM,
self::TYPE_OBJECT,
self::TYPE_OTHER,
];
Expand Down Expand Up @@ -304,6 +307,10 @@ public function getAutoDeterminedType($value)
case 'NULL':
return self::TYPE_NULL;
case 'object':
if ($value instanceof UnitEnum) {
return self::TYPE_ENUM;
}
// enums are typed as objects, so this fall through is intentional
case 'resource':
case 'unknown type':
default:
Expand Down Expand Up @@ -418,6 +425,13 @@ public function generate()
}
$output .= $endArray;
break;
case self::TYPE_ENUM:
if (! is_object($value)) {
throw new Exception\RuntimeException('Value is not an object.');
}

$output = sprintf('%s::%s', get_class($value), (string) $value->name);
break;
case self::TYPE_OTHER:
default:
throw new Exception\RuntimeException(sprintf(
Expand Down
9 changes: 9 additions & 0 deletions test/Generator/TestAsset/TestEnum.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace LaminasTest\Code\Generator\TestAsset;

enum TestEnum
{
case Test1;
case Test2;
}
24 changes: 24 additions & 0 deletions test/Generator/ValueGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Laminas\Code\Generator\PropertyValueGenerator;
use Laminas\Code\Generator\ValueGenerator;
use Laminas\Stdlib\ArrayObject as StdlibArrayObject;
use LaminasTest\Code\Generator\TestAsset\TestEnum;
use PHPUnit\Framework\TestCase;

use function fopen;
Expand Down Expand Up @@ -388,6 +389,29 @@ public function testPropertyDefaultValueCanHandleBool()
self::assertNotEquals($valueGenerator1->generate(), $valueGenerator2->generate());
}

/** @requires PHP >= 8.1 */
public function testPropertyDefaultValueCanHandleEnums(): void
{
$valueGenerator1 = new ValueGenerator(
TestEnum::Test1,
ValueGenerator::TYPE_AUTO,
ValueGenerator::OUTPUT_MULTIPLE_LINE
);

$valueGenerator2 = new ValueGenerator(
TestEnum::Test2,
ValueGenerator::TYPE_ENUM,
ValueGenerator::OUTPUT_MULTIPLE_LINE
);

$valueGenerator1->initEnvironmentConstants();
$valueGenerator2->initEnvironmentConstants();

self::assertNotEquals($valueGenerator1->generate(), $valueGenerator2->generate());
self::assertEquals(sprintf('%s::%s', TestEnum::class, 'Test1'), $valueGenerator1->generate());
self::assertEquals(sprintf('%s::%s', TestEnum::class, 'Test2'), $valueGenerator2->generate());
}

/**
* @dataProvider simpleArray
* @param string $type
Expand Down

0 comments on commit bd480f6

Please sign in to comment.