Skip to content

Commit 3e1b465

Browse files
committed
[make:validator] generate final classes
- adds generated file comparison to test suite
1 parent 58f8b30 commit 3e1b465

File tree

6 files changed

+84
-17
lines changed

6 files changed

+84
-17
lines changed

src/Maker/MakeValidator.php

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,12 @@
1616
use Symfony\Bundle\MakerBundle\Generator;
1717
use Symfony\Bundle\MakerBundle\InputConfiguration;
1818
use Symfony\Bundle\MakerBundle\Str;
19+
use Symfony\Bundle\MakerBundle\Util\ClassSource\Model\ClassData;
1920
use Symfony\Component\Console\Command\Command;
2021
use Symfony\Component\Console\Input\InputArgument;
2122
use Symfony\Component\Console\Input\InputInterface;
23+
use Symfony\Component\Validator\Constraint;
24+
use Symfony\Component\Validator\ConstraintValidator;
2225
use Symfony\Component\Validator\Validation;
2326

2427
/**
@@ -49,26 +52,35 @@ public function configureCommand(Command $command, InputConfiguration $inputConf
4952
/** @return void */
5053
public function generate(InputInterface $input, ConsoleStyle $io, Generator $generator)
5154
{
52-
$validatorClassNameDetails = $generator->createClassNameDetails(
53-
$input->getArgument('name'),
54-
'Validator\\',
55-
'Validator'
55+
$validatorClassData = ClassData::create(
56+
class: \sprintf('Validator\\%s', $input->getArgument('name')),
57+
suffix: 'Validator',
58+
extendsClass: ConstraintValidator::class,
59+
useStatements: [
60+
Constraint::class,
61+
],
5662
);
5763

58-
$constraintFullClassName = Str::removeSuffix($validatorClassNameDetails->getFullName(), 'Validator');
64+
$constraintDataClass = ClassData::create(
65+
class: \sprintf('Validator\\%s', Str::removeSuffix($validatorClassData->getClassName(), 'Validator')),
66+
extendsClass: Constraint::class,
67+
);
5968

6069
$generator->generateClass(
61-
$validatorClassNameDetails->getFullName(),
70+
$validatorClassData->getFullClassName(),
6271
'validator/Validator.tpl.php',
6372
[
64-
'constraint_class_name' => Str::getShortClassName($constraintFullClassName),
73+
'class_data' => $validatorClassData,
74+
'constraint_class_name' => $constraintDataClass->getClassName(),
6575
]
6676
);
6777

6878
$generator->generateClass(
69-
$constraintFullClassName,
79+
$constraintDataClass->getFullClassName(),
7080
'validator/Constraint.tpl.php',
71-
[]
81+
[
82+
'class_data' => $constraintDataClass,
83+
]
7284
);
7385

7486
$generator->writeChanges();

src/Resources/skeleton/validator/Constraint.tpl.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
<?= "<?php\n" ?>
22

3-
namespace <?= $namespace; ?>;
3+
namespace <?= $class_data->getNamespace(); ?>;
44

5-
use Symfony\Component\Validator\Constraint;
5+
<?= $class_data->getUseStatements(); ?>
66

77
#[\Attribute(\Attribute::TARGET_PROPERTY | \Attribute::TARGET_METHOD | \Attribute::IS_REPEATABLE)]
8-
class <?= $class_name ?> extends Constraint
8+
<?= $class_data->getClassDeclaration(); ?>
99
{
1010
public string $message = 'The string "{{ string }}" contains an illegal character: it can only contain letters or numbers.';
1111

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
<?= "<?php\n" ?>
22

3-
namespace <?= $namespace; ?>;
3+
namespace <?= $class_data->getNamespace(); ?>;
44

5-
use Symfony\Component\Validator\Constraint;
6-
use Symfony\Component\Validator\ConstraintValidator;
5+
<?= $class_data->getUseStatements(); ?>
76

8-
class <?= $class_name ?> extends ConstraintValidator
7+
<?= $class_data->getClassDeclaration(); ?>
98
{
109
public function validate(mixed $value, Constraint $constraint): void
1110
{
@@ -18,6 +17,7 @@ public function validate(mixed $value, Constraint $constraint): void
1817
// TODO: implement the validation here
1918
$this->context->buildViolation($constraint->message)
2019
->setParameter('{{ value }}', $value)
21-
->addViolation();
20+
->addViolation()
21+
;
2222
}
2323
}

tests/Maker/MakeValidatorTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,18 @@ public function getTestDetails(): \Generator
3232
'FooBar',
3333
]
3434
);
35+
36+
// Validator
37+
$expectedVoterPath = \dirname(__DIR__).'/fixtures/make-validator/expected/FooBarValidator.php';
38+
$generatedVoter = $runner->getPath('src/Validator/FooBarValidator.php');
39+
40+
self::assertSame(file_get_contents($expectedVoterPath), file_get_contents($generatedVoter));
41+
42+
// Constraint
43+
$expectedVoterPath = \dirname(__DIR__).'/fixtures/make-validator/expected/FooBar.php';
44+
$generatedVoter = $runner->getPath('src/Validator/FooBar.php');
45+
46+
self::assertSame(file_get_contents($expectedVoterPath), file_get_contents($generatedVoter));
3547
}),
3648
];
3749
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespace App\Validator;
4+
5+
use Symfony\Component\Validator\Constraint;
6+
7+
/**
8+
* @Annotation
9+
* @Target({"PROPERTY", "METHOD", "ANNOTATION"})
10+
*/
11+
#[\Attribute(\Attribute::TARGET_PROPERTY | \Attribute::TARGET_METHOD | \Attribute::IS_REPEATABLE)]
12+
final class FooBar extends Constraint
13+
{
14+
/*
15+
* Any public properties become valid options for the annotation.
16+
* Then, use these in your validator class.
17+
*/
18+
public string $message = 'The value "{{ value }}" is not valid.';
19+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
namespace App\Validator;
4+
5+
use Symfony\Component\Validator\Constraint;
6+
use Symfony\Component\Validator\ConstraintValidator;
7+
8+
final class FooBarValidator extends ConstraintValidator
9+
{
10+
public function validate(mixed $value, Constraint $constraint): void
11+
{
12+
/* @var FooBar $constraint */
13+
14+
if (null === $value || '' === $value) {
15+
return;
16+
}
17+
18+
// TODO: implement the validation here
19+
$this->context->buildViolation($constraint->message)
20+
->setParameter('{{ value }}', $value)
21+
->addViolation()
22+
;
23+
}
24+
}

0 commit comments

Comments
 (0)