Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
TMap.php
Go to the documentation of this file.
1 <?php
7 
9 
14 class TMap implements \IteratorAggregate, \Countable, \ArrayAccess
15 {
19  private $type;
20 
24  private $array = [];
25 
29  private $objectsArray = [];
30 
34  private $counter = 0;
35 
39  private $objectManager;
40 
44  private $configInterface;
45 
49  private $objectCreationStrategy;
50 
58  public function __construct(
59  $type,
60  ObjectManagerInterface $objectManager,
61  ConfigInterface $configInterface,
62  array $array = [],
63  \Closure $objectCreationStrategy = null
64  ) {
65  if (!class_exists($this->type) && !interface_exists($type)) {
66  throw new \InvalidArgumentException(sprintf('Unknown type %s', $type));
67  }
68 
69  $this->type = $type;
70 
71  $this->objectManager = $objectManager;
72  $this->configInterface = $configInterface;
73 
74  array_walk(
75  $array,
76  function ($item, $index) {
77  $this->assertValidTypeLazy($item, $index);
78  }
79  );
80 
81  $this->array = $array;
82  $this->counter = count($array);
83  $this->objectCreationStrategy = $objectCreationStrategy;
84  }
85 
94  private function assertValidTypeLazy($instanceName, $index = null)
95  {
96  $realType = $this->configInterface->getInstanceType(
97  $this->configInterface->getPreference($instanceName)
98  );
99 
100  if (!in_array(
101  $this->type,
102  array_unique(array_merge(class_parents($realType), class_implements($realType))),
103  true
104  )) {
105  $this->throwTypeException($realType, $index);
106  }
107  }
108 
117  private function throwTypeException($inputType, $index)
118  {
119  $message = 'Type mismatch. Expected type: %s. Actual: %s, Code: %s';
120 
121  throw new \InvalidArgumentException(
122  sprintf($message, $this->type, $inputType, $index)
123  );
124  }
125 
132  private function initObject($index)
133  {
134  if (isset($this->objectsArray[$index])) {
135  return $this->objectsArray[$index];
136  }
137 
138  $objectCreationStrategy = $this->objectCreationStrategy;
139  return $this->objectsArray[$index] = $objectCreationStrategy === null
140  ? $this->objectManager->create($this->array[$index])
141  : $objectCreationStrategy($this->objectManager, $this->array[$index]);
142  }
143 
147  public function getIterator()
148  {
149  if (array_keys($this->array) != array_keys($this->objectsArray)) {
150  foreach (array_keys($this->array) as $index) {
151  $this->initObject($index);
152  }
153  }
154 
155  return new \ArrayIterator($this->objectsArray);
156  }
157 
161  public function offsetExists($offset)
162  {
163  return array_key_exists($offset, $this->array);
164  }
165 
169  public function offsetGet($offset)
170  {
171  return isset($this->array[$offset]) ? $this->initObject($offset) : null;
172  }
173 
177  public function offsetSet($offset, $value)
178  {
179  $this->assertValidTypeLazy($value, $offset);
180  if ($offset === null) {
181  $this->array[] = $value;
182  } else {
183  $this->array[$offset] = $value;
184  }
185 
186  $this->counter++;
187  }
188 
192  public function offsetUnset($offset)
193  {
194  if ($this->counter && isset($this->array[$offset])) {
195  $this->counter--;
196  unset($this->array[$offset]);
197 
198  if (isset($this->objectsArray[$offset])) {
199  unset($this->objectsArray[$offset]);
200  }
201  }
202  }
203 
207  public function count()
208  {
209  return $this->counter;
210  }
211 }
__construct( $type, ObjectManagerInterface $objectManager, ConfigInterface $configInterface, array $array=[], \Closure $objectCreationStrategy=null)
Definition: TMap.php:58
$objectManager
Definition: bootstrap.php:17
$message
$value
Definition: gender.phtml:16
$index
Definition: list.phtml:44