Skip to content

Commit

Permalink
Merge pull request #166 from laminas/value_generator_enum_support
Browse files Browse the repository at this point in the history
Support generating properties, constants and parameter default values with ENUM references
  • Loading branch information
Ocramius authored Dec 8, 2022
2 parents 27f73dd + 793801d commit de371cb
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 1 deletion.
15 changes: 14 additions & 1 deletion 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 All @@ -23,7 +24,6 @@
use function sprintf;
use function str_contains;
use function str_repeat;
use function strpos;

class ValueGenerator extends AbstractGenerator
{
Expand All @@ -44,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 @@ -286,6 +287,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 @@ -326,6 +328,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 @@ -440,6 +446,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', $value::class, (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;
}
16 changes: 16 additions & 0 deletions test/Generator/ValueGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@
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;
use function sprintf;
use function str_replace;

/**
Expand Down Expand Up @@ -388,6 +390,20 @@ public function testPropertyDefaultValueCanHandleBool()
self::assertNotEquals($valueGenerator1->generate(), $valueGenerator2->generate());
}

public function testPropertyDefaultValueCanHandleEnums(): void
{
$valueGenerator1 = new ValueGenerator(
TestEnum::Test1,
ValueGenerator::TYPE_AUTO,
ValueGenerator::OUTPUT_MULTIPLE_LINE
);

$valueGenerator2 = new ValueGenerator(TestEnum::Test2);

self::assertSame('\LaminasTest\Code\Generator\TestAsset\TestEnum::Test1', $valueGenerator1->generate());
self::assertSame('\LaminasTest\Code\Generator\TestAsset\TestEnum::Test2', $valueGenerator2->generate());
}

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

0 comments on commit de371cb

Please sign in to comment.