Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions src/Symfony/Component/Validator/Attribute/HasNamedArguments.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Validator\Attribute;

#[\Attribute(\Attribute::TARGET_METHOD)]
final class HasNamedArguments
{
}
1 change: 1 addition & 0 deletions src/Symfony/Component/Validator/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ CHANGELOG
* Deprecate `Constraint::$errorNames`, use `Constraint::ERROR_NAMES` instead
* Deprecate constraint `ExpressionLanguageSyntax`, use `ExpressionSyntax` instead
* Add method `__toString()` to `ConstraintViolationInterface` & `ConstraintViolationListInterface`
* Allow creating constraints with required arguments

6.0
---
Expand Down
10 changes: 10 additions & 0 deletions src/Symfony/Component/Validator/Mapping/Loader/AbstractLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Symfony\Component\Validator\Mapping\Loader;

use Symfony\Component\Validator\Attribute\HasNamedArguments;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\Exception\MappingException;

Expand All @@ -34,6 +35,11 @@ abstract class AbstractLoader implements LoaderInterface

protected $namespaces = [];

/**
* @var array<class-string, bool>
*/
private array $namedArgumentsCache = [];

/**
* Adds a namespace alias.
*
Expand Down Expand Up @@ -78,6 +84,10 @@ protected function newConstraint(string $name, mixed $options = null): Constrain
$className = self::DEFAULT_NAMESPACE.$name;
}

if ($this->namedArgumentsCache[$className] ??= (bool) (new \ReflectionMethod($className, '__construct'))->getAttributes(HasNamedArguments::class)) {
return new $className(...$options);
}

return new $className($options);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Validator\Tests\Fixtures;

use Symfony\Component\Validator\Attribute\HasNamedArguments;
use Symfony\Component\Validator\Constraint;

#[\Attribute]
final class ConstraintWithRequiredArgument extends Constraint
{
public string $requiredArg;

#[HasNamedArguments]
public function __construct(string $requiredArg, array $groups = null, mixed $payload = null)
{
parent::__construct([], $groups, $payload);

$this->requiredArg = $requiredArg;
}
}
8 changes: 8 additions & 0 deletions src/Symfony/Component/Validator/Tests/Fixtures/Entity_81.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Symfony\Component\Validator\Tests\Fixtures;

class Entity_81
{
public $title;
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
use Symfony\Component\Validator\Tests\Fixtures\Annotation\GroupSequenceProviderEntity;
use Symfony\Component\Validator\Tests\Fixtures\ConstraintA;
use Symfony\Component\Validator\Tests\Fixtures\ConstraintB;
use Symfony\Component\Validator\Tests\Fixtures\ConstraintWithRequiredArgument;
use Symfony\Component\Validator\Tests\Fixtures\Entity_81;

class XmlFileLoaderTest extends TestCase
{
Expand Down Expand Up @@ -98,6 +100,19 @@ public function testLoadClassMetadataWithNonStrings()
$this->assertFalse($constraints[0]->match);
}

public function testLoadClassMetadataWithRequiredArguments()
{
$loader = new XmlFileLoader(__DIR__.'/constraint-mapping-required-arg.xml');
$metadata = new ClassMetadata(Entity_81::class);

$loader->loadClassMetadata($metadata);

$expected = new ClassMetadata(Entity_81::class);
$expected->addPropertyConstraint('title', new ConstraintWithRequiredArgument('X'));

$this->assertEquals($expected, $metadata);
}

public function testLoadGroupSequenceProvider()
{
$loader = new XmlFileLoader(__DIR__.'/constraint-mapping.xml');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
use Symfony\Component\Validator\Tests\Fixtures\Annotation\GroupSequenceProviderEntity;
use Symfony\Component\Validator\Tests\Fixtures\ConstraintA;
use Symfony\Component\Validator\Tests\Fixtures\ConstraintB;
use Symfony\Component\Validator\Tests\Fixtures\ConstraintWithRequiredArgument;
use Symfony\Component\Validator\Tests\Fixtures\Entity_81;

class YamlFileLoaderTest extends TestCase
{
Expand Down Expand Up @@ -138,6 +140,19 @@ public function testLoadClassMetadataWithConstants()
$this->assertEquals($expected, $metadata);
}

public function testLoadClassMetadataWithRequiredArguments()
{
$loader = new YamlFileLoader(__DIR__.'/constraint-mapping-required-arg.yml');
$metadata = new ClassMetadata(Entity_81::class);

$loader->loadClassMetadata($metadata);

$expected = new ClassMetadata(Entity_81::class);
$expected->addPropertyConstraint('title', new ConstraintWithRequiredArgument('X'));

$this->assertEquals($expected, $metadata);
}

public function testLoadGroupSequenceProvider()
{
$loader = new YamlFileLoader(__DIR__.'/constraint-mapping.yml');
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0" ?>

<constraint-mapping xmlns="http://symfony.com/schema/dic/constraint-mapping"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/constraint-mapping https://symfony.com/schema/dic/constraint-mapping/constraint-mapping-1.0.xsd">

<class name="Symfony\Component\Validator\Tests\Fixtures\Entity_81">

<property name="title">
<constraint name="Symfony\Component\Validator\Tests\Fixtures\ConstraintWithRequiredArgument">
<option name="requiredArg">X</option>
</constraint>
</property>
</class>

</constraint-mapping>
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Symfony\Component\Validator\Tests\Fixtures\Entity_81:
properties:
title:
- Symfony\Component\Validator\Tests\Fixtures\ConstraintWithRequiredArgument:
requiredArg: 'X'