1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* User: delboy1978uk |
4
|
|
|
* Date: 29/11/2016 |
5
|
|
|
* Time: 19:44 |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace Del\Form\Renderer; |
9
|
|
|
|
10
|
|
|
use Del\Form\Collection\FieldCollection; |
11
|
|
|
use Del\Form\AbstractForm; |
12
|
|
|
use Del\Form\FormInterface; |
13
|
|
|
use DOMDocument; |
14
|
|
|
|
15
|
|
|
class FormRenderer |
16
|
|
|
{ |
17
|
|
|
/** @var DOMDocument $dom */ |
18
|
|
|
private $dom; |
19
|
|
|
|
20
|
|
|
/** @var \DomElement $form */ |
21
|
|
|
private $form; |
22
|
|
|
|
23
|
|
|
/** @var bool $displayErrors */ |
24
|
|
|
private $displayErrors; |
25
|
|
|
|
26
|
19 |
|
public function __construct($name) |
27
|
|
|
{ |
28
|
19 |
|
$this->dom = new DOMDocument(); |
29
|
19 |
|
$form = $this->dom->createElement('form'); |
30
|
19 |
|
$form->setAttribute('name', $name); |
31
|
19 |
|
$this->form = $form; |
32
|
19 |
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @param FormInterface $form |
36
|
|
|
* @param bool $displayErrors |
37
|
|
|
* @return string |
38
|
|
|
*/ |
39
|
6 |
|
public function render(FormInterface $form, $displayErrors = true) |
40
|
|
|
{ |
41
|
6 |
|
$this->displayErrors = $displayErrors; |
42
|
6 |
|
$this->setFormAttributes($form); |
43
|
|
|
|
44
|
6 |
|
$fields = $form->getFields(); |
45
|
6 |
|
$this->processFields($fields); |
46
|
|
|
|
47
|
6 |
|
$this->dom->appendChild($this->form); |
48
|
6 |
|
return $this->dom->saveHTML(); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @param FormInterface $form |
53
|
|
|
*/ |
54
|
6 |
|
private function setFormAttributes(FormInterface $form) |
55
|
|
|
{ |
56
|
6 |
|
$method = $this->getMethod($form); |
57
|
6 |
|
$id = $this->getId($form); |
58
|
6 |
|
$action = $this->getAction($form); |
59
|
6 |
|
$encType = $this->getEncType($form); |
60
|
6 |
|
$class = $form->getClass(); |
61
|
|
|
|
62
|
6 |
|
$this->form->setAttribute('id', $id); |
63
|
6 |
|
$this->form->setAttribute('method', $method); |
64
|
6 |
|
$this->form->setAttribute('class', $class); |
65
|
6 |
|
$this->form->setAttribute('action', $action); |
66
|
6 |
|
$this->form->setAttribute('enctype', $encType); |
67
|
6 |
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @param FormInterface $form |
71
|
|
|
* @return string |
72
|
|
|
*/ |
73
|
6 |
|
private function getMethod(FormInterface $form) |
74
|
|
|
{ |
75
|
6 |
|
return $form->getMethod() ?: AbstractForm::METHOD_POST; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @param FormInterface $form |
80
|
|
|
* @return string |
81
|
|
|
*/ |
82
|
6 |
|
private function getId(FormInterface $form) |
83
|
|
|
{ |
84
|
6 |
|
return $form->getId() ?: $this->form->getAttribute('name'); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @param FormInterface $form |
89
|
|
|
* @return string |
90
|
|
|
*/ |
91
|
6 |
|
private function getAction(FormInterface $form) |
92
|
|
|
{ |
93
|
6 |
|
return $form->getAction() ?: $this->form->getAttribute('action'); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @param FormInterface $form |
98
|
|
|
* @return string |
99
|
|
|
*/ |
100
|
6 |
|
private function getEncType(FormInterface $form) |
101
|
|
|
{ |
102
|
6 |
|
return $form->getEncType() ?: $this->form->getAttribute('enc-type'); |
103
|
|
|
} |
104
|
|
|
|
105
|
6 |
|
private function processFields(FieldCollection $fields) |
106
|
|
|
{ |
107
|
6 |
|
$fields->rewind(); |
108
|
6 |
|
while ($fields->valid()) { |
109
|
5 |
|
$current = $fields->current(); |
110
|
5 |
|
$child = $current->getRenderer()->render($this->dom, $current, $this->displayErrors); |
|
|
|
|
111
|
5 |
|
$this->form->appendChild($child); |
112
|
5 |
|
$fields->next(); |
113
|
|
|
} |
114
|
6 |
|
$fields->rewind(); |
115
|
6 |
|
} |
116
|
|
|
|
117
|
|
|
} |
This check marks calls to methods that do not seem to exist on an object.
This is most likely the result of a method being renamed without all references to it being renamed likewise.