-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Enums marshalling for PHP 8.1 * Allow to be nullable * Update docblock * Suppress psalm warning for enums
- Loading branch information
Showing
5 changed files
with
152 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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']); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |