Skip to content

Commit c31fe90

Browse files
committed
add UlidType and UuidType form types
1 parent 29b88af commit c31fe90

File tree

10 files changed

+529
-1
lines changed

10 files changed

+529
-1
lines changed

src/Symfony/Component/Form/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ CHANGELOG
1313
* Deprecated passing an array as the second argument of the `RadioListMapper::mapDataToForms()` method, pass `\Traversable` instead.
1414
* Deprecated passing an array as the first argument of the `RadioListMapper::mapFormsToData()` method, pass `\Traversable` instead.
1515
* Added a `choice_translation_parameters` option to `ChoiceType`
16+
* Add `UuidType` and `UlidType`
1617

1718
5.2.0
1819
-----
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Form\Extension\Core\DataTransformer;
13+
14+
use Symfony\Component\Form\DataTransformerInterface;
15+
use Symfony\Component\Form\Exception\TransformationFailedException;
16+
use Symfony\Component\Uid\Ulid;
17+
18+
/**
19+
* Transforms between a ULID string and a Ulid object.
20+
*
21+
* @author Pavel Dyakonov <wapinet@mail.ru>
22+
*/
23+
class UlidToStringTransformer implements DataTransformerInterface
24+
{
25+
/**
26+
* Transforms a Ulid object into a string.
27+
*
28+
* @param Ulid $value A Ulid object
29+
*
30+
* @return string A value as produced by Uid component
31+
*
32+
* @throws TransformationFailedException If the given value is not a Ulid object
33+
*/
34+
public function transform($value)
35+
{
36+
if (null === $value) {
37+
return '';
38+
}
39+
40+
if (!$value instanceof Ulid) {
41+
throw new TransformationFailedException('Expected a Ulid.');
42+
}
43+
44+
return (string) $value;
45+
}
46+
47+
/**
48+
* Transforms a ULID string into a Ulid object.
49+
*
50+
* @param string $value A ULID string
51+
*
52+
* @return Ulid|null An instance of Ulid
53+
*
54+
* @throws TransformationFailedException If the given value is not a string,
55+
* or could not be transformed
56+
*/
57+
public function reverseTransform($value)
58+
{
59+
if (null === $value || '' === $value) {
60+
return null;
61+
}
62+
63+
if (!\is_string($value)) {
64+
throw new TransformationFailedException('Expected a string.');
65+
}
66+
67+
try {
68+
$ulid = new Ulid($value);
69+
} catch (\InvalidArgumentException $e) {
70+
throw new TransformationFailedException(sprintf('The value "%s" is not a valid ULID.', $value), $e->getCode(), $e);
71+
}
72+
73+
return $ulid;
74+
}
75+
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Form\Extension\Core\DataTransformer;
13+
14+
use Symfony\Component\Form\DataTransformerInterface;
15+
use Symfony\Component\Form\Exception\TransformationFailedException;
16+
use Symfony\Component\Uid\Uuid;
17+
18+
/**
19+
* Transforms between a UUID string and a Uuid object.
20+
*
21+
* @author Pavel Dyakonov <wapinet@mail.ru>
22+
*/
23+
class UuidToStringTransformer implements DataTransformerInterface
24+
{
25+
/**
26+
* Transforms a Uuid object into a string.
27+
*
28+
* @param Uuid $value A Uuid object
29+
*
30+
* @return string A value as produced by Uid component
31+
*
32+
* @throws TransformationFailedException If the given value is not a Uuid object
33+
*/
34+
public function transform($value)
35+
{
36+
if (null === $value) {
37+
return '';
38+
}
39+
40+
if (!$value instanceof Uuid) {
41+
throw new TransformationFailedException('Expected a Uuid.');
42+
}
43+
44+
return (string) $value;
45+
}
46+
47+
/**
48+
* Transforms a UUID string into a Uuid object.
49+
*
50+
* @param string $value A UUID string
51+
*
52+
* @return Uuid|null An instance of Uuid
53+
*
54+
* @throws TransformationFailedException If the given value is not a string,
55+
* or could not be transformed
56+
*/
57+
public function reverseTransform($value)
58+
{
59+
if (null === $value || '' === $value) {
60+
return null;
61+
}
62+
63+
if (!\is_string($value)) {
64+
throw new TransformationFailedException('Expected a string.');
65+
}
66+
67+
try {
68+
$uuid = new Uuid($value);
69+
} catch (\InvalidArgumentException $e) {
70+
throw new TransformationFailedException(sprintf('The value "%s" is not a valid UUID.', $value), $e->getCode(), $e);
71+
}
72+
73+
return $uuid;
74+
}
75+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Form\Extension\Core\Type;
13+
14+
use Symfony\Component\Form\AbstractType;
15+
use Symfony\Component\Form\Extension\Core\DataTransformer\UlidToStringTransformer;
16+
use Symfony\Component\Form\FormBuilderInterface;
17+
use Symfony\Component\OptionsResolver\Options;
18+
use Symfony\Component\OptionsResolver\OptionsResolver;
19+
20+
/**
21+
* @author Pavel Dyakonov <wapinet@mail.ru>
22+
*/
23+
class UlidType extends AbstractType
24+
{
25+
/**
26+
* {@inheritdoc}
27+
*/
28+
public function buildForm(FormBuilderInterface $builder, array $options)
29+
{
30+
$builder
31+
->addViewTransformer(new UlidToStringTransformer())
32+
;
33+
}
34+
35+
/**
36+
* {@inheritdoc}
37+
*/
38+
public function configureOptions(OptionsResolver $resolver)
39+
{
40+
$resolver->setDefaults([
41+
'compound' => false,
42+
'invalid_message' => function (Options $options, $previousValue) {
43+
return ($options['legacy_error_messages'] ?? true)
44+
? $previousValue
45+
: 'Please enter a valid ULID.';
46+
},
47+
]);
48+
}
49+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Form\Extension\Core\Type;
13+
14+
use Symfony\Component\Form\AbstractType;
15+
use Symfony\Component\Form\Extension\Core\DataTransformer\UuidToStringTransformer;
16+
use Symfony\Component\Form\FormBuilderInterface;
17+
use Symfony\Component\OptionsResolver\Options;
18+
use Symfony\Component\OptionsResolver\OptionsResolver;
19+
20+
/**
21+
* @author Pavel Dyakonov <wapinet@mail.ru>
22+
*/
23+
class UuidType extends AbstractType
24+
{
25+
/**
26+
* {@inheritdoc}
27+
*/
28+
public function buildForm(FormBuilderInterface $builder, array $options)
29+
{
30+
$builder
31+
->addViewTransformer(new UuidToStringTransformer())
32+
;
33+
}
34+
35+
/**
36+
* {@inheritdoc}
37+
*/
38+
public function configureOptions(OptionsResolver $resolver)
39+
{
40+
$resolver->setDefaults([
41+
'compound' => false,
42+
'invalid_message' => function (Options $options, $previousValue) {
43+
return ($options['legacy_error_messages'] ?? true)
44+
? $previousValue
45+
: 'Please enter a valid UUID.';
46+
},
47+
]);
48+
}
49+
}
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Form\Tests\Extension\Core\DataTransformer;
13+
14+
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\Form\Exception\TransformationFailedException;
16+
use Symfony\Component\Form\Extension\Core\DataTransformer\UlidToStringTransformer;
17+
use Symfony\Component\Uid\Ulid;
18+
19+
class UlidToStringTransformerTest extends TestCase
20+
{
21+
public function provideValidUlid()
22+
{
23+
return [
24+
['01D85PP1982GF6KTVFHQ7W78FB', new Ulid('01d85pp1982gf6ktvfhq7w78fb')],
25+
];
26+
}
27+
28+
/**
29+
* @dataProvider provideValidUlid
30+
*/
31+
public function testTransform($output, $input)
32+
{
33+
$transformer = new UlidToStringTransformer();
34+
35+
$input = new Ulid($input);
36+
37+
$this->assertEquals($output, $transformer->transform($input));
38+
}
39+
40+
public function testTransformEmpty()
41+
{
42+
$transformer = new UlidToStringTransformer();
43+
44+
$this->assertSame('', $transformer->transform(null));
45+
}
46+
47+
public function testTransformExpectsUlid()
48+
{
49+
$transformer = new UlidToStringTransformer();
50+
51+
$this->expectException(TransformationFailedException::class);
52+
53+
$transformer->transform('1234');
54+
}
55+
56+
/**
57+
* @dataProvider provideValidUlid
58+
*/
59+
public function testReverseTransform($input, $output)
60+
{
61+
$reverseTransformer = new UlidToStringTransformer();
62+
63+
$output = new Ulid($output);
64+
65+
$this->assertEquals($output, $reverseTransformer->reverseTransform($input));
66+
}
67+
68+
public function testReverseTransformEmpty()
69+
{
70+
$reverseTransformer = new UlidToStringTransformer();
71+
72+
$this->assertNull($reverseTransformer->reverseTransform(''));
73+
}
74+
75+
public function testReverseTransformExpectsString()
76+
{
77+
$reverseTransformer = new UlidToStringTransformer();
78+
79+
$this->expectException(TransformationFailedException::class);
80+
81+
$reverseTransformer->reverseTransform(1234);
82+
}
83+
84+
public function testReverseTransformExpectsValidUlidString()
85+
{
86+
$reverseTransformer = new UlidToStringTransformer();
87+
88+
$this->expectException(TransformationFailedException::class);
89+
90+
$reverseTransformer->reverseTransform('1234');
91+
}
92+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy