PropertyAccessorBuilder.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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\PropertyAccess;
  11. use Psr\Cache\CacheItemPoolInterface;
  12. use Symfony\Component\PropertyInfo\PropertyReadInfoExtractorInterface;
  13. use Symfony\Component\PropertyInfo\PropertyWriteInfoExtractorInterface;
  14. /**
  15. * A configurable builder to create a PropertyAccessor.
  16. *
  17. * @author Jérémie Augustin <jeremie.augustin@pixel-cookers.com>
  18. */
  19. class PropertyAccessorBuilder
  20. {
  21. private int $magicMethods = PropertyAccessor::MAGIC_GET | PropertyAccessor::MAGIC_SET;
  22. private bool $throwExceptionOnInvalidIndex = false;
  23. private bool $throwExceptionOnInvalidPropertyPath = true;
  24. private ?CacheItemPoolInterface $cacheItemPool = null;
  25. private ?PropertyReadInfoExtractorInterface $readInfoExtractor = null;
  26. private ?PropertyWriteInfoExtractorInterface $writeInfoExtractor = null;
  27. /**
  28. * Enables the use of all magic methods by the PropertyAccessor.
  29. *
  30. * @return $this
  31. */
  32. public function enableMagicMethods(): static
  33. {
  34. $this->magicMethods = PropertyAccessor::MAGIC_GET | PropertyAccessor::MAGIC_SET | PropertyAccessor::MAGIC_CALL;
  35. return $this;
  36. }
  37. /**
  38. * Disable the use of all magic methods by the PropertyAccessor.
  39. *
  40. * @return $this
  41. */
  42. public function disableMagicMethods(): static
  43. {
  44. $this->magicMethods = PropertyAccessor::DISALLOW_MAGIC_METHODS;
  45. return $this;
  46. }
  47. /**
  48. * Enables the use of "__call" by the PropertyAccessor.
  49. *
  50. * @return $this
  51. */
  52. public function enableMagicCall(): static
  53. {
  54. $this->magicMethods |= PropertyAccessor::MAGIC_CALL;
  55. return $this;
  56. }
  57. /**
  58. * Enables the use of "__get" by the PropertyAccessor.
  59. */
  60. public function enableMagicGet(): self
  61. {
  62. $this->magicMethods |= PropertyAccessor::MAGIC_GET;
  63. return $this;
  64. }
  65. /**
  66. * Enables the use of "__set" by the PropertyAccessor.
  67. *
  68. * @return $this
  69. */
  70. public function enableMagicSet(): static
  71. {
  72. $this->magicMethods |= PropertyAccessor::MAGIC_SET;
  73. return $this;
  74. }
  75. /**
  76. * Disables the use of "__call" by the PropertyAccessor.
  77. *
  78. * @return $this
  79. */
  80. public function disableMagicCall(): static
  81. {
  82. $this->magicMethods &= ~PropertyAccessor::MAGIC_CALL;
  83. return $this;
  84. }
  85. /**
  86. * Disables the use of "__get" by the PropertyAccessor.
  87. *
  88. * @return $this
  89. */
  90. public function disableMagicGet(): static
  91. {
  92. $this->magicMethods &= ~PropertyAccessor::MAGIC_GET;
  93. return $this;
  94. }
  95. /**
  96. * Disables the use of "__set" by the PropertyAccessor.
  97. *
  98. * @return $this
  99. */
  100. public function disableMagicSet(): static
  101. {
  102. $this->magicMethods &= ~PropertyAccessor::MAGIC_SET;
  103. return $this;
  104. }
  105. /**
  106. * @return bool whether the use of "__call" by the PropertyAccessor is enabled
  107. */
  108. public function isMagicCallEnabled(): bool
  109. {
  110. return (bool) ($this->magicMethods & PropertyAccessor::MAGIC_CALL);
  111. }
  112. /**
  113. * @return bool whether the use of "__get" by the PropertyAccessor is enabled
  114. */
  115. public function isMagicGetEnabled(): bool
  116. {
  117. return $this->magicMethods & PropertyAccessor::MAGIC_GET;
  118. }
  119. /**
  120. * @return bool whether the use of "__set" by the PropertyAccessor is enabled
  121. */
  122. public function isMagicSetEnabled(): bool
  123. {
  124. return $this->magicMethods & PropertyAccessor::MAGIC_SET;
  125. }
  126. /**
  127. * Enables exceptions when reading a non-existing index.
  128. *
  129. * This has no influence on writing non-existing indices with PropertyAccessorInterface::setValue()
  130. * which are always created on-the-fly.
  131. *
  132. * @return $this
  133. */
  134. public function enableExceptionOnInvalidIndex(): static
  135. {
  136. $this->throwExceptionOnInvalidIndex = true;
  137. return $this;
  138. }
  139. /**
  140. * Disables exceptions when reading a non-existing index.
  141. *
  142. * Instead, null is returned when calling PropertyAccessorInterface::getValue() on a non-existing index.
  143. *
  144. * @return $this
  145. */
  146. public function disableExceptionOnInvalidIndex(): static
  147. {
  148. $this->throwExceptionOnInvalidIndex = false;
  149. return $this;
  150. }
  151. /**
  152. * @return bool whether an exception is thrown or null is returned when reading a non-existing index
  153. */
  154. public function isExceptionOnInvalidIndexEnabled(): bool
  155. {
  156. return $this->throwExceptionOnInvalidIndex;
  157. }
  158. /**
  159. * Enables exceptions when reading a non-existing property.
  160. *
  161. * This has no influence on writing non-existing indices with PropertyAccessorInterface::setValue()
  162. * which are always created on-the-fly.
  163. *
  164. * @return $this
  165. */
  166. public function enableExceptionOnInvalidPropertyPath(): static
  167. {
  168. $this->throwExceptionOnInvalidPropertyPath = true;
  169. return $this;
  170. }
  171. /**
  172. * Disables exceptions when reading a non-existing index.
  173. *
  174. * Instead, null is returned when calling PropertyAccessorInterface::getValue() on a non-existing index.
  175. *
  176. * @return $this
  177. */
  178. public function disableExceptionOnInvalidPropertyPath(): static
  179. {
  180. $this->throwExceptionOnInvalidPropertyPath = false;
  181. return $this;
  182. }
  183. /**
  184. * @return bool whether an exception is thrown or null is returned when reading a non-existing property
  185. */
  186. public function isExceptionOnInvalidPropertyPath(): bool
  187. {
  188. return $this->throwExceptionOnInvalidPropertyPath;
  189. }
  190. /**
  191. * Sets a cache system.
  192. *
  193. * @return $this
  194. */
  195. public function setCacheItemPool(?CacheItemPoolInterface $cacheItemPool = null): static
  196. {
  197. if (1 > \func_num_args()) {
  198. trigger_deprecation('symfony/property-access', '6.2', 'Calling "%s()" without any arguments is deprecated, pass null explicitly instead.', __METHOD__);
  199. }
  200. $this->cacheItemPool = $cacheItemPool;
  201. return $this;
  202. }
  203. /**
  204. * Gets the used cache system.
  205. */
  206. public function getCacheItemPool(): ?CacheItemPoolInterface
  207. {
  208. return $this->cacheItemPool;
  209. }
  210. /**
  211. * @return $this
  212. */
  213. public function setReadInfoExtractor(?PropertyReadInfoExtractorInterface $readInfoExtractor): static
  214. {
  215. $this->readInfoExtractor = $readInfoExtractor;
  216. return $this;
  217. }
  218. public function getReadInfoExtractor(): ?PropertyReadInfoExtractorInterface
  219. {
  220. return $this->readInfoExtractor;
  221. }
  222. /**
  223. * @return $this
  224. */
  225. public function setWriteInfoExtractor(?PropertyWriteInfoExtractorInterface $writeInfoExtractor): static
  226. {
  227. $this->writeInfoExtractor = $writeInfoExtractor;
  228. return $this;
  229. }
  230. public function getWriteInfoExtractor(): ?PropertyWriteInfoExtractorInterface
  231. {
  232. return $this->writeInfoExtractor;
  233. }
  234. /**
  235. * Builds and returns a new PropertyAccessor object.
  236. */
  237. public function getPropertyAccessor(): PropertyAccessorInterface
  238. {
  239. $throw = PropertyAccessor::DO_NOT_THROW;
  240. if ($this->throwExceptionOnInvalidIndex) {
  241. $throw |= PropertyAccessor::THROW_ON_INVALID_INDEX;
  242. }
  243. if ($this->throwExceptionOnInvalidPropertyPath) {
  244. $throw |= PropertyAccessor::THROW_ON_INVALID_PROPERTY_PATH;
  245. }
  246. return new PropertyAccessor($this->magicMethods, $throw, $this->cacheItemPool, $this->readInfoExtractor, $this->writeInfoExtractor);
  247. }
  248. }