8000 [Validator] Migrate Type. · symfony/symfony@ea652da · GitHub
[go: up one dir, main page]

Skip to content

Commit ea652da

Browse files
committed
[Validator] Migrate Type.
1 parent 4e97ac2 commit ea652da

File tree

3 files changed

+87
-14
lines changed

3 files changed

+87
-14
lines changed

src/Symfony/Component/Validator/Constraints/Type.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
*
2020
* @author Bernhard Schussek <bschussek@gmail.com>
2121
*/
22+
#[\Attribute(\Attribute::TARGET_PROPERTY | \Attribute::TARGET_METHOD | \Attribute::IS_REPEATABLE)]
2223
class Type extends Constraint
2324
{
2425
const INVALID_TYPE_ERROR = 'ba785a8c-82cb-4283-967c-3cf342181b40';
@@ -30,6 +31,24 @@ class Type extends Constraint
3031
public $message = 'This value should be of type {{ type }}.';
3132
public $type;
3233

34+
/**
35+
* {@inheritdoc}
36+
*
37+
* @param string|array $type One ore multiple types to validate against or a set of options.
38+
*/
39+
public function __construct($type, string $message = null, array $groups = null, $payload = null, array $options = [])
40+
{
41+
if (\is_array($type) && \is_string(key($type))) {
42+
$options = array_merge($type, $options);
43+
} elseif (null !== $type) {
44+
$options['value'] = $type;
45+
}
46+
47+
parent::__construct($options, $groups, $payload);
48+
49+
$this->message = $message ?? $this->message;
50+
}
51+
3352
/**
3453
* {@inheritdoc}
3554
*/
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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\Validator\Tests\Constraints;
13+
14+
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\Validator\Constraints\Type;
16+
use Symfony\Component\Validator\Mapping\ClassMetadata;
17+
use Symfony\Component\Validator\Mapping\Loader\AnnotationLoader;
18+
19+
/**
20+
* @requires PHP 8
21+
*/
22+
class TypeTest extends TestCase
23+
{
24+
public function testAttributes()
25+
{
26+
$metadata = new ClassMetadata(TypeDummy::class);
27+
self::assertTrue((new AnnotationLoader())->loadClassMetadata($metadata));
28+
29+
list($aConstraint) = $metadata->properties['a']->getConstraints();
30+
self::assertSame('integer', $aConstraint->type);
31+
32+
list($bConstraint) = $metadata->properties['b']->getConstraints();
33+
self::assertSame(\DateTime::class, $bConstraint->type);
34+
self::assertSame('myMessage', $bConstraint->message);
35+
self::assertSame(['Default', 'TypeDummy'], $bConstraint->groups);
36+
37+
list($cConstraint) = $metadata->properties['c']->getConstraints();
38+
self::assertSame(['string', 'array'], $cConstraint->type);
39+
self::assertSame(['my_group'], $cConstraint->groups);
40+
self::assertSame('some attached data', $cConstraint->payload);
41+
}
42+
}
43+
44+
class TypeDummy
45+
{
46+
#[Type('integer')]
47+
private $a;
48+
49+
#[Type(type: \DateTime::class, message: 'myMessage')]
50+
private $b;
51+
52+
#[Type(type: ['string', 'array'], groups: ['my_group'], payload: 'some attached data')]
53+
private $c;
54+
}

src/Symfony/Component/Validator/Tests/Constraints/TypeValidatorTest.php

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -184,29 +184,29 @@ public function getValidValuesMultipleTypes()
184184
}
185185

186186
/**
187-
* @dataProvider getInvalidValuesMultipleTypes
187+
* @dataProvider provideConstraintsWithMultipleTypes
188188
*/
189-
public function testInvalidValuesMultipleTypes($value, $types, $valueAsString)
189+
public function testInvalidValuesMultipleTypes(Type $constraint)
190190
{
191-
$constraint = new Type([
192-
'type' => $types,
193-
'message' => 'myMessage',
194-
]);
195-
196-
$this->validator->validate($value, $constraint);
191+
$this->validator->validate('12345', $constraint);
197192

198193
$this->buildViolation('myMessage')
199-
->setParameter('{{ value }}', $valueAsString)
200-
->setParameter('{{ type }}', implode('|', $types))
194+
->setParameter('{{ value }}', '"12345"')
195+
->setParameter('{{ type }}', implode('|', ['boolean', 'array']))
201196
->setCode(Type::INVALID_TYPE_ERROR)
202197
->assertRaised();
203198
}
204199

205-
public function getInvalidValuesMultipleTypes()
200+
public function provideConstraintsWithMultipleTypes()
206201
{
207-
return [
208-
['12345', ['boolean', 'array'], '"12345"'],
209-
];
202+
yield 'Doctrine style' => [new Type([
203+
'type' => ['boolean', 'array'],
204+
'message' => 'myMessage',
205+
])];
206+
207+
if (\PHP_VERSION_ID >= 80000) {
208+
yield 'named arguments' => [eval('return new \Symfony\Component\Validator\Constraints\Type(type: ["boolean", "array"], message: "myMessage");')];
209+
}
210210
}
211211

212212
protected function createFile()

0 commit comments

Comments
 (0)
0