Drupal investigation

GetterMetadata.php 2.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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;
  11. use Symfony\Component\Validator\Exception\ValidatorException;
  12. /**
  13. * Stores all metadata needed for validating a class property via its getter
  14. * method.
  15. *
  16. * A property getter is any method that is equal to the property's name,
  17. * prefixed with either "get" or "is". That method will be used to access the
  18. * property's value.
  19. *
  20. * The getter will be invoked by reflection, so the access of private and
  21. * protected getters is supported.
  22. *
  23. * This class supports serialization and cloning.
  24. *
  25. * @author Bernhard Schussek <bschussek@gmail.com>
  26. *
  27. * @see PropertyMetadataInterface
  28. */
  29. class GetterMetadata extends MemberMetadata
  30. {
  31. /**
  32. * Constructor.
  33. *
  34. * @param string $class The class the getter is defined on
  35. * @param string $property The property which the getter returns
  36. * @param string|null $method The method that is called to retrieve the value being validated (null for auto-detection)
  37. *
  38. * @throws ValidatorException
  39. */
  40. public function __construct($class, $property, $method = null)
  41. {
  42. if (null === $method) {
  43. $getMethod = 'get'.ucfirst($property);
  44. $isMethod = 'is'.ucfirst($property);
  45. $hasMethod = 'has'.ucfirst($property);
  46. if (method_exists($class, $getMethod)) {
  47. $method = $getMethod;
  48. } elseif (method_exists($class, $isMethod)) {
  49. $method = $isMethod;
  50. } elseif (method_exists($class, $hasMethod)) {
  51. $method = $hasMethod;
  52. } else {
  53. throw new ValidatorException(sprintf('Neither of these methods exist in class %s: %s, %s, %s', $class, $getMethod, $isMethod, $hasMethod));
  54. }
  55. } elseif (!method_exists($class, $method)) {
  56. throw new ValidatorException(sprintf('The %s() method does not exist in class %s.', $method, $class));
  57. }
  58. parent::__construct($class, $method, $property);
  59. }
  60. /**
  61. * {@inheritdoc}
  62. */
  63. public function getPropertyValue($object)
  64. {
  65. return $this->newReflectionMember($object)->invoke($object);
  66. }
  67. /**
  68. * {@inheritdoc}
  69. */
  70. protected function newReflectionMember($objectOrClassName)
  71. {
  72. return new \ReflectionMethod($objectOrClassName, $this->getName());
  73. }
  74. }