PropertyAccessorBuilder.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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. /** @var int */
  22. private $magicMethods = PropertyAccessor::MAGIC_GET | PropertyAccessor::MAGIC_SET;
  23. private $throwExceptionOnInvalidIndex = false;
  24. private $throwExceptionOnInvalidPropertyPath = true;
  25. /**
  26. * @var CacheItemPoolInterface|null
  27. */
  28. private $cacheItemPool;
  29. /**
  30. * @var PropertyReadInfoExtractorInterface|null
  31. */
  32. private $readInfoExtractor;
  33. /**
  34. * @var PropertyWriteInfoExtractorInterface|null
  35. */
  36. private $writeInfoExtractor;
  37. /**
  38. * Enables the use of all magic methods by the PropertyAccessor.
  39. */
  40. public function enableMagicMethods(): self
  41. {
  42. $this->magicMethods = PropertyAccessor::MAGIC_GET | PropertyAccessor::MAGIC_SET | PropertyAccessor::MAGIC_CALL;
  43. return $this;
  44. }
  45. /**
  46. * Disable the use of all magic methods by the PropertyAccessor.
  47. */
  48. public function disableMagicMethods(): self
  49. {
  50. $this->magicMethods = PropertyAccessor::DISALLOW_MAGIC_METHODS;
  51. return $this;
  52. }
  53. /**
  54. * Enables the use of "__call" by the PropertyAccessor.
  55. *
  56. * @return $this
  57. */
  58. public function enableMagicCall()
  59. {
  60. $this->magicMethods |= PropertyAccessor::MAGIC_CALL;
  61. return $this;
  62. }
  63. /**
  64. * Enables the use of "__get" by the PropertyAccessor.
  65. */
  66. public function enableMagicGet(): self
  67. {
  68. $this->magicMethods |= PropertyAccessor::MAGIC_GET;
  69. return $this;
  70. }
  71. /**
  72. * Enables the use of "__set" by the PropertyAccessor.
  73. */
  74. public function enableMagicSet(): self
  75. {
  76. $this->magicMethods |= PropertyAccessor::MAGIC_SET;
  77. return $this;
  78. }
  79. /**
  80. * Disables the use of "__call" by the PropertyAccessor.
  81. *
  82. * @return $this
  83. */
  84. public function disableMagicCall()
  85. {
  86. $this->magicMethods &= ~PropertyAccessor::MAGIC_CALL;
  87. return $this;
  88. }
  89. /**
  90. * Disables the use of "__get" by the PropertyAccessor.
  91. */
  92. public function disableMagicGet(): self
  93. {
  94. $this->magicMethods &= ~PropertyAccessor::MAGIC_GET;
  95. return $this;
  96. }
  97. /**
  98. * Disables the use of "__set" by the PropertyAccessor.
  99. */
  100. public function disableMagicSet(): self
  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()
  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()
  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()
  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()
  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()
  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()
  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()
  187. {
  188. return $this->throwExceptionOnInvalidPropertyPath;
  189. }
  190. /**
  191. * Sets a cache system.
  192. *
  193. * @return PropertyAccessorBuilder The builder object
  194. */
  195. public function setCacheItemPool(CacheItemPoolInterface $cacheItemPool = null)
  196. {
  197. $this->cacheItemPool = $cacheItemPool;
  198. return $this;
  199. }
  200. /**
  201. * Gets the used cache system.
  202. *
  203. * @return CacheItemPoolInterface|null
  204. */
  205. public function getCacheItemPool()
  206. {
  207. return $this->cacheItemPool;
  208. }
  209. /**
  210. * @return $this
  211. */
  212. public function setReadInfoExtractor(?PropertyReadInfoExtractorInterface $readInfoExtractor)
  213. {
  214. $this->readInfoExtractor = $readInfoExtractor;
  215. return $this;
  216. }
  217. public function getReadInfoExtractor(): ?PropertyReadInfoExtractorInterface
  218. {
  219. return $this->readInfoExtractor;
  220. }
  221. /**
  222. * @return $this
  223. */
  224. public function setWriteInfoExtractor(?PropertyWriteInfoExtractorInterface $writeInfoExtractor)
  225. {
  226. $this->writeInfoExtractor = $writeInfoExtractor;
  227. return $this;
  228. }
  229. public function getWriteInfoExtractor(): ?PropertyWriteInfoExtractorInterface
  230. {
  231. return $this->writeInfoExtractor;
  232. }
  233. /**
  234. * Builds and returns a new PropertyAccessor object.
  235. *
  236. * @return PropertyAccessorInterface The built PropertyAccessor
  237. */
  238. public function getPropertyAccessor()
  239. {
  240. return new PropertyAccessor($this->magicMethods, $this->throwExceptionOnInvalidIndex, $this->cacheItemPool, $this->throwExceptionOnInvalidPropertyPath, $this->readInfoExtractor, $this->writeInfoExtractor);
  241. }
  242. }