Без опису
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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. * @return $this
  41. */
  42. public function enableMagicMethods(): self
  43. {
  44. $this->magicMethods = PropertyAccessor::MAGIC_GET | PropertyAccessor::MAGIC_SET | PropertyAccessor::MAGIC_CALL;
  45. return $this;
  46. }
  47. /**
  48. * Disable the use of all magic methods by the PropertyAccessor.
  49. *
  50. * @return $this
  51. */
  52. public function disableMagicMethods(): self
  53. {
  54. $this->magicMethods = PropertyAccessor::DISALLOW_MAGIC_METHODS;
  55. return $this;
  56. }
  57. /**
  58. * Enables the use of "__call" by the PropertyAccessor.
  59. *
  60. * @return $this
  61. */
  62. public function enableMagicCall()
  63. {
  64. $this->magicMethods |= PropertyAccessor::MAGIC_CALL;
  65. return $this;
  66. }
  67. /**
  68. * Enables the use of "__get" by the PropertyAccessor.
  69. */
  70. public function enableMagicGet(): self
  71. {
  72. $this->magicMethods |= PropertyAccessor::MAGIC_GET;
  73. return $this;
  74. }
  75. /**
  76. * Enables the use of "__set" by the PropertyAccessor.
  77. *
  78. * @return $this
  79. */
  80. public function enableMagicSet(): self
  81. {
  82. $this->magicMethods |= PropertyAccessor::MAGIC_SET;
  83. return $this;
  84. }
  85. /**
  86. * Disables the use of "__call" by the PropertyAccessor.
  87. *
  88. * @return $this
  89. */
  90. public function disableMagicCall()
  91. {
  92. $this->magicMethods &= ~PropertyAccessor::MAGIC_CALL;
  93. return $this;
  94. }
  95. /**
  96. * Disables the use of "__get" by the PropertyAccessor.
  97. *
  98. * @return $this
  99. */
  100. public function disableMagicGet(): self
  101. {
  102. $this->magicMethods &= ~PropertyAccessor::MAGIC_GET;
  103. return $this;
  104. }
  105. /**
  106. * Disables the use of "__set" by the PropertyAccessor.
  107. *
  108. * @return $this
  109. */
  110. public function disableMagicSet(): self
  111. {
  112. $this->magicMethods &= ~PropertyAccessor::MAGIC_SET;
  113. return $this;
  114. }
  115. /**
  116. * @return bool whether the use of "__call" by the PropertyAccessor is enabled
  117. */
  118. public function isMagicCallEnabled()
  119. {
  120. return (bool) ($this->magicMethods & PropertyAccessor::MAGIC_CALL);
  121. }
  122. /**
  123. * @return bool whether the use of "__get" by the PropertyAccessor is enabled
  124. */
  125. public function isMagicGetEnabled(): bool
  126. {
  127. return $this->magicMethods & PropertyAccessor::MAGIC_GET;
  128. }
  129. /**
  130. * @return bool whether the use of "__set" by the PropertyAccessor is enabled
  131. */
  132. public function isMagicSetEnabled(): bool
  133. {
  134. return $this->magicMethods & PropertyAccessor::MAGIC_SET;
  135. }
  136. /**
  137. * Enables exceptions when reading a non-existing index.
  138. *
  139. * This has no influence on writing non-existing indices with PropertyAccessorInterface::setValue()
  140. * which are always created on-the-fly.
  141. *
  142. * @return $this
  143. */
  144. public function enableExceptionOnInvalidIndex()
  145. {
  146. $this->throwExceptionOnInvalidIndex = true;
  147. return $this;
  148. }
  149. /**
  150. * Disables exceptions when reading a non-existing index.
  151. *
  152. * Instead, null is returned when calling PropertyAccessorInterface::getValue() on a non-existing index.
  153. *
  154. * @return $this
  155. */
  156. public function disableExceptionOnInvalidIndex()
  157. {
  158. $this->throwExceptionOnInvalidIndex = false;
  159. return $this;
  160. }
  161. /**
  162. * @return bool whether an exception is thrown or null is returned when reading a non-existing index
  163. */
  164. public function isExceptionOnInvalidIndexEnabled()
  165. {
  166. return $this->throwExceptionOnInvalidIndex;
  167. }
  168. /**
  169. * Enables exceptions when reading a non-existing property.
  170. *
  171. * This has no influence on writing non-existing indices with PropertyAccessorInterface::setValue()
  172. * which are always created on-the-fly.
  173. *
  174. * @return $this
  175. */
  176. public function enableExceptionOnInvalidPropertyPath()
  177. {
  178. $this->throwExceptionOnInvalidPropertyPath = true;
  179. return $this;
  180. }
  181. /**
  182. * Disables exceptions when reading a non-existing index.
  183. *
  184. * Instead, null is returned when calling PropertyAccessorInterface::getValue() on a non-existing index.
  185. *
  186. * @return $this
  187. */
  188. public function disableExceptionOnInvalidPropertyPath()
  189. {
  190. $this->throwExceptionOnInvalidPropertyPath = false;
  191. return $this;
  192. }
  193. /**
  194. * @return bool whether an exception is thrown or null is returned when reading a non-existing property
  195. */
  196. public function isExceptionOnInvalidPropertyPath()
  197. {
  198. return $this->throwExceptionOnInvalidPropertyPath;
  199. }
  200. /**
  201. * Sets a cache system.
  202. *
  203. * @return $this
  204. */
  205. public function setCacheItemPool(?CacheItemPoolInterface $cacheItemPool = null)
  206. {
  207. $this->cacheItemPool = $cacheItemPool;
  208. return $this;
  209. }
  210. /**
  211. * Gets the used cache system.
  212. *
  213. * @return CacheItemPoolInterface|null
  214. */
  215. public function getCacheItemPool()
  216. {
  217. return $this->cacheItemPool;
  218. }
  219. /**
  220. * @return $this
  221. */
  222. public function setReadInfoExtractor(?PropertyReadInfoExtractorInterface $readInfoExtractor)
  223. {
  224. $this->readInfoExtractor = $readInfoExtractor;
  225. return $this;
  226. }
  227. public function getReadInfoExtractor(): ?PropertyReadInfoExtractorInterface
  228. {
  229. return $this->readInfoExtractor;
  230. }
  231. /**
  232. * @return $this
  233. */
  234. public function setWriteInfoExtractor(?PropertyWriteInfoExtractorInterface $writeInfoExtractor)
  235. {
  236. $this->writeInfoExtractor = $writeInfoExtractor;
  237. return $this;
  238. }
  239. public function getWriteInfoExtractor(): ?PropertyWriteInfoExtractorInterface
  240. {
  241. return $this->writeInfoExtractor;
  242. }
  243. /**
  244. * Builds and returns a new PropertyAccessor object.
  245. *
  246. * @return PropertyAccessorInterface
  247. */
  248. public function getPropertyAccessor()
  249. {
  250. $throw = PropertyAccessor::DO_NOT_THROW;
  251. if ($this->throwExceptionOnInvalidIndex) {
  252. $throw |= PropertyAccessor::THROW_ON_INVALID_INDEX;
  253. }
  254. if ($this->throwExceptionOnInvalidPropertyPath) {
  255. $throw |= PropertyAccessor::THROW_ON_INVALID_PROPERTY_PATH;
  256. }
  257. return new PropertyAccessor($this->magicMethods, $throw, $this->cacheItemPool, $this->readInfoExtractor, $this->writeInfoExtractor);
  258. }
  259. }