comparison 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
comparison
equal deleted inserted replaced
-1:000000000000 0:4c8ae668cc8c
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 const DEFAULT_NAMESPACE = '\\Symfony\\Component\\Validator\\Constraints\\';
34
35 /**
36 * @var array
37 */
38 protected $namespaces = array();
39
40 /**
41 * Adds a namespace alias.
42 *
43 * The namespace alias can be used to reference constraints from specific
44 * namespaces in {@link newConstraint()}:
45 *
46 * $this->addNamespaceAlias('mynamespace', '\\Acme\\Package\\Constraints\\');
47 *
48 * $constraint = $this->newConstraint('mynamespace:NotNull');
49 *
50 * @param string $alias The alias
51 * @param string $namespace The PHP namespace
52 */
53 protected function addNamespaceAlias($alias, $namespace)
54 {
55 $this->namespaces[$alias] = $namespace;
56 }
57
58 /**
59 * Creates a new constraint instance for the given constraint name.
60 *
61 * @param string $name The constraint name. Either a constraint relative
62 * to the default constraint namespace, or a fully
63 * qualified class name. Alternatively, the constraint
64 * may be preceded by a namespace alias and a colon.
65 * The namespace alias must have been defined using
66 * {@link addNamespaceAlias()}.
67 * @param mixed $options The constraint options
68 *
69 * @return Constraint
70 *
71 * @throws MappingException If the namespace prefix is undefined
72 */
73 protected function newConstraint($name, $options = null)
74 {
75 if (strpos($name, '\\') !== false && class_exists($name)) {
76 $className = (string) $name;
77 } elseif (strpos($name, ':') !== false) {
78 list($prefix, $className) = explode(':', $name, 2);
79
80 if (!isset($this->namespaces[$prefix])) {
81 throw new MappingException(sprintf('Undefined namespace prefix "%s"', $prefix));
82 }
83
84 $className = $this->namespaces[$prefix].$className;
85 } else {
86 $className = self::DEFAULT_NAMESPACE.$name;
87 }
88
89 return new $className($options);
90 }
91 }