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\View\Helper\FormElement; |
13
|
|
|
use Zend\Form\View\Helper\FormInput; |
14
|
|
|
use Zend\View\Helper\AbstractHelper; |
15
|
|
|
|
16
|
|
|
class MagiumRenderer extends AbstractHelper |
17
|
|
|
{ |
18
|
|
|
function __invoke($section, $group, $id, $options) |
|
|
|
|
19
|
|
|
{ |
20
|
|
|
$type = 'text'; |
21
|
|
|
$options = ['value' => $options['value']]; |
22
|
|
|
if (isset($options['type'])) { |
23
|
|
|
$type = $options['type']; |
24
|
|
|
} |
25
|
|
|
$viewClass = 'Zend\Form\View\Helper\Form' . ucfirst(strtolower($type)); |
26
|
|
|
$formClass = 'Zend\Form\Element\\' . ucfirst(strtolower($type)); |
27
|
|
|
$reflectionClass = new \ReflectionClass($viewClass); |
28
|
|
|
if (!$reflectionClass->isSubclassOf(AbstractHelper::class)) { |
29
|
|
|
throw new InvalidViewConfigurationException('Invalid setting input type'); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
$instance = $reflectionClass->newInstance(); |
33
|
|
|
if ($instance instanceof FormInput) { |
34
|
|
|
$instance->setView($this->getView()); |
|
|
|
|
35
|
|
|
$formInstance = new \ReflectionClass($formClass); |
36
|
|
|
$name = sprintf('%s_%s_%s', $section, $group, $id); |
37
|
|
|
$formElement = $formInstance->newInstance($name, $options); |
38
|
|
|
/* @var $formElement Element */ |
39
|
|
|
$formElement->setValue($options['value']); |
40
|
|
|
$formElement->setAttribute('id', $id); |
41
|
|
|
$output = $instance->render($formElement); |
42
|
|
|
return $output; |
43
|
|
|
} |
44
|
|
|
throw new InvalidViewConfigurationException('Invalid form input type'); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
|
48
|
|
|
} |
49
|
|
|
|
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.