PropertyAccessorBuilder.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. /**
  13. * A configurable builder to create a PropertyAccessor.
  14. *
  15. * @author Jérémie Augustin <jeremie.augustin@pixel-cookers.com>
  16. */
  17. class PropertyAccessorBuilder
  18. {
  19. private $magicCall = false;
  20. private $throwExceptionOnInvalidIndex = false;
  21. private $throwExceptionOnInvalidPropertyPath = true;
  22. /**
  23. * @var CacheItemPoolInterface|null
  24. */
  25. private $cacheItemPool;
  26. /**
  27. * Enables the use of "__call" by the PropertyAccessor.
  28. *
  29. * @return $this
  30. */
  31. public function enableMagicCall()
  32. {
  33. $this->magicCall = true;
  34. return $this;
  35. }
  36. /**
  37. * Disables the use of "__call" by the PropertyAccessor.
  38. *
  39. * @return $this
  40. */
  41. public function disableMagicCall()
  42. {
  43. $this->magicCall = false;
  44. return $this;
  45. }
  46. /**
  47. * @return bool whether the use of "__call" by the PropertyAccessor is enabled
  48. */
  49. public function isMagicCallEnabled()
  50. {
  51. return $this->magicCall;
  52. }
  53. /**
  54. * Enables exceptions when reading a non-existing index.
  55. *
  56. * This has no influence on writing non-existing indices with PropertyAccessorInterface::setValue()
  57. * which are always created on-the-fly.
  58. *
  59. * @return $this
  60. */
  61. public function enableExceptionOnInvalidIndex()
  62. {
  63. $this->throwExceptionOnInvalidIndex = true;
  64. return $this;
  65. }
  66. /**
  67. * Disables exceptions when reading a non-existing index.
  68. *
  69. * Instead, null is returned when calling PropertyAccessorInterface::getValue() on a non-existing index.
  70. *
  71. * @return $this
  72. */
  73. public function disableExceptionOnInvalidIndex()
  74. {
  75. $this->throwExceptionOnInvalidIndex = false;
  76. return $this;
  77. }
  78. /**
  79. * @return bool whether an exception is thrown or null is returned when reading a non-existing index
  80. */
  81. public function isExceptionOnInvalidIndexEnabled()
  82. {
  83. return $this->throwExceptionOnInvalidIndex;
  84. }
  85. /**
  86. * Enables exceptions when reading a non-existing property.
  87. *
  88. * This has no influence on writing non-existing indices with PropertyAccessorInterface::setValue()
  89. * which are always created on-the-fly.
  90. *
  91. * @return $this
  92. */
  93. public function enableExceptionOnInvalidPropertyPath()
  94. {
  95. $this->throwExceptionOnInvalidPropertyPath = true;
  96. return $this;
  97. }
  98. /**
  99. * Disables exceptions when reading a non-existing index.
  100. *
  101. * Instead, null is returned when calling PropertyAccessorInterface::getValue() on a non-existing index.
  102. *
  103. * @return $this
  104. */
  105. public function disableExceptionOnInvalidPropertyPath()
  106. {
  107. $this->throwExceptionOnInvalidPropertyPath = false;
  108. return $this;
  109. }
  110. /**
  111. * @return bool whether an exception is thrown or null is returned when reading a non-existing property
  112. */
  113. public function isExceptionOnInvalidPropertyPath()
  114. {
  115. return $this->throwExceptionOnInvalidPropertyPath;
  116. }
  117. /**
  118. * Sets a cache system.
  119. *
  120. * @return PropertyAccessorBuilder The builder object
  121. */
  122. public function setCacheItemPool(CacheItemPoolInterface $cacheItemPool = null)
  123. {
  124. $this->cacheItemPool = $cacheItemPool;
  125. return $this;
  126. }
  127. /**
  128. * Gets the used cache system.
  129. *
  130. * @return CacheItemPoolInterface|null
  131. */
  132. public function getCacheItemPool()
  133. {
  134. return $this->cacheItemPool;
  135. }
  136. /**
  137. * Builds and returns a new PropertyAccessor object.
  138. *
  139. * @return PropertyAccessorInterface The built PropertyAccessor
  140. */
  141. public function getPropertyAccessor()
  142. {
  143. return new PropertyAccessor($this->magicCall, $this->throwExceptionOnInvalidIndex, $this->cacheItemPool, $this->throwExceptionOnInvalidPropertyPath);
  144. }
  145. }