Skip to content

Commit

Permalink
feature #28644 [Validator] Pre-check constraint validator dependencie…
Browse files Browse the repository at this point in the history
…s (ro0NL)

This PR was merged into the 4.2-dev branch.

Discussion
----------

[Validator] Pre-check constraint validator dependencies

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no     <!-- see https://symfony.com/bc -->
| Deprecations? | yes
| Tests pass?   | yes    <!-- please add some, will be required by reviewers -->
| Fixed tickets | #25865
| License       | MIT
| Doc PR        | symfony/symfony-docs#... <!-- required for new features -->

<!--
Write a short README entry for your feature/bugfix here (replace this comment block.)
This will help people understand your PR and can be used as a start of the Doc PR.
Additionally:
 - Bug fixes must be submitted against the lowest branch where they apply
   (lowest branches are regularly merged to upper ones so they get the fixes too).
 - Features and deprecations must be submitted against the master branch.
-->

Commits
-------

0544985 [Validator] Pre-check constraint validator dependencies
  • Loading branch information
nicolas-grekas committed Oct 3, 2018
2 parents 38655bd + 0544985 commit 3b604ff
Show file tree
Hide file tree
Showing 10 changed files with 74 additions and 4 deletions.
4 changes: 3 additions & 1 deletion UPGRADE-4.2.md
Expand Up @@ -216,4 +216,6 @@ Validator
* The component is now decoupled from `symfony/translation` and uses `Symfony\Contracts\Translation\TranslatorInterface` instead
* The `ValidatorBuilderInterface` has been deprecated and `ValidatorBuilder` made final
* Deprecated validating instances of `\DateTimeInterface` in `DateTimeValidator`, `DateValidator` and `TimeValidator`. Use `Type` instead or remove the constraint if the underlying model is type hinted to `\DateTimeInterface` already.
* Using the `Bic` constraint without `symfony/intl` is deprecated
* Using the `Bic`, `Country`, `Currency`, `Language` and `Locale` constraints without `symfony/intl` is deprecated
* Using the `Email` constraint without `egulias/email-validator` is deprecated
* Using the `Expression` constraint without `symfony/expression-language` is deprecated
4 changes: 3 additions & 1 deletion UPGRADE-5.0.md
Expand Up @@ -199,7 +199,9 @@ Validator
* The component is now decoupled from `symfony/translation` and uses `Symfony\Contracts\Translation\TranslatorInterface` instead
* The `ValidatorBuilderInterface` has been removed and `ValidatorBuilder` is now final
* Removed support for validating instances of `\DateTimeInterface` in `DateTimeValidator`, `DateValidator` and `TimeValidator`. Use `Type` instead or remove the constraint if the underlying model is type hinted to `\DateTimeInterface` already.
* The `symfony/intl` component is now required for using the `Bic` constraint
* The `symfony/intl` component is now required for using the `Bic`, `Country`, `Currency`, `Language` and `Locale` constraints
* The `egulias/email-validator` component is now required for using the `Email` constraint
* The `symfony/expression-language` component is now required for using the `Expression` constraint

Workflow
--------
Expand Down
4 changes: 3 additions & 1 deletion src/Symfony/Component/Validator/CHANGELOG.md
Expand Up @@ -10,7 +10,9 @@ CHANGELOG
* made `ValidatorBuilder` final
* marked `format` the default option in `DateTime` constraint
* deprecated validating instances of `\DateTimeInterface` in `DateTimeValidator`, `DateValidator` and `TimeValidator`.
* deprecated using the `Bic` constraint without `symfony/intl`
* deprecated using the `Bic`, `Country`, `Currency`, `Language` and `Locale` constraints without `symfony/intl`
* deprecated using the `Email` constraint without `egulias/email-validator`
* deprecated using the `Expression` constraint without `symfony/expression-language`

4.1.0
-----
Expand Down
4 changes: 3 additions & 1 deletion src/Symfony/Component/Validator/Constraints/BicValidator.php
Expand Up @@ -14,6 +14,7 @@
use Symfony\Component\Intl\Intl;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
use Symfony\Component\Validator\Exception\LogicException;
use Symfony\Component\Validator\Exception\UnexpectedTypeException;

