8000 [Form][Uid] Add UlidType and UuidType form types by Gemorroj · Pull Request #39863 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Form][Uid] Add UlidType and UuidType form types #39863

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Symfony/Component/Form/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ CHANGELOG
* Deprecated passing an array as the second argument of the `RadioListMapper::mapDataToForms()` method, pass `\Traversable` instead.
* Deprecated passing an array as the first argument of the `RadioListMapper::mapFormsToData()` method, pass `\Traversable` instead.
* Added a `choice_translation_parameters` option to `ChoiceType`
* Add `UuidType` and `UlidType`

5.2.0
-----
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Form\Extension\Core\DataTransformer;

use Symfony\Component\Form\DataTransformerInterface;
use Symfony\Component\Form\Exception\TransformationFailedException;
use Symfony\Component\Uid\Ulid;

/**
* Transforms between a ULID string and a Ulid object.
*
* @author Pavel Dyakonov <wapinet@mail.ru>
*/
class UlidToStringTransformer implements DataTransformerInterface
{
/**
* Transforms a Ulid object into a string.
*
* @param Ulid $value A Ulid object
*
* @return string|null A value as produced by Uid component
*
* @throws TransformationFailedException If the given value is not a Ulid object
*/
public function transform($value)
{
if (null === $value) {
return null;
}

if (!$value instanceof Ulid) {
throw new TransformationFailedException('Expected a Ulid.');
}

return (string) $value;
}

/**
* Transforms a ULID string into a Ulid object.
*
* @param string $value A ULID string
*
* @return Ulid|null An instance of Ulid
*
* @throws TransformationFailedException If the given value is not a string,
* or could not be transformed
*/
public function reverseTransform($value)
{
if (null === $value || '' === $value) {
return null;
}

if (!\is_string($value)) {
throw new TransformationFailedException('Expected a string.');
}

try {
$ulid = new Ulid($value);
} catch (\InvalidArgumentException $e) {
throw new TransformationFailedException(sprintf('The value "%s" is not a valid ULID.', $value), $e->getCode(), $e);
}

return $ulid;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Form\Extension\Core\DataTransformer;

use Symfony\Component\Form\DataTransformerInterface;
use Symfony\Component\Form\Exception\TransformationFailedException;
use Symfony\Component\Uid\Uuid;

/**
* Transforms between a UUID string and a Uuid object.
*
* @author Pavel Dyakonov <wapinet@mail.ru>
*/
class UuidToStringTransformer implements DataTransformerInterface
{
/**
* Transforms a Uuid object into a string.
*
* @param Uuid $value A Uuid object
*
* @return string|null A value as produced by Uid component
*
* @throws TransformationFailedException If the given value is not a Uuid object
*/
public function transform($value)
{
if (null === $value) {
return null;
}

if (!$value instanceof Uuid) {
throw new TransformationFailedException('Expected a Uuid.');
}

return (string) $value;
}

/**
* Transforms a UUID string into a Uuid object.
*
* @param string $value A UUID string
*
* @return Uuid|null An instance of Uuid
*
* @throws TransformationFailedException If the given value is not a string,
* or could not be transformed
*/
public function reverseTransform($value)
{
if (null === $value || '' === $value) {
return null;
}

if (!\is_string($value)) {
throw new TransformationFailedException('Expected a string.');
}

try {
$uuid = new Uuid($value);
} catch (\InvalidArgumentException $e) {
throw new TransformationFailedException(sprintf('The value "%s" is not a valid UUID.', $value), $e->getCode(), $e);
}

return $uuid;
}
}
49 changes: 49 additions & 0 deletions src/Symfony/Component/Form/Extension/Core/Type/UlidType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Form\Extension\Core\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\DataTransformer\UlidToStringTransformer;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\Options;
use Symfony\Component\OptionsResolver\OptionsResolver;

/**
* @author Pavel Dyakonov <wapinet@mail.ru>
*/
class UlidType extends AbstractType
{
/**
* {@inheritdoc}
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->addViewTransformer(new UlidToStringTransformer())
;
}

/**
* {@inheritdoc}
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'compound' => false,
'invalid_message' => function (Options $options, $previousValue) {
return ($options['legacy_error_messages'] ?? true)
? $previousValue
: 'Please enter a valid ULID.';
},
]);
}
}
49 changes: 49 additions & 0 deletions src/Symfony/Component/Form/Extension/Core/Type/UuidType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Form\Extension\Core\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\DataTransformer\UuidToStringTransformer;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\Options;
use Symfony\Component\OptionsResolver\OptionsResolver;

/**
* @author Pavel Dyakonov <wapinet@mail.ru>
*/
class UuidType extends AbstractType
{
/**
* {@inheritdoc}
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->addViewTransformer(new UuidToStringTransformer())
;
}

/**
* {@inheritdoc}
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'compound' => false,
'invalid_message' => function (Options $options, $previousValue) {
return ($options['legacy_error_messages'] ?? true)
? $previousValue
: 'Please enter a valid UUID.';
},
]);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Form\Tests\Extension\Core\DataTransformer;

use PHPUnit\Framework\TestCase;
use Symfony\Component\Form\Exception\TransformationFailedException;
use Symfony\Component\Form\Extension\Core\DataTransformer\UlidToStringTransformer;
use Symfony\Component\Uid\Ulid;

class UlidToStringTransformerTest extends TestCase
{
public function provideValidUlid()
{
return [
['01D85PP1982GF6KTVFHQ7W78FB', new Ulid('01d85pp1982gf6ktvfhq7w78fb')],
];
}

/**
* @dataProvider provideValidUlid
*/
public function testTransform($output, $input)
{
$transformer = new UlidToStringTransformer();

$input = new Ulid($input);

$this->assertEquals($output, $transformer->transform($input));
}

public function testTransformEmpty()
{
$transformer = new UlidToStringTransformer();

$this->assertNull($transformer->transform(null));
}

public function testTransformExpectsUlid()
{
$transformer = new UlidToStringTransformer();

$this->expectException(TransformationFailedException::class);

$transformer->transform('1234');
}

/**
* @dataProvider provideValidUlid
*/
public function testReverseTransform($input, $output)
{
$reverseTransformer = new UlidToStringTransformer();

$output = new Ulid($output);

$this->assertEquals($output, $reverseTransformer->reverseTransform($input));
}

public function testReverseTransformEmpty()
{
$reverseTransformer = new UlidToStringTransformer();

$this->assertNull($reverseTransformer->reverseTransform(''));
}

public function testReverseTransformExpectsString()
{
$reverseTransformer = new UlidToStringTransformer();

$this->expectException(TransformationFailedException::class);

$reverseTransformer->reverseTransform(1234);
}

public function testReverseTransformExpectsValidUlidString()
{
$reverseTransformer = new UlidToStringTransformer();

$this->expectException(TransformationFailedException::class);

$reverseTransformer->reverseTransform('1234');
}
}
Loading
0