1 | <?php |
||
15 | abstract class AbstractForm implements FormInterface |
||
16 | { |
||
17 | const ENC_TYPE_MULTIPART_FORM_DATA = 'multipart/form-data'; |
||
18 | const METHOD_POST = 'post'; |
||
19 | const METHOD_GET = 'get'; |
||
20 | |||
21 | /** @var FieldCollection $fieldCollection */ |
||
22 | private $fieldCollection; |
||
23 | |||
24 | /** @var DOMDocument $dom */ |
||
25 | private $dom; |
||
26 | |||
27 | /** @var DomElement $form */ |
||
28 | private $form; |
||
29 | |||
30 | 10 | public function __construct($name) |
|
39 | |||
40 | abstract public function init(); |
||
41 | |||
42 | /** |
||
43 | * @return bool |
||
44 | */ |
||
45 | public function isValid() |
||
49 | |||
50 | /** |
||
51 | * @return array |
||
52 | */ |
||
53 | 1 | public function getValues() |
|
54 | { |
||
55 | 1 | $values = []; |
|
56 | /** @var FieldInterface $field */ |
||
57 | 1 | foreach ($this->fieldCollection as $field) { |
|
58 | 1 | $values[$field->getName()] = $field->getValue(); |
|
59 | 1 | } |
|
60 | 1 | return $values; |
|
61 | } |
||
62 | |||
63 | /** |
||
64 | * @param array $data |
||
65 | * @return $this |
||
66 | */ |
||
67 | public function populate(array $data) |
||
68 | { |
||
69 | /** @var FieldInterface $field */ |
||
70 | foreach ($this->fieldCollection as $field) { |
||
71 | $name = $field->getName(); |
||
72 | if (isset($data[$name])) { |
||
73 | $field->setValue($data[$name]); |
||
74 | } |
||
75 | } |
||
76 | return $this; |
||
77 | } |
||
78 | |||
79 | /** |
||
80 | * @param string $name |
||
81 | * @return FieldInterface|null |
||
82 | */ |
||
83 | 1 | public function getField($name) |
|
84 | { |
||
85 | 1 | return $this->fieldCollection->findByName($name); |
|
86 | } |
||
87 | |||
88 | /** |
||
89 | * @return FieldCollection |
||
90 | */ |
||
91 | 2 | public function getFields() |
|
92 | { |
||
93 | 2 | return $this->fieldCollection; |
|
94 | } |
||
95 | |||
96 | /** |
||
97 | * @param FieldInterface $field |
||
98 | * @return $this |
||
99 | */ |
||
100 | 3 | public function addField(FieldInterface $field) |
|
105 | |||
106 | /** |
||
107 | * @return string |
||
108 | */ |
||
109 | 2 | public function render() |
|
110 | { |
||
111 | 2 | $method = $this->form->getAttribute('method') ?: self::METHOD_POST; |
|
112 | 2 | $id = $this->form->getAttribute('id') ?: $this->form->getAttribute('name'); |
|
113 | 2 | $action = $this->form->getAttribute('action') ?: $this->form->getAttribute('action'); |
|
114 | |||
115 | 2 | $this->form->setAttribute('id', $id); |
|
116 | 2 | $this->form->setAttribute('method', $method); |
|
117 | 2 | $this->form->setAttribute('action', $action); |
|
118 | |||
119 | 2 | $this->fieldCollection->rewind(); |
|
120 | 2 | while ($this->fieldCollection->valid()) { |
|
121 | /** @var FieldInterface $current */ |
||
122 | 1 | $current = $this->fieldCollection->current(); |
|
123 | 1 | $child = $this->dom->createElement($current->getTag()); |
|
124 | |||
125 | 1 | $child->setAttribute('type', $current->getTagType()); |
|
126 | 1 | $child->setAttribute('name', $current->getName()); |
|
127 | 1 | $child->setAttribute('id', $current->getId()); |
|
128 | 1 | $child->setAttribute('value', $current->getValue()); |
|
129 | 1 | $child->setAttribute('class', $current->getClass()); |
|
130 | |||
131 | 1 | $this->form->appendChild($child); |
|
132 | 1 | $this->fieldCollection->next(); |
|
133 | 1 | } |
|
134 | 2 | $this->fieldCollection->rewind(); |
|
135 | |||
136 | 2 | $this->dom->appendChild($this->form); |
|
137 | |||
138 | 2 | return $this->dom->saveHTML(); |
|
139 | } |
||
140 | |||
141 | /** |
||
142 | * @param $url |
||
143 | * @return $this |
||
144 | */ |
||
145 | 2 | public function setAction($url) |
|
150 | |||
151 | /** |
||
152 | * @return string |
||
153 | */ |
||
154 | 2 | public function getAction() |
|
158 | |||
159 | /** |
||
160 | * @return string |
||
161 | */ |
||
162 | 2 | public function getId() |
|
166 | |||
167 | /** |
||
168 | * @param string $id |
||
169 | * @return $this |
||
170 | */ |
||
171 | 2 | public function setId($id) |
|
176 | |||
177 | /** |
||
178 | * @param $encType |
||
179 | * @return $this |
||
180 | */ |
||
181 | 2 | public function setEncType($encType) |
|
186 | |||
187 | /** |
||
188 | * @return string |
||
189 | */ |
||
190 | 1 | public function getEncType() |
|
194 | |||
195 | /** |
||
196 | * @param string $method |
||
197 | * @return FormInterface |
||
198 | */ |
||
199 | 2 | public function setMethod($method) |
|
204 | |||
205 | /** |
||
206 | * @return string |
||
207 | */ |
||
208 | 1 | public function getMethod() |
|
212 | |||
213 | /** |
||
214 | * @param $class |
||
215 | * @return FormInterface |
||
216 | */ |
||
217 | 2 | public function setClass($class) |
|
222 | |||
223 | /** |
||
224 | * @return string |
||
225 | */ |
||
226 | 2 | public function getClass() |
|
230 | } |
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..