|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* YAWIK |
|
4
|
|
|
* |
|
5
|
|
|
* @filesource |
|
6
|
|
|
* @copyright (c) 2013 - 2016 Cross Solution (http://cross-solution.de) |
|
7
|
|
|
* @license MIT |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
/** Core forms view helpers */ |
|
11
|
|
|
namespace Core\Form\View\Helper; |
|
12
|
|
|
|
|
13
|
|
|
use Core\Form\ViewPartialProviderInterface; |
|
14
|
|
|
use Core\Form\ExplicitParameterProviderInterface; |
|
15
|
|
|
use Core\Form\Element\ViewHelperProviderInterface; |
|
16
|
|
|
use Core\Form\Container; |
|
17
|
|
|
use Zend\Form\View\Helper\AbstractHelper; |
|
18
|
|
|
use Core\Form\SummaryForm; |
|
|
|
|
|
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Helper for rendering form containers |
|
22
|
|
|
* |
|
23
|
|
|
* @author Mathias Gelhausen <[email protected]> |
|
24
|
|
|
*/ |
|
25
|
|
|
class FormContainer extends AbstractHelper |
|
26
|
|
|
{ |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Invoke as function. |
|
30
|
|
|
* |
|
31
|
|
|
* Proxies to {@link render()} or returns self. |
|
32
|
|
|
* |
|
33
|
|
|
* @param null|Container $container |
|
34
|
|
|
* @param string $layout |
|
35
|
|
|
* @param array $parameter |
|
36
|
|
|
* @return FormContainer|string |
|
37
|
|
|
*/ |
|
38
|
|
View Code Duplication |
public function __invoke(Container $container = null, $layout = Form::LAYOUT_HORIZONTAL, $parameter = array()) |
|
|
|
|
|
|
39
|
|
|
{ |
|
40
|
|
|
if (!$container) { |
|
41
|
|
|
return $this; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
return $this->render($container, $layout, $parameter); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Renders the forms of a container. |
|
49
|
|
|
* |
|
50
|
|
|
* @param Container $container |
|
51
|
|
|
* @param string $layout |
|
52
|
|
|
* @param array $parameter |
|
53
|
|
|
* @return string |
|
54
|
|
|
*/ |
|
55
|
|
|
public function render(Container $container, $layout = Form::LAYOUT_HORIZONTAL, $parameter = array()) |
|
56
|
|
|
{ |
|
57
|
|
|
|
|
58
|
|
|
$content = ''; |
|
59
|
|
|
|
|
60
|
|
|
$content .= $container->renderPre($this->getView()); |
|
|
|
|
|
|
61
|
|
|
|
|
62
|
|
|
if ($container instanceof ViewPartialProviderInterface) { |
|
63
|
|
|
return $this->getView()->partial($container->getViewPartial(), array('element' => $container)); |
|
|
|
|
|
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
foreach ($container as $element) { |
|
67
|
|
|
$content .= $this->renderElement($element, $layout, $parameter); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
$content .= $container->renderPost($this->getView()); |
|
|
|
|
|
|
71
|
|
|
|
|
72
|
|
|
return $content; |
|
73
|
|
|
|
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
public function renderElement($element, $layout, $parameter) |
|
77
|
|
|
{ |
|
78
|
|
|
$parameterPartial = $parameter; |
|
79
|
|
|
$content = ''; |
|
80
|
|
|
if ($element instanceof ExplicitParameterProviderInterface) { |
|
81
|
|
|
$parameterPartial = array_merge($element->getParams(), $parameterPartial); |
|
82
|
|
|
} |
|
83
|
|
|
if ($element instanceof ViewPartialProviderInterface) { |
|
84
|
|
|
$parameterPartial = array_merge(array('element' => $element, 'layout' => $layout), $parameterPartial); |
|
85
|
|
|
$content .= $this->getView()->partial( |
|
|
|
|
|
|
86
|
|
|
$element->getViewPartial(), |
|
87
|
|
|
$parameterPartial |
|
88
|
|
|
); |
|
89
|
|
|
|
|
90
|
|
|
} elseif ($element instanceof ViewHelperProviderInterface) { |
|
91
|
|
|
$helper = $element->getViewHelper(); |
|
92
|
|
|
if (is_string($helper)) { |
|
93
|
|
|
$helper = $this->getView()->plugin($helper); |
|
|
|
|
|
|
94
|
|
|
} |
|
95
|
|
|
$content .= $helper($element); |
|
96
|
|
|
} elseif ($element instanceof SummaryForm) { |
|
97
|
|
|
$content .= $this->getView()->summaryForm($element); |
|
|
|
|
|
|
98
|
|
|
} elseif ($element instanceof Container) { |
|
99
|
|
|
$content.= $this->render($element, $layout, $parameter); |
|
100
|
|
|
} else { |
|
101
|
|
|
$content.= $this->getView()->form($element, $layout, $parameter); |
|
|
|
|
|
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
return $content; |
|
105
|
|
|
} |
|
106
|
|
|
} |
|
107
|
|
|
|
Let’s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let’s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: