Composite.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Ui\Config\Argument\Parser;
  7. use Magento\Ui\Config\Argument\ParserInterface;
  8. /**
  9. * This class is a composite of parsers which converts XML nodes to array
  10. */
  11. class Composite implements ParserInterface
  12. {
  13. /**
  14. * Format: array('<name>' => <instance>, ...)
  15. *
  16. * @var ParserInterface[]
  17. */
  18. private $parsers;
  19. /**
  20. * Data key that holds name of an parser to be used for that data
  21. *
  22. * @var string
  23. */
  24. private $discriminator;
  25. /**
  26. * @param ParserInterface[] $parsers
  27. * @param string $discriminator
  28. * @throws \InvalidArgumentException if parser isn't implement parser interface
  29. */
  30. public function __construct(array $parsers, $discriminator)
  31. {
  32. foreach ($parsers as $parserName => $parserInstance) {
  33. if (!$parserInstance instanceof ParserInterface) {
  34. throw new \InvalidArgumentException(
  35. "Parser named '{$parserName}' is expected to be an argument parser instance."
  36. );
  37. }
  38. }
  39. $this->parsers = $parsers;
  40. $this->discriminator = $discriminator;
  41. }
  42. /**
  43. * @inheritdoc
  44. * @throws \InvalidArgumentException if discriminator isn't passed
  45. */
  46. public function parse(array $data, \DOMNode $node)
  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. $parserName = $data[$this->discriminator];
  54. $parser = $this->getParser($parserName);
  55. return $parser->parse($data, $node);
  56. }
  57. /**
  58. * Register parser instance under a given unique name
  59. *
  60. * @param string $name
  61. * @param ParserInterface $instance
  62. * @return void
  63. * @throws \InvalidArgumentException if parser has already been defined
  64. */
  65. public function addParser($name, ParserInterface $instance)
  66. {
  67. if (isset($this->parsers[$name])) {
  68. throw new \InvalidArgumentException("Argument parser named '{$name}' has already been defined.");
  69. }
  70. $this->parsers[$name] = $instance;
  71. }
  72. /**
  73. * Retrieve parser instance by its unique name
  74. *
  75. * @param string $name
  76. * @return ParserInterface
  77. * @throws \InvalidArgumentException if the parser hasn't already been defined
  78. */
  79. private function getParser($name)
  80. {
  81. if (!isset($this->parsers[$name])) {
  82. throw new \InvalidArgumentException("Argument parser named '{$name}' has not been defined.");
  83. }
  84. return $this->parsers[$name];
  85. }
  86. }