1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Chinstrap\Core\Views\Functions; |
6
|
|
|
|
7
|
|
|
use Chinstrap\Core\Contracts\Factories\FormFactory; |
8
|
|
|
use Chinstrap\Core\Exceptions\Forms\FormNotFound; |
9
|
|
|
use Laminas\Form\View\Helper\Form as FormHelper; |
10
|
|
|
use Laminas\View\Renderer\PhpRenderer; |
11
|
|
|
use PublishingKit\Config\Config; |
12
|
|
|
use Twig\Markup; |
13
|
|
|
|
14
|
|
|
final class Form |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var Config |
18
|
|
|
*/ |
19
|
|
|
private $config; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var FormFactory |
23
|
|
|
*/ |
24
|
|
|
private $factory; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var FormHelper |
28
|
|
|
*/ |
29
|
|
|
private $helper; |
30
|
|
|
|
31
|
|
|
public function __construct(Config $config, FormFactory $factory, FormHelper $helper, PhpRenderer $renderer) |
32
|
|
|
{ |
33
|
|
|
$this->config = $config->get('forms'); |
34
|
|
|
$this->factory = $factory; |
35
|
|
|
$plugin = $renderer->getHelperPluginManager(); |
36
|
|
|
foreach ($this->getInvokables() as $pluginName => $pluginClass) { |
37
|
|
|
$plugin->setInvokableClass($pluginName, $pluginClass); |
38
|
|
|
} |
39
|
|
|
$helper->setView($renderer); |
40
|
|
|
$this->helper = $helper; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
private function getInvokables(): array |
44
|
|
|
{ |
45
|
|
|
return [ |
46
|
|
|
'formRow' => 'Laminas\Form\View\Helper\FormRow', |
47
|
|
|
'form_label' => 'Laminas\Form\View\Helper\FormLabel', |
48
|
|
|
'form_element' => 'Laminas\Form\View\Helper\FormElement', |
49
|
|
|
'form_element_errors' => 'Laminas\Form\View\Helper\FormElementErrors', |
50
|
|
|
'forminput' => 'Laminas\Form\View\Helper\FormInput', |
51
|
|
|
'formtext' => 'Laminas\Form\View\Helper\FormText', |
52
|
|
|
'formtextarea' => 'Laminas\Form\View\Helper\FormTextarea', |
53
|
|
|
'formemail' => 'Laminas\Form\View\Helper\FormEmail', |
54
|
|
|
'formsubmit' => 'Laminas\Form\View\Helper\FormSubmit', |
55
|
|
|
]; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function __invoke(string $name): Markup |
59
|
|
|
{ |
60
|
|
|
if (!isset($this->config[$name])) { |
61
|
|
|
throw new FormNotFound('The specified form is not registered'); |
62
|
|
|
} |
63
|
|
|
$form = $this->factory->make($this->config[$name]); |
64
|
|
|
return new Markup($this->helper->__invoke($form), 'UTF-8'); |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|