|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of slick/form package |
|
5
|
|
|
* |
|
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
7
|
|
|
* file that was distributed with this source code. |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace Slick\Form; |
|
11
|
|
|
|
|
12
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
|
13
|
|
|
use Slick\Form\Element\ContainerInterface; |
|
14
|
|
|
use Slick\Form\Element\FieldSet; |
|
15
|
|
|
use Slick\Form\Element\RenderAwareMethods; |
|
16
|
|
|
use Slick\Form\Input\File; |
|
17
|
|
|
use Slick\Form\Renderer\Form as FormRenderer; |
|
18
|
|
|
use Slick\Form\Renderer\RendererInterface; |
|
19
|
|
|
use Slick\Http\PhpEnvironment\Request; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* HTTP Form |
|
23
|
|
|
* |
|
24
|
|
|
* @package Slick\Form |
|
25
|
|
|
* @author Filipe Silva <[email protected]> |
|
26
|
|
|
*/ |
|
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() |
|
44
|
|
|
{ |
|
45
|
54 |
|
parent::__construct(); |
|
46
|
54 |
|
$this->setAttribute('action', '') |
|
47
|
54 |
|
->setAttribute('method', 'post'); |
|
48
|
54 |
|
} |
|
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) |
|
63
|
|
|
{ |
|
64
|
2 |
|
$this->setValues($data); |
|
65
|
2 |
|
return $this; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Returns submitted data or current data |
|
70
|
|
|
* |
|
71
|
|
|
* @return array |
|
72
|
|
|
*/ |
|
73
|
4 |
|
public function getData() |
|
74
|
|
|
{ |
|
75
|
4 |
|
$data = []; |
|
76
|
4 |
|
$this->getElementData($this, $data); |
|
77
|
4 |
|
return $data; |
|
78
|
|
|
} |
|
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) |
|
89
|
|
|
{ |
|
90
|
6 |
|
$this->request = $request; |
|
91
|
6 |
|
return $this; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* Recursively collects all posted data |
|
96
|
|
|
* |
|
97
|
|
|
* @param ContainerInterface $container |
|
98
|
|
|
* @param array $data |
|
99
|
|
|
*/ |
|
100
|
4 |
|
protected function getElementData( |
|
101
|
2 |
|
ContainerInterface $container, &$data = [] |
|
102
|
|
|
) { |
|
103
|
4 |
|
foreach ($container as $element) { |
|
104
|
2 |
|
if ($element instanceof InputInterface) { |
|
105
|
2 |
|
$data[$element->getName()] = $element->getValue(); |
|
106
|
2 |
|
continue; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
4 |
|
if ($element instanceof ContainerInterface) { |
|
110
|
2 |
|
$this->getElementData($element, $data); |
|
111
|
2 |
|
continue; |
|
112
|
|
|
} |
|
113
|
4 |
|
} |
|
114
|
4 |
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* Gets the HTML renderer for this element |
|
118
|
|
|
* |
|
119
|
|
|
* @return RendererInterface |
|
120
|
|
|
*/ |
|
121
|
2 |
|
protected function getRenderer() |
|
122
|
|
|
{ |
|
123
|
2 |
|
return new FormRenderer($this); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* Gets the form id |
|
128
|
|
|
* |
|
129
|
|
|
* @return string |
|
130
|
|
|
*/ |
|
131
|
6 |
|
public function getId() |
|
132
|
|
|
{ |
|
133
|
6 |
|
return $this->id; |
|
134
|
|
|
} |
|
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) |
|
144
|
|
|
{ |
|
145
|
16 |
|
$this->id = $formId; |
|
146
|
16 |
|
return $this; |
|
147
|
|
|
} |
|
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) |
|
157
|
|
|
{ |
|
158
|
42 |
|
if ($element instanceof File) { |
|
159
|
2 |
|
$this->setAttribute('enctype', 'multipart/form-data'); |
|
160
|
2 |
|
} |
|
161
|
42 |
|
return parent::add($element); |
|
162
|
|
|
} |
|
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() |
|
191
|
|
|
{ |
|
192
|
6 |
|
if (null == $this->request) { |
|
193
|
2 |
|
$this->request = new Request(); |
|
194
|
2 |
|
} |
|
195
|
6 |
|
return $this->request; |
|
196
|
|
|
} |
|
197
|
|
|
|
|
198
|
|
|
/** |
|
199
|
|
|
* Assign form values from request |
|
200
|
|
|
*/ |
|
201
|
2 |
|
protected function updateValuesFromRequest() |
|
|
|
|
|
|
202
|
|
|
{ |
|
203
|
2 |
|
$data = $this->getRequest()->isPost() |
|
|
|
|
|
|
204
|
2 |
|
? $this->getRequest()->getPost() |
|
|
|
|
|
|
205
|
2 |
|
: $this->getRequest()->getQuery(); |
|
|
|
|
|
|
206
|
2 |
|
if (isset($_FILES)) { |
|
207
|
2 |
|
$data = array_merge($data, $this->request->getUploadedFiles()); |
|
208
|
2 |
|
} |
|
209
|
2 |
|
$this->setValues($data); |
|
210
|
|
|
|
|
211
|
|
|
} |
|
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: