|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of the Ray.WebFormModule package. |
|
4
|
|
|
* |
|
5
|
|
|
* @license http://opensource.org/licenses/MIT MIT |
|
6
|
|
|
*/ |
|
7
|
|
|
namespace Ray\WebFormModule; |
|
8
|
|
|
|
|
9
|
|
|
use Aura\Filter\FilterFactory; |
|
10
|
|
|
use Aura\Filter\SubjectFilter; |
|
11
|
|
|
use Aura\Html\HelperLocator; |
|
12
|
|
|
use Aura\Html\HelperLocatorFactory; |
|
13
|
|
|
use Aura\Input\AntiCsrfInterface; |
|
14
|
|
|
use Aura\Input\BuilderInterface; |
|
15
|
|
|
use Aura\Input\Fieldset; |
|
16
|
|
|
use Ray\WebFormModule\Exception\CsrfViolationException; |
|
17
|
|
|
use Ray\WebFormModule\Exception\LogicException; |
|
18
|
|
|
|
|
19
|
|
|
abstract class AbstractForm extends Fieldset implements FormInterface |
|
20
|
|
|
{ |
|
21
|
|
|
/** |
|
22
|
|
|
* @var SubjectFilter |
|
23
|
|
|
*/ |
|
24
|
|
|
protected $filter; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var null | array |
|
28
|
|
|
*/ |
|
29
|
|
|
protected $errorMessages; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var HelperLocator |
|
33
|
|
|
*/ |
|
34
|
|
|
protected $helper; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @var AntiCsrfInterface |
|
38
|
|
|
*/ |
|
39
|
|
|
protected $antiCsrf; |
|
40
|
|
|
|
|
41
|
24 |
|
public function __construct() |
|
42
|
|
|
{ |
|
43
|
24 |
|
} |
|
44
|
|
|
|
|
45
|
1 |
|
public function __clone() |
|
46
|
|
|
{ |
|
47
|
1 |
|
$this->filter = clone $this->filter; |
|
48
|
1 |
|
$this->init(); |
|
49
|
1 |
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Return form markup string |
|
53
|
|
|
* |
|
54
|
|
|
* @return string |
|
55
|
|
|
*/ |
|
56
|
1 |
|
public function __toString() |
|
57
|
|
|
{ |
|
58
|
|
|
try { |
|
59
|
1 |
|
if (! $this instanceof ToStringInterface) { |
|
60
|
1 |
|
throw new LogicException(ToStringInterface::class . ' is not implemented'); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
return $this->toString(); |
|
|
|
|
|
|
64
|
1 |
|
} catch (\Exception $e) { |
|
65
|
1 |
|
trigger_error($e->getMessage() . PHP_EOL . $e->getTraceAsString(), E_USER_ERROR); |
|
66
|
|
|
|
|
67
|
1 |
|
return ''; |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* @param BuilderInterface $builder |
|
73
|
|
|
* @param FilterFactory $filterFactory |
|
74
|
|
|
* @param HelperLocatorFactory $helperFactory |
|
75
|
|
|
* |
|
76
|
|
|
* @\Ray\Di\Di\Inject |
|
77
|
|
|
*/ |
|
78
|
24 |
|
public function setBaseDependencies( |
|
79
|
|
|
BuilderInterface $builder, |
|
80
|
|
|
FilterFactory $filterFactory, |
|
81
|
|
|
HelperLocatorFactory $helperFactory |
|
82
|
|
|
) { |
|
83
|
24 |
|
$this->builder = $builder; |
|
|
|
|
|
|
84
|
24 |
|
$this->filter = $filterFactory->newSubjectFilter(); |
|
85
|
24 |
|
$this->helper = $helperFactory->newInstance(); |
|
86
|
24 |
|
} |
|
87
|
|
|
|
|
88
|
1 |
|
public function setAntiCsrf(AntiCsrfInterface $antiCsrf) |
|
89
|
|
|
{ |
|
90
|
1 |
|
$this->antiCsrf = $antiCsrf; |
|
91
|
1 |
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* @\Ray\Di\Di\PostConstruct |
|
95
|
|
|
*/ |
|
96
|
24 |
|
public function postConstruct() |
|
97
|
|
|
{ |
|
98
|
24 |
|
$this->init(); |
|
99
|
24 |
|
if ($this->antiCsrf instanceof AntiCsrfInterface) { |
|
100
|
13 |
|
$this->antiCsrf->setField($this); |
|
101
|
|
|
} |
|
102
|
24 |
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* {@inheritdoc} |
|
106
|
|
|
*/ |
|
107
|
2 |
|
public function input($input) |
|
108
|
|
|
{ |
|
109
|
2 |
|
return $this->helper->input($this->get($input)); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* {@inheritdoc} |
|
114
|
|
|
*/ |
|
115
|
2 |
|
public function error($input) |
|
116
|
|
|
{ |
|
117
|
2 |
|
if (! $this->errorMessages) { |
|
118
|
2 |
|
$failure = $this->filter->getFailures(); |
|
119
|
2 |
|
if ($failure) { |
|
120
|
1 |
|
$this->errorMessages = $failure->getMessages(); |
|
121
|
|
|
} |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
2 |
|
if (isset($this->errorMessages[$input])) { |
|
125
|
1 |
|
return $this->errorMessages[$input][0]; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
1 |
|
return ''; |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* @param array $attr attributes for the form tag |
|
133
|
|
|
* |
|
134
|
|
|
* @throws \Aura\Html\Exception\HelperNotFound |
|
135
|
|
|
* @throws \Aura\Input\Exception\NoSuchInput |
|
136
|
|
|
* |
|
137
|
|
|
* @return string |
|
138
|
|
|
*/ |
|
139
|
3 |
|
public function form($attr = []) |
|
140
|
|
|
{ |
|
141
|
3 |
|
$form = $this->helper->form($attr); |
|
142
|
3 |
|
if (isset($this->inputs['__csrf_token'])) { |
|
143
|
1 |
|
$form .= $this->helper->input($this->get('__csrf_token')); |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
3 |
|
return $form; |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
/** |
|
150
|
|
|
* Applies the filter to a subject. |
|
151
|
|
|
* |
|
152
|
|
|
* @param array $data |
|
153
|
|
|
* |
|
154
|
|
|
* @throws CsrfViolationException |
|
155
|
|
|
* |
|
156
|
|
|
* @return bool |
|
157
|
|
|
*/ |
|
158
|
10 |
|
public function apply(array $data) |
|
159
|
|
|
{ |
|
160
|
10 |
|
if ($this->antiCsrf && ! $this->antiCsrf->isValid($data)) { |
|
161
|
1 |
|
throw new CsrfViolationException; |
|
162
|
|
|
} |
|
163
|
9 |
|
$this->fill($data); |
|
164
|
|
|
|
|
165
|
9 |
|
return $this->filter->apply($data); |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
/** |
|
169
|
|
|
* Returns all failure messages for all fields. |
|
170
|
|
|
* |
|
171
|
|
|
* @return array |
|
172
|
|
|
*/ |
|
173
|
4 |
|
public function getFailureMessages() |
|
174
|
|
|
{ |
|
175
|
4 |
|
return $this->filter->getFailures()->getMessages(); |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
/** |
|
179
|
|
|
* Returns all the fields collection |
|
180
|
|
|
* |
|
181
|
|
|
* @return \ArrayIterator |
|
182
|
|
|
*/ |
|
183
|
1 |
|
public function getIterator() |
|
184
|
|
|
{ |
|
185
|
1 |
|
return new \ArrayIterator($this->inputs); |
|
186
|
|
|
} |
|
187
|
|
|
} |
|
188
|
|
|
|
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.