/**
Expand Down Expand Up @@ -68,11 +69,12 @@ public function validate($value, Constraint $constraint)
return;
}

// @deprecated since Symfony 4.2
// @deprecated since Symfony 4.2, will throw in 5.0
if (class_exists(Intl::class)) {
$validCountryCode = isset(Intl::getRegionBundle()->getCountryNames()[substr($canonicalize, 4, 2)]);
} else {
$validCountryCode = ctype_alpha(substr($canonicalize, 4, 2));
// throw new LogicException('The "symfony/intl" component is required to use the Bic constraint.');
}

if (!$validCountryCode) {
Expand Down
12 changes: 12 additions & 0 deletions src/Symfony/Component/Validator/Constraints/Country.php
Expand Up @@ -11,7 +11,9 @@

namespace Symfony\Component\Validator\Constraints;

use Symfony\Component\Intl\Intl;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\Exception\LogicException;

/**
* @Annotation
Expand All @@ -28,4 +30,14 @@ class Country extends Constraint
);

public $message = 'This value is not a valid country.';

public function __construct($options = null)
{
if (!class_exists(Intl::class)) {
// throw new LogicException(sprintf('The "symfony/intl" component is required to use the "%s" constraint.', __CLASS__));
@trigger_error(sprintf('Using the "%s" constraint without the "symfony/intl" component installed is deprecated since Symfony 4.2.', __CLASS__), E_USER_DEPRECATED);
}

parent::__construct($options);
}
}
12 changes: 12 additions & 0 deletions src/Symfony/Component/Validator/Constraints/Currency.php
Expand Up @@ -11,7 +11,9 @@

namespace Symfony\Component\Validator\Constraints;

use Symfony\Component\Intl\Intl;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\Exception\LogicException;

/**
* @Annotation
Expand All @@ -29,4 +31,14 @@ class Currency extends Constraint
);

public $message = 'This value is not a valid currency.';

public function __construct($options = null)
{
if (!class_exists(Intl::class)) {
// throw new LogicException(sprintf('The "symfony/intl" component is required to use the "%s" constraint.', __CLASS__));
@trigger_error(sprintf('Using the "%s" constraint without the "symfony/intl" component installed is deprecated since Symfony 4.2.', __CLASS__), E_USER_DEPRECATED);
}

parent::__construct($options);
}
}
7 changes: 7 additions & 0 deletions src/Symfony/Component/Validator/Constraints/Email.php
Expand Up @@ -11,7 +11,9 @@

namespace Symfony\Component\Validator\Constraints;

use Egulias\EmailValidator\EmailValidator as StrictEmailValidator;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\Exception\LogicException;

/**
* @Annotation
Expand Down Expand Up @@ -67,5 +69,10 @@ public function __construct($options = null)
}

parent::__construct($options);

if ((self::VALIDATION_MODE_STRICT === $this->mode || true === $this->strict) && !class_exists(StrictEmailValidator::class)) {
// throw new LogicException(sprintf('The "egulias/email-validator" component is required to use the "%s" constraint.', __CLASS__));
@trigger_error(sprintf('Using the "%s" constraint without the "egulias/email-validator" component installed is deprecated since Symfony 4.2.', __CLASS__), E_USER_DEPRECATED);
}
}
}
12 changes: 12 additions & 0 deletions src/Symfony/Component/Validator/Constraints/Expression.php
Expand Up @@ -11,7 +11,9 @@

namespace Symfony\Component\Validator\Constraints;

use Symfony\Component\ExpressionLanguage\ExpressionLanguage;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\Exception\LogicException;

/**
* @Annotation
Expand All @@ -32,6 +34,16 @@ class Expression extends Constraint
public $expression;
public $values = array();

public function __construct($options = null)
{
if (!class_exists(ExpressionLanguage::class)) {
// throw new LogicException(sprintf('The "symfony/expression-language" component is required to use the "%s" constraint.', __CLASS__));
@trigger_error(sprintf('Using the "%s" constraint without the "symfony/expression-language" component installed is deprecated since Symfony 4.2.', __CLASS__), E_USER_DEPRECATED);
}

parent::__construct($options);
}

/**
* {@inheritdoc}
*/
Expand Down
12 changes: 12 additions & 0 deletions src/Symfony/Component/Validator/Constraints/Language.php
Expand Up @@ -11,7 +11,9 @@

namespace Symfony\Component\Validator\Constraints;

use Symfony\Component\Intl\Intl;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\Exception\LogicException;

/**
* @Annotation
Expand All @@ -28,4 +30,14 @@ class Language extends Constraint
);

public $message = 'This value is not a valid language.';

public function __construct($options = null)
{
if (!class_exists(Intl::class)) {
// throw new LogicException(sprintf('The "symfony/intl" component is required to use the "%s" constraint.', __CLASS__));
@trigger_error(sprintf('Using the "%s" constraint without the "symfony/intl" component installed is deprecated since Symfony 4.2.', __CLASS__), E_USER_DEPRECATED);
}

parent::__construct($options);
}
}
7 changes: 7 additions & 0 deletions src/Symfony/Component/Validator/Constraints/Locale.php
Expand Up @@ -11,7 +11,9 @@

namespace Symfony\Component\Validator\Constraints;

use Symfony\Component\Intl\Intl;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\Exception\LogicException;

/**
* @Annotation
Expand All @@ -36,6 +38,11 @@ public function __construct($options = null)
@trigger_error('The "canonicalize" option with value "false" is deprecated since Symfony 4.1, set it to "true" instead.', E_USER_DEPRECATED);
}

if (!class_exists(Intl::class)) {
// throw new LogicException(sprintf('The "symfony/intl" component is required to use the "%s" constraint.', __CLASS__));
@trigger_error(sprintf('Using the "%s" constraint without the "symfony/intl" component installed is deprecated since Symfony 4.2.', __CLASS__), E_USER_DEPRECATED);
}

parent::__construct($options);
}
}

0 comments on commit 3b604ff

Please sign in to comment.