Composite.php 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\Data\Argument\Interpreter;
  7. use Magento\Framework\Data\Argument\InterpreterInterface;
  8. /**
  9. * Interpreter that aggregates named interpreters and delegates every evaluation to one of them
  10. */
  11. class Composite implements InterpreterInterface
  12. {
  13. /**
  14. * Format: array('<name>' => <instance>, ...)
  15. *
  16. * @var InterpreterInterface[]
  17. */
  18. private $interpreters;
  19. /**
  20. * Data key that holds name of an interpreter to be used for that data
  21. *
  22. * @var string
  23. */
  24. private $discriminator;
  25. /**
  26. * @param InterpreterInterface[] $interpreters
  27. * @param string $discriminator
  28. * @throws \InvalidArgumentException
  29. */
  30. public function __construct(array $interpreters, $discriminator)
  31. {
  32. foreach ($interpreters as $interpreterName => $interpreterInstance) {
  33. if (!$interpreterInstance instanceof InterpreterInterface) {
  34. throw new \InvalidArgumentException(
  35. "Interpreter named '{$interpreterName}' is expected to be an argument interpreter instance."
  36. );
  37. }
  38. }
  39. $this->interpreters = $interpreters;
  40. $this->discriminator = $discriminator;
  41. }
  42. /**
  43. * {@inheritdoc}
  44. * @throws \InvalidArgumentException
  45. */
  46. public function evaluate(array $data)
  47. {
  48. if (!isset($data[$this->discriminator])) {
  49. throw new \InvalidArgumentException(
  50. sprintf('Value for key "%s" is missing in the argument data.', $this->discriminator)
  51. );
  52. }
  53. $interpreterName = $data[$this->discriminator];
  54. unset($data[$this->discriminator]);
  55. $interpreter = $this->getInterpreter($interpreterName);
  56. return $interpreter->evaluate($data);
  57. }
  58. /**
  59. * Register interpreter instance under a given unique name
  60. *
  61. * @param string $name
  62. * @param InterpreterInterface $instance
  63. * @return void
  64. * @throws \InvalidArgumentException
  65. */
  66. public function addInterpreter($name, InterpreterInterface $instance)
  67. {
  68. if (isset($this->interpreters[$name])) {
  69. throw new \InvalidArgumentException("Argument interpreter named '{$name}' has already been defined.");
  70. }
  71. $this->interpreters[$name] = $instance;
  72. }
  73. /**
  74. * Retrieve interpreter instance by its unique name
  75. *
  76. * @param string $name
  77. * @return InterpreterInterface
  78. * @throws \InvalidArgumentException
  79. */
  80. protected function getInterpreter($name)
  81. {
  82. if (!isset($this->interpreters[$name])) {
  83. throw new \InvalidArgumentException("Argument interpreter named '{$name}' has not been defined.");
  84. }
  85. return $this->interpreters[$name];
  86. }
  87. }