Skip to content

Commit

Permalink
Enums php81 (#201)
Browse files Browse the repository at this point in the history
* Enums marshalling for PHP 8.1

* Allow to be nullable

* Update docblock

* Suppress psalm warning for enums
  • Loading branch information
cv65kr authored Jun 29, 2022
1 parent 91eafd5 commit d248301
Show file tree
Hide file tree
Showing 5 changed files with 152 additions and 0 deletions.
62 changes: 62 additions & 0 deletions src/Internal/Marshaller/Type/EnumType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

/**
* This file is part of Temporal package.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Temporal\Internal\Marshaller\Type;

use Temporal\Internal\Marshaller\MarshallerInterface;

use function is_array;

class EnumType extends Type implements DetectableTypeInterface
{
private string $classFQCN;

public function __construct(MarshallerInterface $marshaller, string $class = null)
{
if (PHP_VERSION_ID < 80104) {
throw new \RuntimeException('Enums are not available in this version of PHP');
}

if ($class === null) {
throw new \RuntimeException('Enum is required');
}

$this->classFQCN = $class;
parent::__construct($marshaller);
}

public static function match(\ReflectionNamedType $type): bool
{
return $type->getName() === 'enum';
}

public function parse($value, $current)
{
if ($value === null) {
return null;
}

if (is_array($value)) {
$value = $value['name'];
}

return ($this->classFQCN)::from($value);
}

/**
* @psalm-suppress UndefinedDocblockClass
* @return \UnitEnum|null
*/
public function serialize($value)
{
return $value;
}
}
24 changes: 24 additions & 0 deletions tests/Unit/DTO/Enum/EnumDTO.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

/**
* This file is part of Temporal package.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Temporal\Tests\Unit\DTO\Enum;

use Temporal\Internal\Marshaller\Meta\Marshal;
use Temporal\Internal\Marshaller\Type\EnumType;

class EnumDTO
{
#[Marshal(name: 'simpleEnum', type: EnumType::class, of: SimpleEnum::class)]
public SimpleEnum $simpleEnum;

#[Marshal(name: 'scalarEnum', type: EnumType::class, of: ScalarEnum::class)]
public ScalarEnum $scalarEnum;
}
32 changes: 32 additions & 0 deletions tests/Unit/DTO/Enum/EnumTestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

/**
* This file is part of Temporal package.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Temporal\Tests\Unit\DTO\Enum;

use Temporal\Tests\Unit\DTO\DTOMarshallingTestCase;

class EnumTestCase extends DTOMarshallingTestCase
{
public function testMarshalling(): void
{
if (PHP_VERSION_ID < 80104) {
$this->markTestSkipped();
}

$dto = new EnumDTO();
$dto->simpleEnum = SimpleEnum::TEST;
$dto->scalarEnum = ScalarEnum::TESTED_ENUM;

$result = $this->marshal($dto);
$this->assertEquals($dto->simpleEnum, $result['simpleEnum']);
$this->assertEquals($dto->scalarEnum, $result['scalarEnum']);
}
}
17 changes: 17 additions & 0 deletions tests/Unit/DTO/Enum/ScalarEnum.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

/**
* This file is part of Temporal package.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Temporal\Tests\Unit\DTO\Enum;

enum ScalarEnum: string
{
case TESTED_ENUM = 'tested';
}
17 changes: 17 additions & 0 deletions tests/Unit/DTO/Enum/SimpleEnum.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

/**
* This file is part of Temporal package.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Temporal\Tests\Unit\DTO\Enum;

enum SimpleEnum
{
case TEST;
}

0 comments on commit d248301

Please sign in to comment.