Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
CollectorFactory.php
Go to the documentation of this file.
1 <?php
8 
11 
16 {
20  const TYPE_INTERACTIVE = 'interactive';
21 
25  const TYPE_SIMPLE = 'simple';
26 
32  private $objectManager;
33 
37  private $types = [];
38 
45  public function __construct(
46  ObjectManagerInterface $objectManager,
47  array $types = []
48  ) {
49  $this->objectManager = $objectManager;
50  $this->types = $types;
51  }
52 
65  public function create($type)
66  {
67  if (!isset($this->types[$type])) {
68  throw new LocalizedException(
69  __('The class for "%1" type wasn\'t declared. Enter the class and try again.', $type)
70  );
71  }
72 
73  $object = $this->objectManager->create($this->types[$type]);
74 
75  if (!$object instanceof CollectorInterface) {
76  throw new LocalizedException(
77  __('%1 does not implement %2', get_class($object), CollectorInterface::class)
78  );
79  }
80 
81  return $object;
82  }
83 }
$objectManager
Definition: bootstrap.php:17
__()
Definition: __.php:13
$type
Definition: item.phtml:13
__construct(ObjectManagerInterface $objectManager, array $types=[])