-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support editable mode on FieldDescriptionInterface::TYPE_ENUM
- Loading branch information
Showing
7 changed files
with
179 additions
and
1 deletion.
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
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,73 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of the Sonata Project package. | ||
* | ||
* (c) Thomas Rabaix <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Sonata\AdminBundle\Form\DataTransformer; | ||
|
||
use Symfony\Component\Form\DataTransformerInterface; | ||
use Symfony\Component\Form\Exception\TransformationFailedException; | ||
use Symfony\Component\Form\Exception\UnexpectedTypeException; | ||
|
||
/** | ||
* @phpstan-template T of \BackedEnum | ||
* @phpstan-implements DataTransformerInterface<T, int|string> | ||
*/ | ||
final class BackedEnumTransformer implements DataTransformerInterface | ||
{ | ||
/** | ||
* @phpstan-param class-string<T> $className | ||
*/ | ||
public function __construct( | ||
private string $className, | ||
) { | ||
} | ||
|
||
/** | ||
* @param int|string|null $value | ||
* | ||
* @phpstan-return T|null | ||
*/ | ||
public function reverseTransform($value): ?\BackedEnum | ||
{ | ||
if (null === $value || '' === $value) { | ||
return null; | ||
} | ||
|
||
if (!\is_int($value) && !\is_string($value)) { | ||
throw new TransformationFailedException(\sprintf('Could not transform value: expecting an int or string, got "%s".', get_debug_type($value))); | ||
} | ||
|
||
try { | ||
return $this->className::from($value); | ||
} catch (\ValueError|\TypeError $e) { | ||
throw new TransformationFailedException(\sprintf('Could not transform value "%s".', $value)); | ||
} | ||
} | ||
|
||
/** | ||
* @param \BackedEnum|null $value | ||
* | ||
* @phpstan-param T|null $value | ||
*/ | ||
public function transform($value): string|int|null | ||
{ | ||
if (null === $value) { | ||
return null; | ||
} | ||
|
||
if (!$value instanceof \BackedEnum) { | ||
throw new UnexpectedTypeException($value, \BackedEnum::class); | ||
} | ||
|
||
return $value->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
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
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,69 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of the Sonata Project package. | ||
* | ||
* (c) Thomas Rabaix <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Sonata\AdminBundle\Tests\Form\DataTransformer; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Sonata\AdminBundle\Form\DataTransformer\BackedEnumTransformer; | ||
use Sonata\AdminBundle\Tests\Fixtures\Enum\Suit; | ||
use Symfony\Component\Form\Exception\TransformationFailedException; | ||
use Symfony\Component\Form\Exception\UnexpectedTypeException; | ||
|
||
final class BackedEnumTransformerTest extends TestCase | ||
{ | ||
public function testReverseTransform(): void | ||
{ | ||
$transformer = new BackedEnumTransformer(Suit::class); | ||
|
||
static::assertNull($transformer->reverseTransform(null)); | ||
static::assertNull($transformer->reverseTransform('')); | ||
static::assertSame(Suit::Hearts, $transformer->reverseTransform(Suit::Hearts->value)); | ||
} | ||
|
||
public function testReverseTransformNotValidValue(): void | ||
{ | ||
$this->expectException(TransformationFailedException::class); | ||
$this->expectExceptionMessage('Could not transform value "not_valid_value".'); | ||
|
||
$transformer = new BackedEnumTransformer(Suit::class); | ||
$transformer->reverseTransform('not_valid_value'); | ||
} | ||
|
||
public function testReverseTransformNotScalar(): void | ||
{ | ||
$this->expectException(TransformationFailedException::class); | ||
$this->expectExceptionMessage('Could not transform value: expecting an int or string, got "stdClass".'); | ||
|
||
$transformer = new BackedEnumTransformer(Suit::class); | ||
// @phpstan-ignore-next-line | ||
$transformer->reverseTransform(new \stdClass()); | ||
Check failure on line 49 in tests/Form/DataTransformer/BackedEnumTransformerTest.php GitHub Actions / PsalmInvalidArgument
|
||
} | ||
|
||
public function testTransform(): void | ||
{ | ||
$transformer = new BackedEnumTransformer(Suit::class); | ||
|
||
static::assertNull($transformer->transform(null)); | ||
static::assertSame(Suit::Clubs->value, $transformer->transform(Suit::Clubs)); | ||
} | ||
|
||
public function testTransformUnexpectedType(): void | ||
{ | ||
$this->expectException(UnexpectedTypeException::class); | ||
$this->expectExceptionMessage('Expected argument of type "BackedEnum", "stdClass" given'); | ||
|
||
$transformer = new BackedEnumTransformer(Suit::class); | ||
// @phpstan-ignore-next-line | ||
$transformer->transform(new \stdClass()); | ||
Check failure on line 67 in tests/Form/DataTransformer/BackedEnumTransformerTest.php GitHub Actions / PsalmInvalidArgument
|
||
} | ||
} |