|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* The MIT License |
|
5
|
|
|
* |
|
6
|
|
|
* Copyright 2020 Enjoys. |
|
7
|
|
|
* |
|
8
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
|
9
|
|
|
* of this software and associated documentation files (the "Software"), to deal |
|
10
|
|
|
* in the Software without restriction, including without limitation the rights |
|
11
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|
12
|
|
|
* copies of the Software, and to permit persons to whom the Software is |
|
13
|
|
|
* furnished to do so, subject to the following conditions: |
|
14
|
|
|
* |
|
15
|
|
|
* The above copyright notice and this permission notice shall be included in |
|
16
|
|
|
* all copies or substantial portions of the Software. |
|
17
|
|
|
* |
|
18
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
19
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|
20
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|
21
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|
22
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|
23
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
|
24
|
|
|
* THE SOFTWARE. |
|
25
|
|
|
*/ |
|
26
|
|
|
|
|
27
|
|
|
declare(strict_types=1); |
|
28
|
|
|
|
|
29
|
|
|
namespace Enjoys\Forms\Renderer; |
|
30
|
|
|
|
|
31
|
|
|
use Enjoys\Forms\Form; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Description of BaseRenderer |
|
35
|
|
|
* |
|
36
|
|
|
* @author Enjoys |
|
37
|
|
|
*/ |
|
38
|
|
|
class BaseRenderer implements RendererInterface |
|
39
|
|
|
{ |
|
40
|
|
|
|
|
41
|
|
|
protected Form $form; |
|
42
|
|
|
protected array $elements = []; |
|
43
|
|
|
|
|
44
|
|
|
public function __construct(Form $form = null){ |
|
45
|
|
|
$this->form = $this->setForm($form ?? new Form()); |
|
46
|
|
|
} |
|
47
|
|
|
/** |
|
48
|
|
|
* |
|
49
|
|
|
* @param Form $form |
|
50
|
|
|
* @return Form |
|
51
|
|
|
*/ |
|
52
|
|
|
public function setForm(Form $form): Form |
|
53
|
|
|
{ |
|
54
|
|
|
$this->form = $form; |
|
55
|
|
|
$this->elements = $this->form->getElements(); |
|
56
|
|
|
return $this->form; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* |
|
61
|
|
|
* @return string |
|
62
|
|
|
*/ |
|
63
|
|
|
protected function hiddenRender(): string |
|
64
|
|
|
{ |
|
65
|
|
|
$html = ''; |
|
66
|
|
|
/** @var \Enjoys\Forms\Element $element */ |
|
67
|
|
|
foreach ($this->elements as $key => $element) { |
|
68
|
|
|
if ($element instanceof \Enjoys\Forms\Elements\Hidden) { |
|
69
|
|
|
unset($this->elements[$key]); |
|
70
|
|
|
$html .= $element->baseHtml(); |
|
71
|
|
|
} |
|
72
|
|
|
continue; |
|
73
|
|
|
} |
|
74
|
|
|
return $html; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* |
|
79
|
|
|
* @param array|null $elements |
|
80
|
|
|
* @psalm-param null|array{\Enjoys\Forms\Element} $elements |
|
81
|
|
|
* @return string |
|
82
|
|
|
*/ |
|
83
|
|
|
protected function elementsRender(?array $elements = null): string |
|
84
|
|
|
{ |
|
85
|
|
|
$elements ??= $this->elements; |
|
86
|
|
|
$html = ''; |
|
87
|
|
|
|
|
88
|
|
|
foreach ($elements as $key => $element) { |
|
89
|
|
|
unset($elements[$key]); |
|
90
|
|
|
|
|
91
|
|
|
if (!($element instanceof \Enjoys\Forms\Element)) { |
|
92
|
|
|
continue; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
$html .= $this->elementRender($element); |
|
96
|
|
|
} |
|
97
|
|
|
return $html; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* |
|
102
|
|
|
* @param \Enjoys\Forms\Element $element |
|
103
|
|
|
* @return string |
|
104
|
|
|
*/ |
|
105
|
|
|
public function elementRender(\Enjoys\Forms\Element $element): string |
|
106
|
|
|
{ |
|
107
|
|
|
$elementRender = new BaseElementRender($element); |
|
108
|
|
|
return $elementRender->render(); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* |
|
113
|
|
|
* @return string |
|
114
|
|
|
*/ |
|
115
|
|
|
public function render(): string |
|
116
|
|
|
{ |
|
117
|
|
|
return "<form{$this->form->getAttributesString()}>" |
|
118
|
|
|
. $this->hiddenRender() |
|
119
|
|
|
. $this->elementsRender($this->elements) |
|
120
|
|
|
. "</form>"; |
|
121
|
|
|
} |
|
122
|
|
|
} |
|
123
|
|
|
|