From 5789965374fc8052fd7448cfb0024250a1cfc820 Mon Sep 17 00:00:00 2001 From: Wouter de Jong Date: Mon, 19 May 2025 10:27:13 +0200 Subject: [PATCH] Revert Slug constraint This commit reverts #58542. --- src/Symfony/Component/Validator/CHANGELOG.md | 1 - .../Component/Validator/Constraints/Slug.php | 42 ------- .../Validator/Constraints/SlugValidator.php | 47 ------- .../Validator/Tests/Constraints/SlugTest.php | 47 ------- .../Tests/Constraints/SlugValidatorTest.php | 117 ------------------ 5 files changed, 254 deletions(-) delete mode 100644 src/Symfony/Component/Validator/Constraints/Slug.php delete mode 100644 src/Symfony/Component/Validator/Constraints/SlugValidator.php delete mode 100644 src/Symfony/Component/Validator/Tests/Constraints/SlugTest.php delete mode 100644 src/Symfony/Component/Validator/Tests/Constraints/SlugValidatorTest.php diff --git a/src/Symfony/Component/Validator/CHANGELOG.md b/src/Symfony/Component/Validator/CHANGELOG.md index ae1ae20da804d..a7363d7f59c19 100644 --- a/src/Symfony/Component/Validator/CHANGELOG.md +++ b/src/Symfony/Component/Validator/CHANGELOG.md @@ -8,7 +8,6 @@ CHANGELOG * Deprecate defining custom constraints not supporting named arguments * Deprecate passing an array of options to the constructors of the constraint classes, pass each option as a dedicated argument instead * Add support for ratio checks for SVG files to the `Image` constraint - * Add the `Slug` constraint * Add support for the `otherwise` option in the `When` constraint * Add support for multiple fields containing nested constraints in `Composite` constraints * Add the `stopOnFirstError` option to the `Unique` constraint to validate all elements diff --git a/src/Symfony/Component/Validator/Constraints/Slug.php b/src/Symfony/Component/Validator/Constraints/Slug.php deleted file mode 100644 index 55d847ccb618b..0000000000000 --- a/src/Symfony/Component/Validator/Constraints/Slug.php +++ /dev/null @@ -1,42 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Symfony\Component\Validator\Constraints; - -use Symfony\Component\Validator\Attribute\HasNamedArguments; -use Symfony\Component\Validator\Constraint; - -/** - * Validates that a value is a valid slug. - * - * @author Raffaele Carelle - */ -#[\Attribute(\Attribute::TARGET_PROPERTY | \Attribute::TARGET_METHOD | \Attribute::IS_REPEATABLE)] -class Slug extends Constraint -{ - public const NOT_SLUG_ERROR = '14e6df1e-c8ab-4395-b6ce-04b132a3765e'; - - public string $message = 'This value is not a valid slug.'; - public string $regex = '/^[a-z0-9]+(?:-[a-z0-9]+)*$/i'; - - #[HasNamedArguments] - public function __construct( - ?string $regex = null, - ?string $message = null, - ?array $groups = null, - mixed $payload = null, - ) { - parent::__construct([], $groups, $payload); - - $this->message = $message ?? $this->message; - $this->regex = $regex ?? $this->regex; - } -} diff --git a/src/Symfony/Component/Validator/Constraints/SlugValidator.php b/src/Symfony/Component/Validator/Constraints/SlugValidator.php deleted file mode 100644 index b914cad31b466..0000000000000 --- a/src/Symfony/Component/Validator/Constraints/SlugValidator.php +++ /dev/null @@ -1,47 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Symfony\Component\Validator\Constraints; - -use Symfony\Component\Validator\Constraint; -use Symfony\Component\Validator\ConstraintValidator; -use Symfony\Component\Validator\Exception\UnexpectedTypeException; -use Symfony\Component\Validator\Exception\UnexpectedValueException; - -/** - * @author Raffaele Carelle - */ -class SlugValidator extends ConstraintValidator -{ - public function validate(mixed $value, Constraint $constraint): void - { - if (!$constraint instanceof Slug) { - throw new UnexpectedTypeException($constraint, Slug::class); - } - - if (null === $value || '' === $value) { - return; - } - - if (!\is_scalar($value) && !$value instanceof \Stringable) { - throw new UnexpectedValueException($value, 'string'); - } - - $value = (string) $value; - - if (0 === preg_match($constraint->regex, $value)) { - $this->context->buildViolation($constraint->message) - ->setParameter('{{ value }}', $this->formatValue($value)) - ->setCode(Slug::NOT_SLUG_ERROR) - ->addViolation(); - } - } -} diff --git a/src/Symfony/Component/Validator/Tests/Constraints/SlugTest.php b/src/Symfony/Component/Validator/Tests/Constraints/SlugTest.php deleted file mode 100644 index a2c5b07d3f873..0000000000000 --- a/src/Symfony/Component/Validator/Tests/Constraints/SlugTest.php +++ /dev/null @@ -1,47 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Symfony\Component\Validator\Tests\Constraints; - -use PHPUnit\Framework\TestCase; -use Symfony\Component\Validator\Constraints\Slug; -use Symfony\Component\Validator\Mapping\ClassMetadata; -use Symfony\Component\Validator\Mapping\Loader\AttributeLoader; - -class SlugTest extends TestCase -{ - public function testAttributes() - { - $metadata = new ClassMetadata(SlugDummy::class); - $loader = new AttributeLoader(); - self::assertTrue($loader->loadClassMetadata($metadata)); - - [$bConstraint] = $metadata->properties['b']->getConstraints(); - self::assertSame('myMessage', $bConstraint->message); - self::assertSame(['Default', 'SlugDummy'], $bConstraint->groups); - - [$cConstraint] = $metadata->properties['c']->getConstraints(); - self::assertSame(['my_group'], $cConstraint->groups); - self::assertSame('some attached data', $cConstraint->payload); - } -} - -class SlugDummy -{ - #[Slug] - private $a; - - #[Slug(message: 'myMessage')] - private $b; - - #[Slug(groups: ['my_group'], payload: 'some attached data')] - private $c; -} diff --git a/src/Symfony/Component/Validator/Tests/Constraints/SlugValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/SlugValidatorTest.php deleted file mode 100644 index f88bff3e167ef..0000000000000 --- a/src/Symfony/Component/Validator/Tests/Constraints/SlugValidatorTest.php +++ /dev/null @@ -1,117 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Symfony\Component\Validator\Tests\Constraints; - -use Symfony\Component\String\Slugger\AsciiSlugger; -use Symfony\Component\Validator\Constraints\Slug; -use Symfony\Component\Validator\Constraints\SlugValidator; -use Symfony\Component\Validator\Exception\UnexpectedValueException; -use Symfony\Component\Validator\Test\ConstraintValidatorTestCase; - -class SlugValidatorTest extends ConstraintValidatorTestCase -{ - protected function createValidator(): SlugValidator - { - return new SlugValidator(); - } - - public function testNullIsValid() - { - $this->validator->validate(null, new Slug()); - - $this->assertNoViolation(); - } - - public function testEmptyStringIsValid() - { - $this->validator->validate('', new Slug()); - - $this->assertNoViolation(); - } - - public function testExpectsStringCompatibleType() - { - $this->expectException(UnexpectedValueException::class); - $this->validator->validate(new \stdClass(), new Slug()); - } - - /** - * @testWith ["test-slug"] - * ["slug-123-test"] - * ["slug"] - * ["TestSlug"] - */ - public function testValidSlugs($slug) - { - $this->validator->validate($slug, new Slug()); - - $this->assertNoViolation(); - } - - /** - * @testWith ["Not a slug"] - * ["not-รก-slug"] - * ["not-@-slug"] - */ - public function testInvalidSlugs($slug) - { - $constraint = new Slug(message: 'myMessage'); - - $this->validator->validate($slug, $constraint); - - $this->buildViolation('myMessage') - ->setParameter('{{ value }}', '"'.$slug.'"') - ->setCode(Slug::NOT_SLUG_ERROR) - ->assertRaised(); - } - - /** - * @testWith ["test-slug", true] - * ["slug-123-test", true] - */ - public function testCustomRegexInvalidSlugs($slug) - { - $constraint = new Slug(regex: '/^[a-z0-9]+$/i'); - - $this->validator->validate($slug, $constraint); - - $this->buildViolation($constraint->message) - ->setParameter('{{ value }}', '"'.$slug.'"') - ->setCode(Slug::NOT_SLUG_ERROR) - ->assertRaised(); - } - - /** - * @testWith ["slug"] - * ["test1234"] - */ - public function testCustomRegexValidSlugs($slug) - { - $constraint = new Slug(regex: '/^[a-z0-9]+$/i'); - - $this->validator->validate($slug, $constraint); - - $this->assertNoViolation(); - } - - /** - * @testWith ["PHP"] - * ["Symfony is cool"] - * ["Lorem ipsum dolor sit amet"] - */ - public function testAcceptAsciiSluggerResults(string $text) - { - $this->validator->validate((new AsciiSlugger())->slug($text), new Slug()); - - $this->assertNoViolation(); - } -}