PropertyAccessorBuilder.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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 $magicCall = false;
  22. private $throwExceptionOnInvalidIndex = false;
  23. private $throwExceptionOnInvalidPropertyPath = true;
  24. /**
  25. * @var CacheItemPoolInterface|null
  26. */
  27. private $cacheItemPool;
  28. /**
  29. * @var PropertyReadInfoExtractorInterface|null
  30. */
  31. private $readInfoExtractor;
  32. /**
  33. * @var PropertyWriteInfoExtractorInterface|null
  34. */
  35. private $writeInfoExtractor;
  36. /**
  37. * Enables the use of "__call" by the PropertyAccessor.
  38. *
  39. * @return $this
  40. */
  41. public function enableMagicCall()
  42. {
  43. $this->magicCall = true;
  44. return $this;
  45. }
  46. /**
  47. * Disables the use of "__call" by the PropertyAccessor.
  48. *
  49. * @return $this
  50. */
  51. public function disableMagicCall()
  52. {
  53. $this->magicCall = false;
  54. return $this;
  55. }
  56. /**
  57. * @return bool whether the use of "__call" by the PropertyAccessor is enabled
  58. */
  59. public function isMagicCallEnabled()
  60. {
  61. return $this->magicCall;
  62. }
  63. /**
  64. * Enables exceptions when reading a non-existing index.
  65. *
  66. * This has no influence on writing non-existing indices with PropertyAccessorInterface::setValue()
  67. * which are always created on-the-fly.
  68. *
  69. * @return $this
  70. */
  71. public function enableExceptionOnInvalidIndex()
  72. {
  73. $this->throwExceptionOnInvalidIndex = true;
  74. return $this;
  75. }
  76. /**
  77. * Disables exceptions when reading a non-existing index.
  78. *
  79. * Instead, null is returned when calling PropertyAccessorInterface::getValue() on a non-existing index.
  80. *
  81. * @return $this
  82. */
  83. public function disableExceptionOnInvalidIndex()
  84. {
  85. $this->throwExceptionOnInvalidIndex = false;
  86. return $this;
  87. }
  88. /**
  89. * @return bool whether an exception is thrown or null is returned when reading a non-existing index
  90. */
  91. public function isExceptionOnInvalidIndexEnabled()
  92. {
  93. return $this->throwExceptionOnInvalidIndex;
  94. }
  95. /**
  96. * Enables exceptions when reading a non-existing property.
  97. *
  98. * This has no influence on writing non-existing indices with PropertyAccessorInterface::setValue()
  99. * which are always created on-the-fly.
  100. *
  101. * @return $this
  102. */
  103. public function enableExceptionOnInvalidPropertyPath()
  104. {
  105. $this->throwExceptionOnInvalidPropertyPath = true;
  106. return $this;
  107. }
  108. /**
  109. * Disables exceptions when reading a non-existing index.
  110. *
  111. * Instead, null is returned when calling PropertyAccessorInterface::getValue() on a non-existing index.
  112. *
  113. * @return $this
  114. */
  115. public function disableExceptionOnInvalidPropertyPath()
  116. {
  117. $this->throwExceptionOnInvalidPropertyPath = false;
  118. return $this;
  119. }
  120. /**
  121. * @return bool whether an exception is thrown or null is returned when reading a non-existing property
  122. */
  123. public function isExceptionOnInvalidPropertyPath()
  124. {
  125. return $this->throwExceptionOnInvalidPropertyPath;
  126. }
  127. /**
  128. * Sets a cache system.
  129. *
  130. * @return PropertyAccessorBuilder The builder object
  131. */
  132. public function setCacheItemPool(CacheItemPoolInterface $cacheItemPool = null)
  133. {
  134. $this->cacheItemPool = $cacheItemPool;
  135. return $this;
  136. }
  137. /**
  138. * Gets the used cache system.
  139. *
  140. * @return CacheItemPoolInterface|null
  141. */
  142. public function getCacheItemPool()
  143. {
  144. return $this->cacheItemPool;
  145. }
  146. /**
  147. * @return $this
  148. */
  149. public function setReadInfoExtractor(?PropertyReadInfoExtractorInterface $readInfoExtractor)
  150. {
  151. $this->readInfoExtractor = $readInfoExtractor;
  152. return $this;
  153. }
  154. public function getReadInfoExtractor(): ?PropertyReadInfoExtractorInterface
  155. {
  156. return $this->readInfoExtractor;
  157. }
  158. /**
  159. * @return $this
  160. */
  161. public function setWriteInfoExtractor(?PropertyWriteInfoExtractorInterface $writeInfoExtractor)
  162. {
  163. $this->writeInfoExtractor = $writeInfoExtractor;
  164. return $this;
  165. }
  166. public function getWriteInfoExtractor(): ?PropertyWriteInfoExtractorInterface
  167. {
  168. return $this->writeInfoExtractor;
  169. }
  170. /**
  171. * Builds and returns a new PropertyAccessor object.
  172. *
  173. * @return PropertyAccessorInterface The built PropertyAccessor
  174. */
  175. public function getPropertyAccessor()
  176. {
  177. return new PropertyAccessor($this->magicCall, $this->throwExceptionOnInvalidIndex, $this->cacheItemPool, $this->throwExceptionOnInvalidPropertyPath, $this->readInfoExtractor, $this->writeInfoExtractor);
  178. }
  179. }