-
Notifications
You must be signed in to change notification settings - Fork 9.3k
/
Factory.php
89 lines (82 loc) · 2.01 KB
/
Factory.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
<?php
/**
* Copyright © 2015 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
/**
* @author Magento Core Team <core@magentocommerce.com>
*/
namespace Magento\Framework\Data\Form\Element;
use Magento\Framework\ObjectManagerInterface;
class Factory
{
/**
* @var ObjectManagerInterface
*/
protected $_objectManager;
/**
* Standard library element types
*
* @var string[]
*/
protected $_standardTypes = [
'button',
'checkbox',
'checkboxes',
'column',
'date',
'editablemultiselect',
'editor',
'fieldset',
'file',
'gallery',
'hidden',
'image',
'imagefile',
'label',
'link',
'multiline',
'multiselect',
'note',
'obscure',
'password',
'radio',
'radios',
'reset',
'select',
'submit',
'text',
'textarea',
'time',
];
/**
* @param ObjectManagerInterface $objectManager
*/
public function __construct(ObjectManagerInterface $objectManager)
{
$this->_objectManager = $objectManager;
}
/**
* Factory method
*
* @param string $elementType Standard element type or Custom element class
* @param array $config
* @return AbstractElement
* @throws \InvalidArgumentException
*/
public function create($elementType, array $config = [])
{
if (in_array($elementType, $this->_standardTypes)) {
$className = 'Magento\Framework\Data\Form\Element\\' . ucfirst($elementType);
} else {
$className = $elementType;
}
$element = $this->_objectManager->create($className, $config);
if (!$element instanceof AbstractElement) {
throw new \InvalidArgumentException(
$className . ' doesn\'n extend \Magento\Framework\Data\Form\Element\AbstractElement'
);
}
return $element;
}
}