-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathMagiumRenderer.php
62 lines (55 loc) · 2.07 KB
/
MagiumRenderer.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
<?php
/**
* Created by PhpStorm.
* User: kschr
* Date: 3/20/2017
* Time: 4:54 PM
*/
namespace Magium\Configuration\View;
use Zend\Form\Element;
use Zend\Form\ElementInterface;
use Zend\Form\View\Helper\FormElement;
use Zend\Form\View\Helper\FormInput;
use Zend\Form\View\Helper\FormSelect;
use Zend\View\Helper\AbstractHelper;
use Zend\View\Renderer\RendererInterface;
class MagiumRenderer extends AbstractHelper
{
public function __invoke($section, $group, $id, $options)
{
$type = 'text';
if (isset($options['type'])) {
$type = $options['type'];
}
$value = $options['value'];
$viewClass = 'Zend\Form\View\Helper\Form' . ucfirst(strtolower($type));
$formClass = 'Zend\Form\Element\\' . ucfirst(strtolower($type));
$reflectionClass = new \ReflectionClass($viewClass);
if (!$reflectionClass->isSubclassOf(AbstractHelper::class)) {
throw new InvalidViewConfigurationException('Invalid setting input type');
}
$instance = $reflectionClass->newInstance();
$formInstance = new \ReflectionClass($formClass);
$this->configureFormView($instance);
$name = sprintf('%s_%s_%s', $section, $group, $id);
$formElement = $formInstance->newInstance($name, $options);
/* @var $formElement Element */
if ($formElement instanceof Element\Select) {
$formElement->setOptions(['options' => $options['source']]);
}
$formElement->setAttribute('onchange', 'magiumRegisterChange(event)');
$formElement->setAttribute('class', 'form-control');
$formElement->setAttribute('data-path', $options['path']);
$formElement->setValue($value);
$output = $instance->render($formElement);
return $output;
}
protected function configureFormView(AbstractHelper $instance)
{
$view = $this->getView();
if (!$view instanceof RendererInterface) {
throw new InvalidViewConfigurationException('View is not the correct instance type');
}
$instance->setView($view);
}
}