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 
15 class Composite implements ConverterInterface
16 {
22  private $converters;
23 
29  private $discriminator;
30 
36  public function __construct(array $converters, $discriminator)
37  {
38  foreach ($converters as $converterName => $converterInstance) {
39  if (!$converterInstance instanceof ConverterInterface) {
40  throw new \InvalidArgumentException(
41  "Converter named '{$converterName}' is expected to be an argument converter instance."
42  );
43  }
44  }
45  $this->converters = $converters;
46  $this->discriminator = $discriminator;
47  }
48 
53  public function convert(\DOMNode $node, array $data)
54  {
55  if (!isset($data[$this->discriminator])) {
56  throw new \InvalidArgumentException(
57  sprintf('Value for key "%s" is missing in the argument data.', $this->discriminator)
58  );
59  }
60  $converterName = $data[$this->discriminator];
61  unset($data[$this->discriminator]);
62  $converter = $this->getConverter($converterName);
63  return $converter->convert($node, $data);
64  }
65 
74  public function addConverter($name, ConverterInterface $instance)
75  {
76  if (isset($this->converters[$name])) {
77  throw new \InvalidArgumentException("Argument converter named '{$name}' has already been defined.");
78  }
79  $this->converters[$name] = $instance;
80  }
81 
89  private function getConverter($name)
90  {
91  if (!isset($this->converters[$name])) {
92  throw new \InvalidArgumentException("Argument converter named '{$name}' has not been defined.");
93  }
94  return $this->converters[$name];
95  }
96 }
__construct(array $converters, $discriminator)
Definition: Composite.php:36
convert(\DOMNode $node, array $data)
Definition: Composite.php:53
addConverter($name, ConverterInterface $instance)
Definition: Composite.php:74
if(!isset($_GET['name'])) $name
Definition: log.php:14