PropertyMetadata.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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.
  14. *
  15. * The value of the property is obtained by directly accessing the property.
  16. * The property will be accessed by reflection, so the access of private and
  17. * protected properties is supported.
  18. *
  19. * This class supports serialization and cloning.
  20. *
  21. * @author Bernhard Schussek <bschussek@gmail.com>
  22. *
  23. * @see PropertyMetadataInterface
  24. */
  25. class PropertyMetadata extends MemberMetadata
  26. {
  27. /**
  28. * @param string $class The class this property is defined on
  29. * @param string $name The name of this property
  30. *
  31. * @throws ValidatorException
  32. */
  33. public function __construct($class, $name)
  34. {
  35. if (!property_exists($class, $name)) {
  36. throw new ValidatorException(sprintf('Property "%s" does not exist in class "%s"', $name, $class));
  37. }
  38. parent::__construct($class, $name, $name);
  39. }
  40. /**
  41. * {@inheritdoc}
  42. */
  43. public function getPropertyValue($object)
  44. {
  45. return $this->getReflectionMember($object)->getValue($object);
  46. }
  47. /**
  48. * {@inheritdoc}
  49. */
  50. protected function newReflectionMember($objectOrClassName)
  51. {
  52. $originalClass = \is_string($objectOrClassName) ? $objectOrClassName : \get_class($objectOrClassName);
  53. while (!property_exists($objectOrClassName, $this->getName())) {
  54. $objectOrClassName = get_parent_class($objectOrClassName);
  55. if (false === $objectOrClassName) {
  56. throw new ValidatorException(sprintf('Property "%s" does not exist in class "%s".', $this->getName(), $originalClass));
  57. }
  58. }
  59. $member = new \ReflectionProperty($objectOrClassName, $this->getName());
  60. $member->setAccessible(true);
  61. return $member;
  62. }
  63. }