Skip to content

Commit

Permalink
minor #56 Rename ScalarToEnumTransformer to ValueToEnumTransformer (o…
Browse files Browse the repository at this point in the history
…gizanagi)

This PR was merged into the 1.x-dev branch.

Discussion
----------

Rename ScalarToEnumTransformer to ValueToEnumTransformer

Which is more aligned with current wording.

Commits
-------

6445845 Rename ScalarToEnumTransformer to ValueToEnumTransformer
  • Loading branch information
ogizanagi committed Apr 19, 2018
2 parents f34537f + 6445845 commit 2863118
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
*
Expand All @@ -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)
{
Expand Down
4 changes: 2 additions & 2 deletions src/Bridge/Symfony/Form/Type/EnumType.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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),
Expand All @@ -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();

Expand All @@ -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),
Expand Down
12 changes: 6 additions & 6 deletions tests/Fixtures/Integration/Symfony/app/config/routing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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' }
4 changes: 2 additions & 2 deletions tests/Integration/Bridge/Symfony/Form/EnumTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'];
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,24 @@

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
* @expectedExceptionMessage "stdClass" is not an instance of "Elao\Enum\EnumInterface"
*/
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()));
}
Expand All @@ -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));
}
Expand All @@ -56,7 +56,7 @@ public function testReverseTransform()
*/
public function testReverseTransformThrowsExceptionOnValueNotInEnum()
{
$transformer = new ScalarToEnumTransformer(SimpleEnum::class);
$transformer = new ValueToEnumTransformer(SimpleEnum::class);

$transformer->reverseTransform('1');
}
Expand Down
6 changes: 3 additions & 3 deletions tests/Unit/Bridge/Symfony/Form/Type/EnumTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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(
Expand All @@ -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()
;
}];
Expand Down

0 comments on commit 2863118

Please sign in to comment.