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