vendor/symfony/validator/Mapping/Loader/AbstractLoader.php line 73

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Symfony package.
  4.  *
  5.  * (c) Fabien Potencier <fabien@symfony.com>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace Symfony\Component\Validator\Mapping\Loader;
  11. use Symfony\Component\Validator\Attribute\HasNamedArguments;
  12. use Symfony\Component\Validator\Constraint;
  13. use Symfony\Component\Validator\Exception\MappingException;
  14. /**
  15.  * Base loader for validation metadata.
  16.  *
  17.  * This loader supports the loading of constraints from Symfony's default
  18.  * namespace (see {@link DEFAULT_NAMESPACE}) using the short class names of
  19.  * those constraints. Constraints can also be loaded using their fully
  20.  * qualified class names. At last, namespace aliases can be defined to load
  21.  * constraints with the syntax "alias:ShortName".
  22.  *
  23.  * @author Bernhard Schussek <bschussek@gmail.com>
  24.  */
  25. abstract class AbstractLoader implements LoaderInterface
  26. {
  27.     /**
  28.      * The namespace to load constraints from by default.
  29.      */
  30.     public const DEFAULT_NAMESPACE '\\Symfony\\Component\\Validator\\Constraints\\';
  31.     protected $namespaces = [];
  32.     /**
  33.      * @var array<class-string, bool>
  34.      */
  35.     private array $namedArgumentsCache = [];
  36.     /**
  37.      * Adds a namespace alias.
  38.      *
  39.      * The namespace alias can be used to reference constraints from specific
  40.      * namespaces in {@link newConstraint()}:
  41.      *
  42.      *     $this->addNamespaceAlias('mynamespace', '\\Acme\\Package\\Constraints\\');
  43.      *
  44.      *     $constraint = $this->newConstraint('mynamespace:NotNull');
  45.      */
  46.     protected function addNamespaceAlias(string $aliasstring $namespace)
  47.     {
  48.         $this->namespaces[$alias] = $namespace;
  49.     }
  50.     /**
  51.      * Creates a new constraint instance for the given constraint name.
  52.      *
  53.      * @param string $name    The constraint name. Either a constraint relative
  54.      *                        to the default constraint namespace, or a fully
  55.      *                        qualified class name. Alternatively, the constraint
  56.      *                        may be preceded by a namespace alias and a colon.
  57.      *                        The namespace alias must have been defined using
  58.      *                        {@link addNamespaceAlias()}.
  59.      * @param mixed  $options The constraint options
  60.      *
  61.      * @throws MappingException If the namespace prefix is undefined
  62.      */
  63.     protected function newConstraint(string $namemixed $options null): Constraint
  64.     {
  65.         if (str_contains($name'\\') && class_exists($name)) {
  66.             $className $name;
  67.         } elseif (str_contains($name':')) {
  68.             [$prefix$className] = explode(':'$name2);
  69.             if (!isset($this->namespaces[$prefix])) {
  70.                 throw new MappingException(sprintf('Undefined namespace prefix "%s".'$prefix));
  71.             }
  72.             $className $this->namespaces[$prefix].$className;
  73.         } else {
  74.             $className self::DEFAULT_NAMESPACE.$name;
  75.         }
  76.         if ($this->namedArgumentsCache[$className] ??= (bool) (new \ReflectionMethod($className'__construct'))->getAttributes(HasNamedArguments::class)) {
  77.             return new $className(...$options);
  78.         }
  79.         return new $className($options);
  80.     }
  81. }