Skip to content

Commit 066e8ce

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

File tree

9 files changed

+541
-0
lines changed

9 files changed

+541
-0
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 an ULID string and an Ulid object.
20+
*
21+
* @author Pavel Dyakonov <wapinet@mail.ru>
22+
*/
23+
class UlidToStringTransformer implements DataTransformerInterface
24+
{
25+
/**
26+
* Transforms an Ulid object into a string.
27+
*
28+
* @param Ulid $value An Ulid object
29+
*
30+
* @return string A value as produced by Uid component
31+
*
32+
* @throws TransformationFailedException If the given value is not an 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 an Ulid.');
42+
}
43+
44+
return (string) $value;
45+
}
46+
47+
/**
48+
* Transforms an ULID string into an Ulid object.
49+
*
50+
* @param string $value An 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 (empty($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 (\Exception $e) {
70+
throw new TransformationFailedException($e->getMessage(), $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 an UUID string and an Uuid object.
20+
*
21+
* @author Pavel Dyakonov <wapinet@mail.ru>
22+
*/
23+
class UuidToStringTransformer implements DataTransformerInterface
24+
{
25+
/**
26+
* Transforms an Uuid object into a string.
27+
*
28+
* @param Uuid $value An Uuid object
29+
*
30+
* @return string A value as produced by Uid component
31+
*
32+
* @throws TransformationFailedException If the given value is not an 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 an Uuid.');
42+
}
43+
44+
return (string) $value;
45+
}
46+
47+
/**
48+
* Transforms an UUID string into an Uuid object.
49+
*
50+
* @param string $value An 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 (empty($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 (\Exception $e) {
70+
throw new TransformationFailedException($e->getMessage(), $e->getCode(), $e);
71+
}
72+
73+
return $uuid;
74+
}
75+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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+
50+
/**
51+
* {@inheritdoc}
52+
*/
53+
public function getBlockPrefix()
54+
{
55+
return 'ulid';
56+
}
57+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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+
50+
/**
51+
* {@inheritdoc}
52+
*/
53+
public function getBlockPrefix()
54+
{
55+
return 'uuid';
56+
}
57+
}
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
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\Extension\Core\DataTransformer\UlidToStringTransformer;
16+
use Symfony\Component\Uid\Ulid;
17+
18+
class UlidToStringTransformerTest extends TestCase
19+
{
20+
public function dataProvider()
21+
{
22+
return [
23+
['01D85PP1982GF6KTVFHQ7W78FB', new Ulid('01d85pp1982gf6ktvfhq7w78fb')],
24+
];
25+
}
26+
27+
/**
28+
* @dataProvider dataProvider
29+
*/
30+
public function testTransform($output, $input)
31+
{
32+
$transformer = new UlidToStringTransformer();
33+
34+
$input = new Ulid($input);
35+
36+
$this->assertEquals($output, $transformer->transform($input));
37+
}
38+
39+
public function testTransformEmpty()
40+
{
41+
$transformer = new UlidToStringTransformer();
42+
43+
$this->assertSame('', $transformer->transform(null));
44+
}
45+
46+
public function testTransformExpectsUlid()
47+
{
48+
$transformer = new UlidToStringTransformer();
49+
50+
$this->expectException(\Symfony\Component\Form\Exception\TransformationFailedException::class);
51+
52+
$transformer->transform('1234');
53+
}
54+
55+
/**
56+
* @dataProvider dataProvider
57+
*/
58+
public function testReverseTransform($input, $output)
59+
{
60+
$reverseTransformer = new UlidToStringTransformer();
61+
62+
$output = new Ulid($output);
63+
64+
$this->assertEquals($output, $reverseTransformer->reverseTransform($input));
65+
}
66+
67+
public function testReverseTransformEmpty()
68+
{
69+
$reverseTransformer = new UlidToStringTransformer();
70+
71+
$this->assertNull($reverseTransformer->reverseTransform(''));
72+
}
73+
74+
public function testReverseTransformExpectsString()
75+
{
76+
$reverseTransformer = new UlidToStringTransformer();
77+
78+
$this->expectException(\Symfony\Component\Form\Exception\TransformationFailedException::class);
79+
80+
$reverseTransformer->reverseTransform(1234);
81+
}
82+
83+
public function testReverseTransformExpectsValidUlidString()
84+
{
85+
$reverseTransformer = new UlidToStringTransformer();
86+
87+
$this->expectException(\Symfony\Component\Form\Exception\TransformationFailedException::class);
88+
89+
$reverseTransformer->reverseTransform('1234');
90+
}
91+
}

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