Chris@0: Chris@0: * Chris@0: * For the full copyright and license information, please view the LICENSE Chris@0: * file that was distributed with this source code. Chris@0: */ Chris@0: Chris@0: namespace Symfony\Component\Validator\Mapping\Loader; Chris@0: Chris@0: use Symfony\Component\Validator\Constraint; Chris@0: use Symfony\Component\Validator\Exception\MappingException; Chris@0: Chris@0: /** Chris@0: * Base loader for validation metadata. Chris@0: * Chris@0: * This loader supports the loading of constraints from Symfony's default Chris@0: * namespace (see {@link DEFAULT_NAMESPACE}) using the short class names of Chris@0: * those constraints. Constraints can also be loaded using their fully Chris@0: * qualified class names. At last, namespace aliases can be defined to load Chris@0: * constraints with the syntax "alias:ShortName". Chris@0: * Chris@0: * @author Bernhard Schussek Chris@0: */ Chris@0: abstract class AbstractLoader implements LoaderInterface Chris@0: { Chris@0: /** Chris@0: * The namespace to load constraints from by default. Chris@0: */ Chris@0: const DEFAULT_NAMESPACE = '\\Symfony\\Component\\Validator\\Constraints\\'; Chris@0: Chris@17: protected $namespaces = []; Chris@0: Chris@0: /** Chris@0: * Adds a namespace alias. Chris@0: * Chris@0: * The namespace alias can be used to reference constraints from specific Chris@0: * namespaces in {@link newConstraint()}: Chris@0: * Chris@0: * $this->addNamespaceAlias('mynamespace', '\\Acme\\Package\\Constraints\\'); Chris@0: * Chris@0: * $constraint = $this->newConstraint('mynamespace:NotNull'); Chris@0: * Chris@0: * @param string $alias The alias Chris@0: * @param string $namespace The PHP namespace Chris@0: */ Chris@0: protected function addNamespaceAlias($alias, $namespace) Chris@0: { Chris@0: $this->namespaces[$alias] = $namespace; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Creates a new constraint instance for the given constraint name. Chris@0: * Chris@0: * @param string $name The constraint name. Either a constraint relative Chris@0: * to the default constraint namespace, or a fully Chris@0: * qualified class name. Alternatively, the constraint Chris@0: * may be preceded by a namespace alias and a colon. Chris@0: * The namespace alias must have been defined using Chris@0: * {@link addNamespaceAlias()}. Chris@0: * @param mixed $options The constraint options Chris@0: * Chris@0: * @return Constraint Chris@0: * Chris@0: * @throws MappingException If the namespace prefix is undefined Chris@0: */ Chris@0: protected function newConstraint($name, $options = null) Chris@0: { Chris@14: if (false !== strpos($name, '\\') && class_exists($name)) { Chris@0: $className = (string) $name; Chris@14: } elseif (false !== strpos($name, ':')) { Chris@0: list($prefix, $className) = explode(':', $name, 2); Chris@0: Chris@0: if (!isset($this->namespaces[$prefix])) { Chris@0: throw new MappingException(sprintf('Undefined namespace prefix "%s"', $prefix)); Chris@0: } Chris@0: Chris@0: $className = $this->namespaces[$prefix].$className; Chris@0: } else { Chris@0: $className = self::DEFAULT_NAMESPACE.$name; Chris@0: } Chris@0: Chris@0: return new $className($options); Chris@0: } Chris@0: }