1 | <?php |
||
2 | |||
3 | namespace Nip\Form\Traits; |
||
4 | |||
5 | use Nip\Form\Elements\AbstractElement as ElementAbstract; |
||
6 | |||
7 | /** |
||
8 | * Class NewElementsMethods |
||
9 | * @package Nip\Form\Traits |
||
10 | */ |
||
11 | trait NewElementsMethods |
||
12 | { |
||
13 | /** |
||
14 | * @return ElementAbstract|\Nip_Form_Element_Select |
||
15 | */ |
||
16 | 1 | public function getNewSelectElement() |
|
17 | { |
||
18 | 1 | return $this->getNewElement('select'); |
|
19 | } |
||
20 | |||
21 | /** |
||
22 | * @param $name |
||
23 | * @param bool $label |
||
24 | * @param string $type |
||
25 | * @param bool $isRequired |
||
26 | * @param array $options |
||
27 | * @return $this |
||
28 | */ |
||
29 | 5 | public function add($name, $label = false, $type = 'input', $isRequired = false, $options = []) |
|
30 | { |
||
31 | 5 | $label = ($label) ? $label : ucfirst($name); |
|
32 | 5 | $element = $this->getNewElement($type); |
|
33 | 5 | $element->setName($name); |
|
34 | 5 | $element->setLabel($label); |
|
35 | 5 | $element->setRequired($isRequired); |
|
36 | 5 | $element->setOptions($options); |
|
37 | |||
38 | 5 | $this->addElement($element); |
|
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
39 | |||
40 | 5 | return $this; |
|
41 | } |
||
42 | |||
43 | /** |
||
44 | * @param $className |
||
45 | * @param $name |
||
46 | * @param bool $label |
||
47 | * @param bool $isRequired |
||
48 | * @param array $options |
||
49 | * @return $this |
||
50 | */ |
||
51 | public function addCustom($className, $name, $label = false, $isRequired = false, $options = []) |
||
52 | { |
||
53 | $label = ($label) ? $label : ucfirst($name); |
||
54 | $element = $this->getNewElementByClass($className); |
||
55 | $element->setName($name); |
||
56 | $element->setLabel($label); |
||
57 | $element->setRequired($isRequired); |
||
58 | $element->setOptions($options); |
||
59 | |||
60 | $this->addElement($element); |
||
61 | |||
62 | return $this; |
||
63 | } |
||
64 | |||
65 | /** |
||
66 | * @param string $type |
||
67 | * @return ElementAbstract |
||
68 | */ |
||
69 | 12 | public function getNewElement($type) |
|
70 | { |
||
71 | 12 | $className = $this->getElementClassName($type); |
|
72 | |||
73 | 12 | return $this->getNewElementByClass($className); |
|
74 | } |
||
75 | |||
76 | /** |
||
77 | * @param $type |
||
78 | * @return string |
||
79 | */ |
||
80 | 12 | public function getElementClassName($type) |
|
81 | { |
||
82 | 12 | return 'Nip_Form_Element_' . ucfirst($type); |
|
83 | } |
||
84 | |||
85 | /** |
||
86 | * @param $className |
||
87 | * @return ElementAbstract |
||
88 | */ |
||
89 | 12 | public function getNewElementByClass($className) |
|
90 | { |
||
91 | 12 | $element = new $className($this); |
|
92 | |||
93 | 12 | return $this->initNewElement($element); |
|
94 | } |
||
95 | |||
96 | /** |
||
97 | * @param ElementAbstract $element |
||
98 | * @return ElementAbstract |
||
99 | */ |
||
100 | 12 | public function initNewElement($element) |
|
101 | { |
||
102 | 12 | $element->setForm($this); |
|
103 | |||
104 | 12 | return $element; |
|
105 | } |
||
106 | } |
||
107 |