PropertyAccessorBuilder.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. /**
  12. * A configurable builder to create a PropertyAccessor.
  13. *
  14. * @author Jérémie Augustin <jeremie.augustin@pixel-cookers.com>
  15. */
  16. class PropertyAccessorBuilder
  17. {
  18. /**
  19. * @var bool
  20. */
  21. private $magicCall = false;
  22. /**
  23. * @var bool
  24. */
  25. private $throwExceptionOnInvalidIndex = false;
  26. /**
  27. * Enables the use of "__call" by the PropertyAccessor.
  28. *
  29. * @return PropertyAccessorBuilder The builder object
  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 PropertyAccessorBuilder The builder object
  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 PropertyAccessorBuilder The builder object
  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 PropertyAccessorBuilder The builder object
  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. * Builds and returns a new PropertyAccessor object.
  87. *
  88. * @return PropertyAccessorInterface The built PropertyAccessor
  89. */
  90. public function getPropertyAccessor()
  91. {
  92. return new PropertyAccessor($this->magicCall, $this->throwExceptionOnInvalidIndex);
  93. }
  94. }