Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Validator] Allow opt-out of EmailValidator deprecation when using Validation::createValidatorBuilder() #48644

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 14, 2022
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
13 changes: 5 additions & 8 deletions src/Symfony/Component/Validator/ConstraintValidatorFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,17 @@ class ConstraintValidatorFactory implements ConstraintValidatorFactoryInterface
{
protected $validators = [];

public function __construct()
public function __construct(array $validators = [])
{
$this->validators = $validators;
}

public function getInstance(Constraint $constraint): ConstraintValidatorInterface
{
$className = $constraint->validatedBy();

if (!isset($this->validators[$className])) {
$this->validators[$className] = 'validator.expression' === $className
? new ExpressionValidator()
: new $className();
if ('validator.expression' === $name = $class = $constraint->validatedBy()) {
$class = ExpressionValidator::class;
}

return $this->validators[$className];
return $this->validators[$name] ??= new $class();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

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

namespace Symfony\Component\Validator\Tests;

use PHPUnit\Framework\TestCase;
use Symfony\Component\Validator\ConstraintValidatorFactory;
use Symfony\Component\Validator\Tests\Fixtures\DummyConstraint;
use Symfony\Component\Validator\Tests\Fixtures\DummyConstraintValidator;

class ConstraintValidatorFactoryTest extends TestCase
{
public function testGetInstance()
{
$factory = new ConstraintValidatorFactory();
$this->assertInstanceOf(DummyConstraintValidator::class, $factory->getInstance(new DummyConstraint()));
}

public function testPredefinedGetInstance()
{
$validator = new DummyConstraintValidator();
$factory = new ConstraintValidatorFactory([DummyConstraintValidator::class => $validator]);
$this->assertSame($validator, $factory->getInstance(new DummyConstraint()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@
use Symfony\Component\DependencyInjection\Container;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\Constraints\Blank as BlankConstraint;
use Symfony\Component\Validator\ConstraintValidator;
use Symfony\Component\Validator\ContainerConstraintValidatorFactory;
use Symfony\Component\Validator\Exception\ValidatorException;
use Symfony\Component\Validator\Tests\Fixtures\DummyConstraint;
use Symfony\Component\Validator\Tests\Fixtures\DummyConstraintValidator;

class ContainerConstraintValidatorFactoryTest extends TestCase
{
Expand Down Expand Up @@ -59,18 +60,3 @@ public function testGetInstanceInvalidValidatorClass()
$factory->getInstance($constraint);
}
}

class DummyConstraint extends Constraint
{
public function validatedBy(): string
{
return DummyConstraintValidator::class;
}
}

class DummyConstraintValidator extends ConstraintValidator
{
public function validate($value, Constraint $constraint)
{
}
}
22 changes: 22 additions & 0 deletions src/Symfony/Component/Validator/Tests/Fixtures/DummyConstraint.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* 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\Constraint;

class DummyConstraint extends Constraint
{
public function validatedBy(): string
{
return DummyConstraintValidator::class;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* 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\Constraint;
use Symfony\Component\Validator\ConstraintValidator;

class DummyConstraintValidator extends ConstraintValidator
{
public function validate($value, Constraint $constraint)
{
}
}