-
-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ea73095
commit 9683e83
Showing
6 changed files
with
252 additions
and
0 deletions.
There are no files selected for viewing
84 changes: 84 additions & 0 deletions
84
src/Bridge/Symfony/Form/DataTransformer/ScalarToEnumTransformer.php
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,84 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the "elao/enum" package. | ||
* | ||
* Copyright (C) Elao | ||
* | ||
* @author Elao <contact@elao.com> | ||
*/ | ||
|
||
namespace Elao\Enum\Bridge\Symfony\Form\DataTransformer; | ||
|
||
use Elao\Enum\EnumInterface; | ||
use Elao\Enum\Exception\InvalidValueException; | ||
use Symfony\Component\Form\DataTransformerInterface; | ||
use Symfony\Component\Form\Exception\InvalidArgumentException; | ||
use Symfony\Component\Form\Exception\TransformationFailedException; | ||
|
||
class ScalarToEnumTransformer implements DataTransformerInterface | ||
{ | ||
/** @var string|EnumInterface */ | ||
private $enumClass; | ||
|
||
public function __construct(string $enumClass) | ||
{ | ||
if (!is_a($enumClass, EnumInterface::class, true)) { | ||
throw new InvalidArgumentException(sprintf( | ||
'"%s" is not an instance of "%s"', | ||
$enumClass, | ||
EnumInterface::class | ||
)); | ||
} | ||
|
||
$this->enumClass = $enumClass; | ||
} | ||
|
||
/** | ||
* Transforms EnumInterface object to a scalar value. | ||
* | ||
* @param EnumInterface $value EnumInterface instance | ||
* | ||
* @throws TransformationFailedException When the transformation fails | ||
* | ||
* @return int|string Value of EnumInterface | ||
*/ | ||
public function transform($value) | ||
{ | ||
if ($value === null) { | ||
return null; | ||
} | ||
|
||
if (!$value instanceof $this->enumClass) { | ||
throw new TransformationFailedException(sprintf( | ||
'Expected instance of "%s". Got "%s".', | ||
$this->enumClass, | ||
is_object($value) ? get_class($value) : gettype($value) | ||
)); | ||
} | ||
|
||
return $value->getValue(); | ||
} | ||
|
||
/** | ||
* Transforms scalar enum value to enumeration instance. | ||
* | ||
* @param int|string $value Value accepted by EnumInterface | ||
* | ||
* @throws TransformationFailedException When the transformation fails | ||
* | ||
* @return EnumInterface|null A single FlaggedEnum instance or null | ||
*/ | ||
public function reverseTransform($value) | ||
{ | ||
if ($value === null) { | ||
return null; | ||
} | ||
|
||
try { | ||
return $this->enumClass::get($value); | ||
} catch (InvalidValueException $exception) { | ||
throw new TransformationFailedException($exception->getMessage(), $exception->getCode(), $exception); | ||
} | ||
} | ||
} |
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
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
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
63 changes: 63 additions & 0 deletions
63
tests/Unit/Bridge/Symfony/Form/DataTransformer/ScalarToEnumTransformerTest.php
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,63 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the "elao/enum" package. | ||
* | ||
* Copyright (C) Elao | ||
* | ||
* @author Elao <contact@elao.com> | ||
*/ | ||
|
||
namespace Elao\Enum\Tests\Unit\Bridge\Symfony\Form\DataTransformer; | ||
|
||
use Elao\Enum\Bridge\Symfony\Form\DataTransformer\ScalarToEnumTransformer; | ||
use Elao\Enum\Tests\Fixtures\Enum\SimpleEnum; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class ScalarToEnumTransformerTest extends TestCase | ||
{ | ||
/** | ||
* @expectedException \Symfony\Component\Form\Exception\InvalidArgumentException | ||
* @expectedExceptionMessage "stdClass" is not an instance of "Elao\Enum\EnumInterface" | ||
*/ | ||
public function testThrowsExceptionIfNotEnumInstance() | ||
{ | ||
new ScalarToEnumTransformer(\stdClass::class); | ||
} | ||
|
||
public function testTransform() | ||
{ | ||
$transformer = new ScalarToEnumTransformer(SimpleEnum::class); | ||
|
||
$this->assertSame(SimpleEnum::FIRST, $transformer->transform(SimpleEnum::FIRST())); | ||
} | ||
|
||
/** | ||
* @expectedException \Symfony\Component\Form\Exception\TransformationFailedException | ||
* @expectedExceptionMessage Expected instance of "Elao\Enum\Tests\Fixtures\Enum\SimpleEnum". Got "string". | ||
*/ | ||
public function testTransformThrowsExceptionOnNonEnum() | ||
{ | ||
$transformer = new ScalarToEnumTransformer(SimpleEnum::class); | ||
|
||
$transformer->transform('foo'); | ||
} | ||
|
||
public function testReverseTransform() | ||
{ | ||
$transformer = new ScalarToEnumTransformer(SimpleEnum::class); | ||
|
||
$this->assertSame(SimpleEnum::FIRST(), $transformer->reverseTransform(SimpleEnum::FIRST)); | ||
} | ||
|
||
/** | ||
* @expectedException \Symfony\Component\Form\Exception\TransformationFailedException | ||
* @expectedExceptionMessage "1" is not an acceptable value for "Elao\Enum\Tests\Fixtures\Enum\SimpleEnum | ||
*/ | ||
public function testReverseTransformThrowsExceptionOnValueNotInEnum() | ||
{ | ||
$transformer = new ScalarToEnumTransformer(SimpleEnum::class); | ||
|
||
$transformer->reverseTransform('1'); | ||
} | ||
} |
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