vendor/symfony/form/Extension/Core/DataAccessor/ChainAccessor.php line 54

Open in your IDE?
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <[email protected]>
  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\Form\Extension\Core\DataAccessor;
  11. use Symfony\Component\Form\DataAccessorInterface;
  12. use Symfony\Component\Form\Exception\AccessException;
  13. use Symfony\Component\Form\FormInterface;
  14. /**
  15. * @author Yonel Ceruto <[email protected]>
  16. */
  17. class ChainAccessor implements DataAccessorInterface
  18. {
  19. private $accessors;
  20. /**
  21. * @param DataAccessorInterface[]|iterable $accessors
  22. */
  23. public function __construct(iterable $accessors)
  24. {
  25. $this->accessors = $accessors;
  26. }
  27. /**
  28. * {@inheritdoc}
  29. */
  30. public function getValue($data, FormInterface $form)
  31. {
  32. foreach ($this->accessors as $accessor) {
  33. if ($accessor->isReadable($data, $form)) {
  34. return $accessor->getValue($data, $form);
  35. }
  36. }
  37. throw new AccessException('Unable to read from the given form data as no accessor in the chain is able to read the data.');
  38. }
  39. /**
  40. * {@inheritdoc}
  41. */
  42. public function setValue(&$data, $value, FormInterface $form): void
  43. {
  44. foreach ($this->accessors as $accessor) {
  45. if ($accessor->isWritable($data, $form)) {
  46. $accessor->setValue($data, $value, $form);
  47. return;
  48. }
  49. }
  50. throw new AccessException('Unable to write the given value as no accessor in the chain is able to set the data.');
  51. }
  52. /**
  53. * {@inheritdoc}
  54. */
  55. public function isReadable($data, FormInterface $form): bool
  56. {
  57. foreach ($this->accessors as $accessor) {
  58. if ($accessor->isReadable($data, $form)) {
  59. return true;
  60. }
  61. }
  62. return false;
  63. }
  64. /**
  65. * {@inheritdoc}
  66. */
  67. public function isWritable($data, FormInterface $form): bool
  68. {
  69. foreach ($this->accessors as $accessor) {
  70. if ($accessor->isWritable($data, $form)) {
  71. return true;
  72. }
  73. }
  74. return false;
  75. }
  76. }