1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* |
4
|
|
|
* This file is part of the Aura project for PHP. |
5
|
|
|
* |
6
|
|
|
* @package Aura.Input |
7
|
|
|
* |
8
|
|
|
* @license http://opensource.org/licenses/MIT-license.php MIT |
9
|
|
|
* |
10
|
|
|
*/ |
11
|
|
|
namespace Aura\Input; |
12
|
|
|
|
13
|
|
|
use ArrayIterator; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* |
17
|
|
|
* A top-level form object to contain fields, fieldsets, collections, and |
18
|
|
|
* anti-CSRF protection. |
19
|
|
|
* |
20
|
|
|
* @package Aura.Input |
21
|
|
|
* |
22
|
|
|
*/ |
23
|
|
|
class Form extends Fieldset |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* |
27
|
|
|
* The anti-CSRF implementation, if any. |
28
|
|
|
* |
29
|
|
|
* @var AntiCsrfInterface |
30
|
|
|
* |
31
|
|
|
*/ |
32
|
|
|
protected $anti_csrf; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* |
36
|
|
|
* Sets the anti-CSRF implementation; calls the `setField()` method on the |
37
|
|
|
* implementation to set the anti-CSRF field. |
38
|
|
|
* |
39
|
|
|
* @param AntiCsrfInterface $anti_csrf The anti-CSRF implementation. |
40
|
|
|
* |
41
|
|
|
* @return void |
42
|
|
|
* |
43
|
|
|
*/ |
44
|
|
|
public function setAntiCsrf(AntiCsrfInterface $anti_csrf) |
45
|
|
|
{ |
46
|
|
|
$this->anti_csrf = $anti_csrf; |
47
|
|
|
$this->anti_csrf->setField($this); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* |
52
|
|
|
* Returns the anti-CSRF implementation, if any. |
53
|
|
|
* |
54
|
|
|
* @return mixed |
55
|
|
|
* |
56
|
|
|
*/ |
57
|
|
|
public function getAntiCsrf() |
58
|
|
|
{ |
59
|
|
|
return $this->anti_csrf; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* |
64
|
|
|
* Fills this form with input values. |
65
|
|
|
* |
66
|
|
|
* If an anti-CSRF implementation is set and the incoming data does not |
67
|
|
|
* have a valid anti-CSRF value, it will throw an exception. |
68
|
|
|
* |
69
|
|
|
* @param array $data The values for this fieldset. |
70
|
|
|
* |
71
|
|
|
* @return void |
72
|
|
|
* |
73
|
|
|
* @throws Exception\CsrfViolation if an anti-CSRF implementation is set |
74
|
|
|
* and the incoming data does not have a valid anti-CSRF value. |
75
|
|
|
* |
76
|
|
|
*/ |
77
|
|
|
public function fill(array $data) |
78
|
|
|
{ |
79
|
|
|
if ($this->anti_csrf && ! $this->anti_csrf->isValid($data)) { |
80
|
|
|
throw new Exception\CsrfViolation; |
81
|
|
|
} |
82
|
|
|
parent::fill($data); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* |
87
|
|
|
* Returns all the fields collection |
88
|
|
|
* |
89
|
|
|
* @return ArrayIterator |
90
|
|
|
* |
91
|
|
|
*/ |
92
|
|
|
public function getIterator() |
93
|
|
|
{ |
94
|
|
|
return new ArrayIterator($this->inputs); |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|