PropertyMetadata.php 2.9 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;
  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(string $class, string $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. $reflProperty = $this->getReflectionMember($object);
  46. if (\PHP_VERSION_ID >= 70400 && $reflProperty->hasType() && !$reflProperty->isInitialized($object)) {
  47. // There is no way to check if a property has been unset or if it is uninitialized.
  48. // When trying to access an uninitialized property, __get method is triggered.
  49. // If __get method is not present, no fallback is possible
  50. // Otherwise we need to catch an Error in case we are trying to access an uninitialized but set property.
  51. if (!method_exists($object, '__get')) {
  52. return null;
  53. }
  54. try {
  55. return $reflProperty->getValue($object);
  56. } catch (\Error $e) {
  57. return null;
  58. }
  59. }
  60. return $reflProperty->getValue($object);
  61. }
  62. /**
  63. * {@inheritdoc}
  64. */
  65. protected function newReflectionMember($objectOrClassName)
  66. {
  67. $originalClass = \is_string($objectOrClassName) ? $objectOrClassName : \get_class($objectOrClassName);
  68. while (!property_exists($objectOrClassName, $this->getName())) {
  69. $objectOrClassName = get_parent_class($objectOrClassName);
  70. if (false === $objectOrClassName) {
  71. throw new ValidatorException(sprintf('Property "%s" does not exist in class "%s".', $this->getName(), $originalClass));
  72. }
  73. }
  74. $member = new \ReflectionProperty($objectOrClassName, $this->getName());
  75. $member->setAccessible(true);
  76. return $member;
  77. }
  78. }