14 class TMap implements \IteratorAggregate, \Countable, \ArrayAccess
29 private $objectsArray = [];
39 private $objectManager;
44 private $configInterface;
49 private $objectCreationStrategy;
63 \Closure $objectCreationStrategy =
null 65 if (!
class_exists($this->type) && !interface_exists($type)) {
66 throw new \InvalidArgumentException(sprintf(
'Unknown type %s', $type));
72 $this->configInterface = $configInterface;
81 $this->array = $array;
82 $this->counter =
count($array);
83 $this->objectCreationStrategy = $objectCreationStrategy;
94 private function assertValidTypeLazy($instanceName,
$index =
null)
96 $realType = $this->configInterface->getInstanceType(
97 $this->configInterface->getPreference($instanceName)
102 array_unique(array_merge(class_parents($realType), class_implements($realType))),
105 $this->throwTypeException($realType,
$index);
117 private function throwTypeException($inputType,
$index)
119 $message =
'Type mismatch. Expected type: %s. Actual: %s, Code: %s';
121 throw new \InvalidArgumentException(
132 private function initObject(
$index)
134 if (isset($this->objectsArray[
$index])) {
135 return $this->objectsArray[
$index];
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]);
149 if (array_keys($this->array) != array_keys($this->objectsArray)) {
150 foreach (array_keys($this->array) as
$index) {
151 $this->initObject(
$index);
155 return new \ArrayIterator($this->objectsArray);
163 return array_key_exists($offset, $this->array);
171 return isset($this->array[$offset]) ? $this->initObject($offset) :
null;
179 $this->assertValidTypeLazy(
$value, $offset);
180 if ($offset ===
null) {
183 $this->array[$offset] =
$value;
194 if ($this->counter && isset($this->array[$offset])) {
196 unset($this->array[$offset]);
198 if (isset($this->objectsArray[$offset])) {
199 unset($this->objectsArray[$offset]);
209 return $this->counter;
__construct( $type, ObjectManagerInterface $objectManager, ConfigInterface $configInterface, array $array=[], \Closure $objectCreationStrategy=null)
offsetSet($offset, $value)