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 
14 {
20  private $interpreters;
21 
27  private $discriminator;
28 
34  public function __construct(array $interpreters, $discriminator)
35  {
36  foreach ($interpreters as $interpreterName => $interpreterInstance) {
37  if (!$interpreterInstance instanceof InterpreterInterface) {
38  throw new \InvalidArgumentException(
39  "Interpreter named '{$interpreterName}' is expected to be an argument interpreter instance."
40  );
41  }
42  }
43  $this->interpreters = $interpreters;
44  $this->discriminator = $discriminator;
45  }
46 
51  public function evaluate(array $data)
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  $interpreterName = $data[$this->discriminator];
59  unset($data[$this->discriminator]);
60  $interpreter = $this->getInterpreter($interpreterName);
61  return $interpreter->evaluate($data);
62  }
63 
72  public function addInterpreter($name, InterpreterInterface $instance)
73  {
74  if (isset($this->interpreters[$name])) {
75  throw new \InvalidArgumentException("Argument interpreter named '{$name}' has already been defined.");
76  }
77  $this->interpreters[$name] = $instance;
78  }
79 
87  protected function getInterpreter($name)
88  {
89  if (!isset($this->interpreters[$name])) {
90  throw new \InvalidArgumentException("Argument interpreter named '{$name}' has not been defined.");
91  }
92  return $this->interpreters[$name];
93  }
94 }
addInterpreter($name, InterpreterInterface $instance)
Definition: Composite.php:72
__construct(array $interpreters, $discriminator)
Definition: Composite.php:34
if(!isset($_GET['name'])) $name
Definition: log.php:14