-
-
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.
- Loading branch information
Showing
11 changed files
with
216 additions
and
5 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
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,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) { | ||
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
Large diffs are not rendered by default.
Oops, something went wrong.
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,78 @@ | ||
<?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; | ||
|
||
/** | ||
* @requires PHP 8.1 | ||
*/ | ||
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'); | ||
} | ||
|
||
/** | ||
* @psalm-suppress InvalidArgument | ||
*/ | ||
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()); | ||
} | ||
|
||
public function testTransform(): void | ||
{ | ||
$transformer = new BackedEnumTransformer(Suit::class); | ||
|
||
static::assertNull($transformer->transform(null)); | ||
static::assertSame(Suit::Clubs->value, $transformer->transform(Suit::Clubs)); | ||
} | ||
|
||
/** | ||
* @psalm-suppress InvalidArgument | ||
*/ | ||
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()); | ||
} | ||
} |
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