PropertyAccessorBuilder.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. private $magicCall = false;
  19. private $throwExceptionOnInvalidIndex = false;
  20. /**
  21. * Enables the use of "__call" by the PropertyAccessor.
  22. *
  23. * @return $this
  24. */
  25. public function enableMagicCall()
  26. {
  27. $this->magicCall = true;
  28. return $this;
  29. }
  30. /**
  31. * Disables the use of "__call" by the PropertyAccessor.
  32. *
  33. * @return $this
  34. */
  35. public function disableMagicCall()
  36. {
  37. $this->magicCall = false;
  38. return $this;
  39. }
  40. /**
  41. * @return bool whether the use of "__call" by the PropertyAccessor is enabled
  42. */
  43. public function isMagicCallEnabled()
  44. {
  45. return $this->magicCall;
  46. }
  47. /**
  48. * Enables exceptions when reading a non-existing index.
  49. *
  50. * This has no influence on writing non-existing indices with PropertyAccessorInterface::setValue()
  51. * which are always created on-the-fly.
  52. *
  53. * @return $this
  54. */
  55. public function enableExceptionOnInvalidIndex()
  56. {
  57. $this->throwExceptionOnInvalidIndex = true;
  58. return $this;
  59. }
  60. /**
  61. * Disables exceptions when reading a non-existing index.
  62. *
  63. * Instead, null is returned when calling PropertyAccessorInterface::getValue() on a non-existing index.
  64. *
  65. * @return $this
  66. */
  67. public function disableExceptionOnInvalidIndex()
  68. {
  69. $this->throwExceptionOnInvalidIndex = false;
  70. return $this;
  71. }
  72. /**
  73. * @return bool whether an exception is thrown or null is returned when reading a non-existing index
  74. */
  75. public function isExceptionOnInvalidIndexEnabled()
  76. {
  77. return $this->throwExceptionOnInvalidIndex;
  78. }
  79. /**
  80. * Builds and returns a new PropertyAccessor object.
  81. *
  82. * @return PropertyAccessorInterface The built PropertyAccessor
  83. */
  84. public function getPropertyAccessor()
  85. {
  86. return new PropertyAccessor($this->magicCall, $this->throwExceptionOnInvalidIndex);
  87. }
  88. }