AbstractLoader.php 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. public const DEFAULT_NAMESPACE = '\\Symfony\\Component\\Validator\\Constraints\\';
  30. protected $namespaces = [];
  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. protected function addNamespaceAlias(string $alias, string $namespace)
  42. {
  43. $this->namespaces[$alias] = $namespace;
  44. }
  45. /**
  46. * Creates a new constraint instance for the given constraint name.
  47. *
  48. * @param string $name The constraint name. Either a constraint relative
  49. * to the default constraint namespace, or a fully
  50. * qualified class name. Alternatively, the constraint
  51. * may be preceded by a namespace alias and a colon.
  52. * The namespace alias must have been defined using
  53. * {@link addNamespaceAlias()}.
  54. * @param mixed $options The constraint options
  55. *
  56. * @return Constraint
  57. *
  58. * @throws MappingException If the namespace prefix is undefined
  59. */
  60. protected function newConstraint(string $name, $options = null)
  61. {
  62. if (false !== strpos($name, '\\') && class_exists($name)) {
  63. $className = (string) $name;
  64. } elseif (false !== strpos($name, ':')) {
  65. [$prefix, $className] = explode(':', $name, 2);
  66. if (!isset($this->namespaces[$prefix])) {
  67. throw new MappingException(sprintf('Undefined namespace prefix "%s".', $prefix));
  68. }
  69. $className = $this->namespaces[$prefix].$className;
  70. } else {
  71. $className = self::DEFAULT_NAMESPACE.$name;
  72. }
  73. return new $className($options);
  74. }
  75. }