AbstractLoader.php 3.0 KB

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