annotate vendor/symfony/validator/Mapping/Loader/AbstractLoader.php @ 0:4c8ae668cc8c

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