1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created by PhpStorm. |
4
|
|
|
* User: kschr |
5
|
|
|
* Date: 3/20/2017 |
6
|
|
|
* Time: 4:54 PM |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Magium\Configuration\View; |
10
|
|
|
|
11
|
|
|
use Zend\Form\Element; |
12
|
|
|
use Zend\Form\ElementInterface; |
13
|
|
|
use Zend\Form\View\Helper\FormElement; |
14
|
|
|
use Zend\Form\View\Helper\FormInput; |
15
|
|
|
use Zend\Form\View\Helper\FormSelect; |
16
|
|
|
use Zend\View\Helper\AbstractHelper; |
17
|
|
|
|
18
|
|
|
class MagiumRenderer extends AbstractHelper |
19
|
|
|
{ |
20
|
1 |
|
function __invoke($section, $group, $id, $options) |
|
|
|
|
21
|
|
|
{ |
22
|
1 |
|
$type = 'text'; |
23
|
1 |
|
if (isset($options['type'])) { |
24
|
1 |
|
$type = $options['type']; |
25
|
|
|
} |
26
|
1 |
|
$value = $options['value']; |
27
|
|
|
|
28
|
1 |
|
$viewClass = 'Zend\Form\View\Helper\Form' . ucfirst(strtolower($type)); |
29
|
1 |
|
$formClass = 'Zend\Form\Element\\' . ucfirst(strtolower($type)); |
30
|
1 |
|
$reflectionClass = new \ReflectionClass($viewClass); |
31
|
1 |
|
if (!$reflectionClass->isSubclassOf(AbstractHelper::class)) { |
32
|
|
|
throw new InvalidViewConfigurationException('Invalid setting input type'); |
33
|
|
|
} |
34
|
|
|
|
35
|
1 |
|
$instance = $reflectionClass->newInstance(); |
36
|
1 |
|
$formInstance = new \ReflectionClass($formClass); |
37
|
|
|
|
38
|
1 |
|
$instance->setView($this->getView()); |
39
|
1 |
|
$name = sprintf('%s_%s_%s', $section, $group, $id); |
40
|
1 |
|
$formElement = $formInstance->newInstance($name, $options); |
41
|
|
|
/* @var $formElement Element */ |
42
|
1 |
|
if ($formElement instanceof Element\Select) { |
43
|
|
|
$formElement->setOptions(['options' => $options['source']]); |
44
|
|
|
} |
45
|
1 |
|
$formElement->setAttribute('onchange', 'magiumRegisterChange(event)'); |
46
|
1 |
|
$formElement->setAttribute('class', 'form-control'); |
47
|
1 |
|
$formElement->setAttribute('data-path', $options['path']); |
48
|
1 |
|
$formElement->setValue($value); |
49
|
1 |
|
$output = $instance->render($formElement); |
50
|
1 |
|
return $output; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
|
54
|
|
|
} |
55
|
|
|
|
Adding explicit visibility (
private
,protected
, orpublic
) is generally recommend to communicate to other developers how, and from where this method is intended to be used.