diff --git a/src/Bridge/Symfony/Form/DataTransformer/ScalarToEnumTransformer.php b/src/Bridge/Symfony/Form/DataTransformer/ValueToEnumTransformer.php similarity index 86% rename from src/Bridge/Symfony/Form/DataTransformer/ScalarToEnumTransformer.php rename to src/Bridge/Symfony/Form/DataTransformer/ValueToEnumTransformer.php index 3e2602eb..5a6ee0c2 100644 --- a/src/Bridge/Symfony/Form/DataTransformer/ScalarToEnumTransformer.php +++ b/src/Bridge/Symfony/Form/DataTransformer/ValueToEnumTransformer.php @@ -16,7 +16,10 @@ use Symfony\Component\Form\Exception\InvalidArgumentException; use Symfony\Component\Form\Exception\TransformationFailedException; -class ScalarToEnumTransformer implements DataTransformerInterface +/** + * Enumerated values to enum instances transformer. + */ +class ValueToEnumTransformer implements DataTransformerInterface { /** @var string|EnumInterface */ private $enumClass; @@ -35,7 +38,7 @@ public function __construct(string $enumClass) } /** - * Transforms EnumInterface object to a scalar value. + * Transforms EnumInterface object to a raw enumerated value. * * @param EnumInterface $value EnumInterface instance * @@ -61,13 +64,13 @@ public function transform($value) } /** - * Transforms scalar enum value to enumeration instance. + * Transforms a raw enumerated value to an 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 + * @return EnumInterface|null A single EnumInterface instance or null */ public function reverseTransform($value) { diff --git a/src/Bridge/Symfony/Form/Type/EnumType.php b/src/Bridge/Symfony/Form/Type/EnumType.php index b5f75644..e3a5ab03 100644 --- a/src/Bridge/Symfony/Form/Type/EnumType.php +++ b/src/Bridge/Symfony/Form/Type/EnumType.php @@ -10,7 +10,7 @@ namespace Elao\Enum\Bridge\Symfony\Form\Type; -use Elao\Enum\Bridge\Symfony\Form\DataTransformer\ScalarToEnumTransformer; +use Elao\Enum\Bridge\Symfony\Form\DataTransformer\ValueToEnumTransformer; use Elao\Enum\EnumInterface; use Elao\Enum\ReadableEnumInterface; use Symfony\Component\Form\AbstractType; @@ -34,7 +34,7 @@ public function buildForm(FormBuilderInterface $builder, array $options) return; } - $transformer = new ScalarToEnumTransformer($options['enum_class']); + $transformer = new ValueToEnumTransformer($options['enum_class']); $options['as_value'] // Transform enum instances to values if choices were provided as instances diff --git a/tests/Fixtures/Integration/Symfony/TestBundle/Controller/FormTypeController.php b/tests/Fixtures/Integration/Symfony/TestBundle/Controller/FormTypeController.php index bf820f1e..0892d872 100644 --- a/tests/Fixtures/Integration/Symfony/TestBundle/Controller/FormTypeController.php +++ b/tests/Fixtures/Integration/Symfony/TestBundle/Controller/FormTypeController.php @@ -10,7 +10,7 @@ namespace Elao\Enum\Tests\Fixtures\Integration\Symfony\TestBundle\Controller; -use Elao\Enum\Bridge\Symfony\Form\DataTransformer\ScalarToEnumTransformer; +use Elao\Enum\Bridge\Symfony\Form\DataTransformer\ValueToEnumTransformer; use Elao\Enum\Bridge\Symfony\Form\Type\EnumType; use Elao\Enum\Bridge\Symfony\Form\Type\FlaggedEnumType; use Elao\Enum\Tests\Fixtures\Enum\Gender; @@ -62,7 +62,7 @@ public function flaggedEnumFormAction(Request $request) ]); } - public function enumFormUtilizingScalarTransformerAction(Request $request) + public function valueToEnumTransformerChoiceFormAction(Request $request) { $data = [ 'gender' => Gender::get(Gender::MALE), @@ -81,8 +81,8 @@ public function enumFormUtilizingScalarTransformerAction(Request $request) ->add('submit', SubmitType::class) ; - $builder->get('gender')->addModelTransformer(new ScalarToEnumTransformer(Gender::class)); - $builder->get('simpleEnum')->addModelTransformer(new ScalarToEnumTransformer(SimpleEnum::class)); + $builder->get('gender')->addModelTransformer(new ValueToEnumTransformer(Gender::class)); + $builder->get('simpleEnum')->addModelTransformer(new ValueToEnumTransformer(SimpleEnum::class)); $form = $builder->getForm(); @@ -93,7 +93,7 @@ public function enumFormUtilizingScalarTransformerAction(Request $request) ]); } - public function choicesAsEnumValuesFormAction(Request $request) + public function choicesAsEnumValuesEnumFormAction(Request $request) { $data = [ 'gender' => Gender::get(Gender::MALE), diff --git a/tests/Fixtures/Integration/Symfony/app/config/routing.yml b/tests/Fixtures/Integration/Symfony/app/config/routing.yml index 8db59aba..f5bc6abc 100644 --- a/tests/Fixtures/Integration/Symfony/app/config/routing.yml +++ b/tests/Fixtures/Integration/Symfony/app/config/routing.yml @@ -6,10 +6,10 @@ flagged_enum_form: path: '/form-type/flagged-enum' defaults: { _controller: 'TestBundle:FormType:flaggedEnumForm' } -form_with_scalar_transformer: - path: '/form-type/scalar-transformer-enum' - defaults: { _controller: 'TestBundle:FormType:enumFormUtilizingScalarTransformer' } +value_to_enum_transformer_choice_form: + path: '/form-type/value-to-enum-transformer-choice-form' + defaults: { _controller: 'TestBundle:FormType:valueToEnumTransformerChoiceForm' } -choices_as_enum_values_form: - path: '/form-type/choices-as-enum-values-form' - defaults: { _controller: 'TestBundle:FormType:choicesAsEnumValuesForm' } +choices_as_enum_values_enum_form: + path: '/form-type/choices-as-enum-values-enum-form' + defaults: { _controller: 'TestBundle:FormType:choicesAsEnumValuesEnumForm' } diff --git a/tests/Integration/Bridge/Symfony/Form/EnumTypeTest.php b/tests/Integration/Bridge/Symfony/Form/EnumTypeTest.php index 49e5d88b..7c0f2663 100644 --- a/tests/Integration/Bridge/Symfony/Form/EnumTypeTest.php +++ b/tests/Integration/Bridge/Symfony/Form/EnumTypeTest.php @@ -89,8 +89,8 @@ public function testFlaggedEnumForm() public function provideFormWithChoicesAsEnumValuesUrls() { - yield 'ChoiceType with scalar to enum transformer' => ['/form-type/scalar-transformer-enum']; - yield 'EnumType with choices_as_enum_values' => ['/form-type/choices-as-enum-values-form']; + yield 'ChoiceType with value to enum transformer' => ['/form-type/value-to-enum-transformer-choice-form']; + yield 'EnumType with choices_as_enum_values' => ['/form-type/choices-as-enum-values-enum-form']; } /** diff --git a/tests/Unit/Bridge/Symfony/Form/DataTransformer/ScalarToEnumTransformerTest.php b/tests/Unit/Bridge/Symfony/Form/DataTransformer/ValueToEnumTransformerTest.php similarity index 76% rename from tests/Unit/Bridge/Symfony/Form/DataTransformer/ScalarToEnumTransformerTest.php rename to tests/Unit/Bridge/Symfony/Form/DataTransformer/ValueToEnumTransformerTest.php index d5c42b78..6768a5f0 100644 --- a/tests/Unit/Bridge/Symfony/Form/DataTransformer/ScalarToEnumTransformerTest.php +++ b/tests/Unit/Bridge/Symfony/Form/DataTransformer/ValueToEnumTransformerTest.php @@ -10,11 +10,11 @@ namespace Elao\Enum\Tests\Unit\Bridge\Symfony\Form\DataTransformer; -use Elao\Enum\Bridge\Symfony\Form\DataTransformer\ScalarToEnumTransformer; +use Elao\Enum\Bridge\Symfony\Form\DataTransformer\ValueToEnumTransformer; use Elao\Enum\Tests\Fixtures\Enum\SimpleEnum; use PHPUnit\Framework\TestCase; -class ScalarToEnumTransformerTest extends TestCase +class ValueToEnumTransformerTest extends TestCase { /** * @expectedException \Symfony\Component\Form\Exception\InvalidArgumentException @@ -22,12 +22,12 @@ class ScalarToEnumTransformerTest extends TestCase */ public function testThrowsExceptionIfNotEnumInstance() { - new ScalarToEnumTransformer(\stdClass::class); + new ValueToEnumTransformer(\stdClass::class); } public function testTransform() { - $transformer = new ScalarToEnumTransformer(SimpleEnum::class); + $transformer = new ValueToEnumTransformer(SimpleEnum::class); $this->assertSame(SimpleEnum::FIRST, $transformer->transform(SimpleEnum::FIRST())); } @@ -38,14 +38,14 @@ public function testTransform() */ public function testTransformThrowsExceptionOnNonEnum() { - $transformer = new ScalarToEnumTransformer(SimpleEnum::class); + $transformer = new ValueToEnumTransformer(SimpleEnum::class); $transformer->transform('foo'); } public function testReverseTransform() { - $transformer = new ScalarToEnumTransformer(SimpleEnum::class); + $transformer = new ValueToEnumTransformer(SimpleEnum::class); $this->assertSame(SimpleEnum::FIRST(), $transformer->reverseTransform(SimpleEnum::FIRST)); } @@ -56,7 +56,7 @@ public function testReverseTransform() */ public function testReverseTransformThrowsExceptionOnValueNotInEnum() { - $transformer = new ScalarToEnumTransformer(SimpleEnum::class); + $transformer = new ValueToEnumTransformer(SimpleEnum::class); $transformer->reverseTransform('1'); } diff --git a/tests/Unit/Bridge/Symfony/Form/Type/EnumTypeTest.php b/tests/Unit/Bridge/Symfony/Form/Type/EnumTypeTest.php index ea5ddd3f..9b867d03 100644 --- a/tests/Unit/Bridge/Symfony/Form/Type/EnumTypeTest.php +++ b/tests/Unit/Bridge/Symfony/Form/Type/EnumTypeTest.php @@ -10,7 +10,7 @@ namespace Elao\Enum\Tests\Unit\Bridge\Symfony\Form\Type; -use Elao\Enum\Bridge\Symfony\Form\DataTransformer\ScalarToEnumTransformer; +use Elao\Enum\Bridge\Symfony\Form\DataTransformer\ValueToEnumTransformer; use Elao\Enum\Bridge\Symfony\Form\Type\EnumType; use Elao\Enum\Tests\Fixtures\Enum\Gender; use Elao\Enum\Tests\Fixtures\Enum\SimpleEnum; @@ -465,7 +465,7 @@ public function provideFormWithChoicesAsEnumValues() )->getForm(); }]; - yield 'ChoiceType with scalar to enum transformer' => [function (FormFactoryInterface $factory): FormInterface { + yield 'ChoiceType with value to enum transformer' => [function (FormFactoryInterface $factory): FormInterface { $options = interface_exists(ChoiceListInterface::class) ? ['choices_as_values' => true] : []; return $factory->createBuilder( @@ -475,7 +475,7 @@ public function provideFormWithChoicesAsEnumValues() 'choices' => ['maleLabel' => Gender::MALE, 'femaleLabel' => Gender::FEMALE], ] + $options ) - ->addModelTransformer(new ScalarToEnumTransformer(Gender::class)) + ->addModelTransformer(new ValueToEnumTransformer(Gender::class)) ->getForm() ; }];