AnnotationLoader.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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 Doctrine\Common\Annotations\Reader;
  12. use Symfony\Component\Validator\Constraint;
  13. use Symfony\Component\Validator\Constraints\Callback;
  14. use Symfony\Component\Validator\Constraints\GroupSequence;
  15. use Symfony\Component\Validator\Constraints\GroupSequenceProvider;
  16. use Symfony\Component\Validator\Exception\MappingException;
  17. use Symfony\Component\Validator\Mapping\ClassMetadata;
  18. /**
  19. * Loads validation metadata using a Doctrine annotation {@link Reader}.
  20. *
  21. * @author Bernhard Schussek <bschussek@gmail.com>
  22. */
  23. class AnnotationLoader implements LoaderInterface
  24. {
  25. protected $reader;
  26. public function __construct(Reader $reader)
  27. {
  28. $this->reader = $reader;
  29. }
  30. /**
  31. * {@inheritdoc}
  32. */
  33. public function loadClassMetadata(ClassMetadata $metadata)
  34. {
  35. $reflClass = $metadata->getReflectionClass();
  36. $className = $reflClass->name;
  37. $success = false;
  38. foreach ($this->reader->getClassAnnotations($reflClass) as $constraint) {
  39. if ($constraint instanceof GroupSequence) {
  40. $metadata->setGroupSequence($constraint->groups);
  41. } elseif ($constraint instanceof GroupSequenceProvider) {
  42. $metadata->setGroupSequenceProvider(true);
  43. } elseif ($constraint instanceof Constraint) {
  44. $metadata->addConstraint($constraint);
  45. }
  46. $success = true;
  47. }
  48. foreach ($reflClass->getProperties() as $property) {
  49. if ($property->getDeclaringClass()->name === $className) {
  50. foreach ($this->reader->getPropertyAnnotations($property) as $constraint) {
  51. if ($constraint instanceof Constraint) {
  52. $metadata->addPropertyConstraint($property->name, $constraint);
  53. }
  54. $success = true;
  55. }
  56. }
  57. }
  58. foreach ($reflClass->getMethods() as $method) {
  59. if ($method->getDeclaringClass()->name === $className) {
  60. foreach ($this->reader->getMethodAnnotations($method) as $constraint) {
  61. if ($constraint instanceof Callback) {
  62. $constraint->callback = $method->getName();
  63. $constraint->methods = null;
  64. $metadata->addConstraint($constraint);
  65. } elseif ($constraint instanceof Constraint) {
  66. if (preg_match('/^(get|is|has)(.+)$/i', $method->name, $matches)) {
  67. $metadata->addGetterMethodConstraint(lcfirst($matches[2]), $matches[0], $constraint);
  68. } else {
  69. throw new MappingException(sprintf('The constraint on "%s::%s" cannot be added. Constraints can only be added on methods beginning with "get", "is" or "has".', $className, $method->name));
  70. }
  71. }
  72. $success = true;
  73. }
  74. }
  75. }
  76. return $success;
  77. }
  78. }