1 | <?php |
||
27 | class Form extends FieldSet implements FormInterface |
||
28 | { |
||
29 | |||
30 | /** |
||
31 | * @var string Form id |
||
32 | */ |
||
33 | protected $id; |
||
34 | |||
35 | /** |
||
36 | * Add render capabilities to element |
||
37 | */ |
||
38 | use RenderAwareMethods; |
||
39 | |||
40 | /** |
||
41 | * Creates a form with basic attributes |
||
42 | */ |
||
43 | 54 | public function __construct() |
|
49 | |||
50 | /** |
||
51 | * @var ServerRequestInterface |
||
52 | */ |
||
53 | protected $request; |
||
54 | |||
55 | /** |
||
56 | * Set data to validate and/or populate elements |
||
57 | * |
||
58 | * @param array $data |
||
59 | * |
||
60 | * @return self|$this|FormInterface |
||
61 | */ |
||
62 | 2 | public function setData($data) |
|
67 | |||
68 | /** |
||
69 | * Returns submitted data or current data |
||
70 | * |
||
71 | * @return array |
||
72 | */ |
||
73 | 4 | public function getData() |
|
79 | |||
80 | /** |
||
81 | * Sets HTTP server request |
||
82 | * |
||
83 | * The form data should be populated from this requests. |
||
84 | * |
||
85 | * @param ServerRequestInterface $request |
||
86 | * @return self|$this|FormInterface |
||
87 | */ |
||
88 | 6 | public function setRequest(ServerRequestInterface $request) |
|
93 | |||
94 | /** |
||
95 | * Recursively collects all posted data |
||
96 | * |
||
97 | * @param ContainerInterface $container |
||
98 | * @param array $data |
||
99 | */ |
||
100 | 4 | protected function getElementData( |
|
115 | |||
116 | /** |
||
117 | * Gets the HTML renderer for this element |
||
118 | * |
||
119 | * @return RendererInterface |
||
120 | */ |
||
121 | 2 | protected function getRenderer() |
|
125 | |||
126 | /** |
||
127 | * Gets the form id |
||
128 | * |
||
129 | * @return string |
||
130 | */ |
||
131 | 6 | public function getId() |
|
135 | |||
136 | /** |
||
137 | * Sets internal form ID |
||
138 | * |
||
139 | * @param string $formId |
||
140 | * |
||
141 | * @return self|$this|FormInterface |
||
142 | */ |
||
143 | 16 | public function setId($formId) |
|
148 | |||
149 | /** |
||
150 | * Adds an element to the container |
||
151 | * |
||
152 | * @param ElementInterface $element |
||
153 | * |
||
154 | * @return self|$this|ContainerInterface |
||
155 | */ |
||
156 | 42 | public function add(ElementInterface $element) |
|
163 | |||
164 | /** |
||
165 | * Check if for was submitted or not |
||
166 | * |
||
167 | * @return boolean |
||
168 | */ |
||
169 | 4 | public function wasSubmitted() |
|
170 | { |
||
171 | 4 | $formMethod = strtoupper($this->getAttribute('method', 'post')); |
|
172 | 4 | $formId = $this->getRequest()->getPost('form-id', false); |
|
|
|||
173 | $formId = $formId |
||
174 | 4 | ? $formId |
|
175 | 4 | : $this->getRequest()->getQuery('form-id', null); |
|
176 | |||
177 | 4 | $submitted = $this->request->getMethod() == $formMethod && |
|
178 | 4 | $formId == $this->getId(); |
|
179 | 4 | if ($submitted) { |
|
180 | 2 | $this->updateValuesFromRequest(); |
|
181 | 2 | } |
|
182 | 4 | return $submitted; |
|
183 | } |
||
184 | |||
185 | /** |
||
186 | * Gets the HTTP request |
||
187 | * |
||
188 | * @return ServerRequestInterface|Request |
||
189 | */ |
||
190 | 6 | public function getRequest() |
|
197 | |||
198 | /** |
||
199 | * Assign form values from request |
||
200 | */ |
||
201 | 2 | protected function updateValuesFromRequest() |
|
212 | } |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: