Drupal investigation

AnnotationLoader.php 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. /**
  26. * @var Reader
  27. */
  28. protected $reader;
  29. public function __construct(Reader $reader)
  30. {
  31. $this->reader = $reader;
  32. }
  33. /**
  34. * {@inheritdoc}
  35. */
  36. public function loadClassMetadata(ClassMetadata $metadata)
  37. {
  38. $reflClass = $metadata->getReflectionClass();
  39. $className = $reflClass->name;
  40. $success = false;
  41. foreach ($this->reader->getClassAnnotations($reflClass) as $constraint) {
  42. if ($constraint instanceof GroupSequence) {
  43. $metadata->setGroupSequence($constraint->groups);
  44. } elseif ($constraint instanceof GroupSequenceProvider) {
  45. $metadata->setGroupSequenceProvider(true);
  46. } elseif ($constraint instanceof Constraint) {
  47. $metadata->addConstraint($constraint);
  48. }
  49. $success = true;
  50. }
  51. foreach ($reflClass->getProperties() as $property) {
  52. if ($property->getDeclaringClass()->name === $className) {
  53. foreach ($this->reader->getPropertyAnnotations($property) as $constraint) {
  54. if ($constraint instanceof Constraint) {
  55. $metadata->addPropertyConstraint($property->name, $constraint);
  56. }
  57. $success = true;
  58. }
  59. }
  60. }
  61. foreach ($reflClass->getMethods() as $method) {
  62. if ($method->getDeclaringClass()->name === $className) {
  63. foreach ($this->reader->getMethodAnnotations($method) as $constraint) {
  64. if ($constraint instanceof Callback) {
  65. $constraint->callback = $method->getName();
  66. $constraint->methods = null;
  67. $metadata->addConstraint($constraint);
  68. } elseif ($constraint instanceof Constraint) {
  69. if (preg_match('/^(get|is|has)(.+)$/i', $method->name, $matches)) {
  70. $metadata->addGetterMethodConstraint(lcfirst($matches[2]), $matches[0], $constraint);
  71. } else {
  72. 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));
  73. }
  74. }
  75. $success = true;
  76. }
  77. }
  78. }
  79. return $success;
  80. }
  81. }