1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* User: delboy1978uk |
4
|
|
|
* Date: 07/12/2016 |
5
|
|
|
* Time: 01:54 |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace Del\Form\Renderer; |
9
|
|
|
|
10
|
|
|
use Del\Form\Collection\FieldCollection; |
11
|
|
|
use Del\Form\AbstractForm; |
12
|
|
|
use Del\Form\Field\FieldInterface; |
13
|
|
|
use Del\Form\FormInterface; |
14
|
|
|
use Del\Form\Renderer\Error\DefaultErrorRender; |
15
|
|
|
use Del\Form\Renderer\Error\ErrorRendererInterface; |
16
|
|
|
use DOMDocument; |
17
|
|
|
use DomElement; |
18
|
|
|
|
19
|
|
|
abstract class AbstractFormRenderer implements FormRendererInterface |
20
|
|
|
{ |
21
|
|
|
/** @var DOMDocument $dom */ |
22
|
|
|
protected $dom; |
23
|
|
|
|
24
|
|
|
/** @var DomElement $form */ |
25
|
|
|
protected $form; |
26
|
|
|
|
27
|
|
|
/** @var bool $displayErrors */ |
28
|
|
|
protected $displayErrors; |
29
|
|
|
|
30
|
|
|
/** @var ErrorRendererInterface $errorRenderer */ |
31
|
|
|
protected $errorRenderer; |
32
|
|
|
|
33
|
|
|
/** @var DomElement $label The label element*/ |
34
|
|
|
protected $label; |
35
|
|
|
|
36
|
|
|
/** @var DomElement $element the field element */ |
37
|
|
|
protected $element; |
38
|
|
|
|
39
|
|
|
/** @var DomElement $errors The error block html*/ |
40
|
|
|
protected $errors; |
41
|
|
|
|
42
|
|
|
/** @var DomElement $block The containing html block */ |
43
|
|
|
protected $block; |
44
|
|
|
|
45
|
|
|
/** @var FieldInterface $field The current field being processed */ |
46
|
|
|
protected $field; |
47
|
|
|
|
48
|
19 |
|
public function __construct($name) |
49
|
|
|
{ |
50
|
19 |
|
$this->dom = new DOMDocument(); |
51
|
19 |
|
$this->errorRenderer = new DefaultErrorRender($this->dom); |
52
|
19 |
|
$form = $this->dom->createElement('form'); |
53
|
19 |
|
$form->setAttribute('name', $name); |
54
|
19 |
|
$this->form = $form; |
55
|
19 |
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param FormInterface $form |
59
|
|
|
* @param bool $displayErrors |
60
|
|
|
* @return string |
61
|
|
|
*/ |
62
|
6 |
View Code Duplication |
public function render(FormInterface $form, $displayErrors = true) |
|
|
|
|
63
|
|
|
{ |
64
|
6 |
|
$this->displayErrors = $displayErrors; |
65
|
6 |
|
$this->setFormAttributes($form); |
66
|
|
|
|
67
|
6 |
|
$fields = $form->getFields(); |
68
|
6 |
|
$this->processFields($fields); |
69
|
|
|
|
70
|
6 |
|
$this->dom->appendChild($this->form); |
71
|
6 |
|
return $this->dom->saveHTML(); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @param FormInterface $form |
76
|
|
|
*/ |
77
|
6 |
View Code Duplication |
private function setFormAttributes(FormInterface $form) |
|
|
|
|
78
|
|
|
{ |
79
|
6 |
|
$attributes = $form->getAttributes(); |
80
|
6 |
|
foreach ($attributes as $key => $value) { |
81
|
6 |
|
$this->form->setAttribute($key, $value); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
// set Id as name or method as post if not set |
85
|
6 |
|
$method = $this->getMethod($form); |
86
|
6 |
|
$id = $this->getId($form); |
87
|
|
|
|
88
|
6 |
|
$this->form->setAttribute('id', $id); |
89
|
6 |
|
$this->form->setAttribute('method', $method); |
90
|
6 |
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @param FormInterface $form |
94
|
|
|
* @return string |
95
|
|
|
*/ |
96
|
6 |
|
private function getMethod(FormInterface $form) |
97
|
|
|
{ |
98
|
6 |
|
return $form->getMethod() ?: AbstractForm::METHOD_POST; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @param FormInterface $form |
103
|
|
|
* @return string |
104
|
|
|
*/ |
105
|
6 |
|
private function getId(FormInterface $form) |
106
|
|
|
{ |
107
|
6 |
|
return $form->getId() ?: $this->form->getAttribute('name'); |
108
|
|
|
} |
109
|
|
|
|
110
|
6 |
|
private function processFields(FieldCollection $fields) |
111
|
|
|
{ |
112
|
6 |
|
$fields->rewind(); |
113
|
6 |
|
while ($fields->valid()) { |
114
|
5 |
|
$this->block = $this->dom->createElement('div'); |
115
|
5 |
|
$this->field = $fields->current(); |
116
|
5 |
|
$this->label = $this->renderFieldLabel(); |
117
|
5 |
|
$this->element = $this->field->getRenderer()->render($this->dom, $this->field); |
118
|
5 |
|
$this->errors = $this->field->isValid() ? null : $this->renderError(); |
|
|
|
|
119
|
5 |
|
$this->block = $this->renderFieldBlock(); |
120
|
5 |
|
$this->form->appendChild($this->block); |
121
|
5 |
|
$fields->next(); |
122
|
|
|
} |
123
|
6 |
|
$fields->rewind(); |
124
|
6 |
|
} |
125
|
|
|
|
126
|
|
|
|
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @return DOMElement|null |
130
|
|
|
*/ |
131
|
3 |
|
public function renderError() |
132
|
|
|
{ |
133
|
3 |
|
$errorBlock = null; |
134
|
3 |
|
if (!$this->field->isValid() && $this->displayErrors === true) { |
135
|
2 |
|
$this->block->setAttribute('class', 'has-error '); |
136
|
2 |
|
$errorBlock = $this->errorRenderer->render($this->field); |
137
|
|
|
} |
138
|
3 |
|
return $errorBlock; |
139
|
|
|
} |
140
|
|
|
} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.