Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Composite.php
Go to the documentation of this file.
1 <?php
7 
9 
13 class Composite implements ParserInterface
14 {
20  private $parsers;
21 
27  private $discriminator;
28 
34  public function __construct(array $parsers, $discriminator)
35  {
36  foreach ($parsers as $parserName => $parserInstance) {
37  if (!$parserInstance instanceof ParserInterface) {
38  throw new \InvalidArgumentException(
39  "Parser named '{$parserName}' is expected to be an argument parser instance."
40  );
41  }
42  }
43  $this->parsers = $parsers;
44  $this->discriminator = $discriminator;
45  }
46 
51  public function parse(array $data, \DOMNode $node)
52  {
53  if (!isset($data[$this->discriminator])) {
54  throw new \InvalidArgumentException(
55  sprintf('Value for key "%s" is missing in the argument data.', $this->discriminator)
56  );
57  }
58  $parserName = $data[$this->discriminator];
59  $parser = $this->getParser($parserName);
60  return $parser->parse($data, $node);
61  }
62 
71  public function addParser($name, ParserInterface $instance)
72  {
73  if (isset($this->parsers[$name])) {
74  throw new \InvalidArgumentException("Argument parser named '{$name}' has already been defined.");
75  }
76  $this->parsers[$name] = $instance;
77  }
78 
86  private function getParser($name)
87  {
88  if (!isset($this->parsers[$name])) {
89  throw new \InvalidArgumentException("Argument parser named '{$name}' has not been defined.");
90  }
91  return $this->parsers[$name];
92  }
93 }
__construct(array $parsers, $discriminator)
Definition: Composite.php:34
parse(array $data, \DOMNode $node)
Definition: Composite.php:51
addParser($name, ParserInterface $instance)
Definition: Composite.php:71
if(!isset($_GET['name'])) $name
Definition: log.php:14