PropertyAccessorBuilder.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. /**
  22. * @var CacheItemPoolInterface|null
  23. */
  24. private $cacheItemPool;
  25. /**
  26. * Enables the use of "__call" by the PropertyAccessor.
  27. *
  28. * @return $this
  29. */
  30. public function enableMagicCall()
  31. {
  32. $this->magicCall = true;
  33. return $this;
  34. }
  35. /**
  36. * Disables the use of "__call" by the PropertyAccessor.
  37. *
  38. * @return $this
  39. */
  40. public function disableMagicCall()
  41. {
  42. $this->magicCall = false;
  43. return $this;
  44. }
  45. /**
  46. * @return bool whether the use of "__call" by the PropertyAccessor is enabled
  47. */
  48. public function isMagicCallEnabled()
  49. {
  50. return $this->magicCall;
  51. }
  52. /**
  53. * Enables exceptions when reading a non-existing index.
  54. *
  55. * This has no influence on writing non-existing indices with PropertyAccessorInterface::setValue()
  56. * which are always created on-the-fly.
  57. *
  58. * @return $this
  59. */
  60. public function enableExceptionOnInvalidIndex()
  61. {
  62. $this->throwExceptionOnInvalidIndex = true;
  63. return $this;
  64. }
  65. /**
  66. * Disables exceptions when reading a non-existing index.
  67. *
  68. * Instead, null is returned when calling PropertyAccessorInterface::getValue() on a non-existing index.
  69. *
  70. * @return $this
  71. */
  72. public function disableExceptionOnInvalidIndex()
  73. {
  74. $this->throwExceptionOnInvalidIndex = false;
  75. return $this;
  76. }
  77. /**
  78. * @return bool whether an exception is thrown or null is returned when reading a non-existing index
  79. */
  80. public function isExceptionOnInvalidIndexEnabled()
  81. {
  82. return $this->throwExceptionOnInvalidIndex;
  83. }
  84. /**
  85. * Sets a cache system.
  86. *
  87. * @param CacheItemPoolInterface|null $cacheItemPool
  88. *
  89. * @return PropertyAccessorBuilder The builder object
  90. */
  91. public function setCacheItemPool(CacheItemPoolInterface $cacheItemPool = null)
  92. {
  93. $this->cacheItemPool = $cacheItemPool;
  94. return $this;
  95. }
  96. /**
  97. * Gets the used cache system.
  98. *
  99. * @return CacheItemPoolInterface|null
  100. */
  101. public function getCacheItemPool()
  102. {
  103. return $this->cacheItemPool;
  104. }
  105. /**
  106. * Builds and returns a new PropertyAccessor object.
  107. *
  108. * @return PropertyAccessorInterface The built PropertyAccessor
  109. */
  110. public function getPropertyAccessor()
  111. {
  112. return new PropertyAccessor($this->magicCall, $this->throwExceptionOnInvalidIndex, $this->cacheItemPool);
  113. }
  114. }