1 | <?php |
||
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 | 16 | public function __construct($name) |
|
33 | |||
34 | /** |
||
35 | * @param FormInterface $form |
||
36 | * @param bool $displayErrors |
||
37 | * @return string |
||
38 | */ |
||
39 | 4 | public function render(FormInterface $form, $displayErrors = true) |
|
50 | |||
51 | /** |
||
52 | * @param FormInterface $form |
||
53 | */ |
||
54 | 4 | private function setFormAttributes(FormInterface $form) |
|
68 | |||
69 | /** |
||
70 | * @param FormInterface $form |
||
71 | * @return string |
||
72 | */ |
||
73 | 4 | private function getMethod(FormInterface $form) |
|
77 | |||
78 | /** |
||
79 | * @param FormInterface $form |
||
80 | * @return string |
||
81 | */ |
||
82 | 4 | private function getId(FormInterface $form) |
|
86 | |||
87 | /** |
||
88 | * @param FormInterface $form |
||
89 | * @return string |
||
90 | */ |
||
91 | 4 | private function getAction(FormInterface $form) |
|
95 | |||
96 | /** |
||
97 | * @param FormInterface $form |
||
98 | * @return string |
||
99 | */ |
||
100 | 4 | private function getEncType(FormInterface $form) |
|
104 | |||
105 | 4 | private function processFields(FieldCollection $fields) |
|
106 | { |
||
107 | 4 | $fields->rewind(); |
|
108 | 4 | while ($fields->valid()) { |
|
109 | 3 | $current = $fields->current(); |
|
110 | 3 | $child = $this->createFieldDOM($current); |
|
111 | 3 | $this->form->appendChild($child); |
|
112 | 3 | $fields->next(); |
|
113 | 3 | } |
|
114 | 4 | $fields->rewind(); |
|
115 | 4 | } |
|
116 | |||
117 | /** |
||
118 | * @param FieldInterface $field |
||
119 | * @return DOMElement |
||
120 | */ |
||
121 | 3 | private function createFieldDOM(FieldInterface $field) |
|
122 | { |
||
123 | 3 | $formGroup = $this->dom->createElement('div'); |
|
124 | 3 | $formGroup->setAttribute('class', 'form-group'); |
|
125 | |||
126 | 3 | $label = $this->dom->createElement('label'); |
|
127 | 3 | $label->setAttribute('for', $field->getId()); |
|
128 | 3 | $label->textContent = $field->getLabel(); |
|
129 | |||
130 | 3 | $formField = $this->createChildElement($field); |
|
131 | |||
132 | 3 | $formGroup->appendChild($label); |
|
133 | 3 | $formGroup->appendChild($formField); |
|
134 | |||
135 | 3 | if (!$field->isValid() && $this->displayErrors === true) { |
|
136 | 1 | $formGroup = $this->createHelpBlock($formGroup, $field->getMessages()); |
|
137 | 1 | } |
|
138 | |||
139 | 3 | return $formGroup; |
|
140 | } |
||
141 | |||
142 | 1 | private function createHelpBlock(DOMElement $formGroup, array $messages) |
|
143 | { |
||
144 | 1 | $formGroup->setAttribute('class', 'form-group has-error'); |
|
145 | 1 | $helpBlock = $this->dom->createElement('span'); |
|
146 | 1 | $helpBlock->setAttribute('class', 'help-block'); |
|
147 | 1 | $errorMessages = ''; |
|
148 | 1 | foreach ($messages as $message) { |
|
149 | 1 | if(is_array($message)) { |
|
150 | 1 | foreach ($message as $m) { |
|
151 | 1 | $errorMessages .= $m."\n"; |
|
152 | 1 | } |
|
153 | 1 | } else { |
|
154 | $errorMessages .= $message."\n"; |
||
155 | } |
||
156 | 1 | } |
|
157 | 1 | $helpBlock->textContent = $errorMessages; |
|
158 | 1 | $formGroup->appendChild($helpBlock); |
|
159 | 1 | return $formGroup; |
|
160 | } |
||
161 | |||
162 | |||
163 | |||
164 | /** |
||
165 | * @param FieldInterface $field |
||
166 | * @return DOMElement |
||
167 | */ |
||
168 | 3 | private function createChildElement(FieldInterface $field) |
|
180 | } |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..