"Fossies" - the Fresh Open Source Software Archive

Member "drupal-9.4.5/vendor/symfony/validator/Mapping/Loader/AbstractLoader.php" (20 Jul 2022, 3031 Bytes) of package /linux/www/drupal-9.4.5.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) PHP source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

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