vendor/doctrine/mongodb-odm/src/Mapping/LegacyReflectionFields.php line 57

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Doctrine\ODM\MongoDB\Mapping;
  4. use ArrayAccess;
  5. use Countable;
  6. use Doctrine\ODM\MongoDB\Mapping\PropertyAccessors\ReflectionReadonlyProperty;
  7. use Doctrine\Persistence\Mapping\ReflectionService;
  8. use Doctrine\Persistence\Reflection\EnumReflectionProperty;
  9. use Generator;
  10. use IteratorAggregate;
  11. use OutOfBoundsException;
  12. use ReflectionProperty;
  13. use Traversable;
  14. use function array_keys;
  15. use function assert;
  16. use function count;
  17. use function trigger_deprecation;
  18. /**
  19. * @internal
  20. *
  21. * @template-implements ArrayAccess<string, ReflectionProperty|null>
  22. * @template-implements IteratorAggregate<string, ReflectionProperty|null>
  23. */
  24. class LegacyReflectionFields implements ArrayAccess, IteratorAggregate, Countable
  25. {
  26. /** @var array<string, ReflectionProperty|null> */
  27. private array $reflFields = [];
  28. public function __construct(private ClassMetadata $classMetadata, private ReflectionService $reflectionService)
  29. {
  30. }
  31. /** @param string $offset */
  32. public function offsetExists($offset): bool // phpcs:ignore
  33. {
  34. trigger_deprecation('doctrine/mongodb-odm', '2.14', 'Access to ClassMetadata::$reflFields is deprecated and will be removed in Doctrine ODM 3.0.');
  35. return isset($this->classMetadata->propertyAccessors[$offset]);
  36. }
  37. /**
  38. * @param string $field
  39. *
  40. * @psalm-suppress LessSpecificImplementedReturnType
  41. */
  42. public function offsetGet($field): mixed // phpcs:ignore
  43. {
  44. if (isset($this->reflFields[$field])) {
  45. return $this->reflFields[$field];
  46. }
  47. trigger_deprecation('doctrine/mongodb-odm', '2.14', 'Access to ClassMetadata::$reflFields is deprecated and will be removed in Doctrine ODM 3.0.');
  48. if (! isset($this->classMetadata->propertyAccessors[$field])) {
  49. throw new OutOfBoundsException('Unknown field: ' . $this->classMetadata->name . ' ::$' . $field);
  50. }
  51. $className = $this->classMetadata->fieldMappings[$field]['inherited']
  52. ?? $this->classMetadata->fieldMappings[$field]['declared']
  53. ?? $this->classMetadata->associationMappings[$field]['declared']
  54. ?? $this->classMetadata->name;
  55. $this->reflFields[$field] = $this->getAccessibleProperty($className, $field);
  56. if (isset($this->classMetadata->fieldMappings[$field])) {
  57. if ($this->classMetadata->fieldMappings[$field]['enumType'] ?? null) {
  58. $this->reflFields[$field] = new EnumReflectionProperty(
  59. $this->reflFields[$field],
  60. $this->classMetadata->fieldMappings[$field]['enumType'],
  61. );
  62. }
  63. }
  64. return $this->reflFields[$field];
  65. }
  66. /**
  67. * @param string $offset
  68. * @param ReflectionProperty $value
  69. */
  70. public function offsetSet($offset, $value): void // phpcs:ignore
  71. {
  72. $this->reflFields[$offset] = $value;
  73. }
  74. /** @param string $offset */
  75. public function offsetUnset($offset): void // phpcs:ignore
  76. {
  77. unset($this->reflFields[$offset]);
  78. }
  79. /** @psalm-param class-string $class */
  80. private function getAccessibleProperty(string $class, string $field): ReflectionProperty
  81. {
  82. $reflectionProperty = $this->reflectionService->getAccessibleProperty($class, $field);
  83. assert($reflectionProperty !== null);
  84. if ($reflectionProperty->isReadOnly()) {
  85. $declaringClass = $reflectionProperty->class;
  86. if ($declaringClass !== $class) {
  87. $reflectionProperty = $this->reflectionService->getAccessibleProperty($declaringClass, $field);
  88. assert($reflectionProperty !== null);
  89. }
  90. $reflectionProperty = new ReflectionReadonlyProperty($reflectionProperty);
  91. }
  92. return $reflectionProperty;
  93. }
  94. /** @return Generator<string, ReflectionProperty> */
  95. public function getIterator(): Traversable
  96. {
  97. trigger_deprecation('doctrine/mongodb-odm', '2.14', 'Access to ClassMetadata::$reflFields is deprecated and will be removed in Doctrine ODM 3.0.');
  98. $keys = array_keys($this->classMetadata->propertyAccessors);
  99. foreach ($keys as $key) {
  100. yield $key => $this->offsetGet($key);
  101. }
  102. }
  103. public function count(): int
  104. {
  105. trigger_deprecation('doctrine/mongodb-odm', '2.14', 'Access to ClassMetadata::$reflFields is deprecated and will be removed in Doctrine ODM 3.0.');
  106. return count($this->classMetadata->propertyAccessors);
  107. }
  108. }