1
|
|
|
<?php |
2
|
|
|
namespace Germania\FormValidator; |
3
|
|
|
|
4
|
|
|
class FormValidator implements FormValidatorInterface |
5
|
|
|
{ |
6
|
|
|
|
7
|
|
|
const SUBMITTED = 1; |
8
|
|
|
const VALID = 2; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Holds status definition |
12
|
|
|
* @var int |
13
|
|
|
*/ |
14
|
|
|
protected $flags; |
15
|
|
|
|
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var array |
19
|
|
|
*/ |
20
|
|
|
public $required_fields = array(); |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var array |
24
|
|
|
*/ |
25
|
|
|
public $optional_fields = array(); |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var callable |
29
|
|
|
*/ |
30
|
|
|
public $input_container_factory = array(); |
31
|
|
|
|
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param array $required Array with field and filter definitions |
35
|
|
|
* @param array $optional Array with field and filter definitions, default: empty array |
36
|
|
|
* @param array $input_container_factory Optional callable that takes the filtered input and returns an InputContainer |
37
|
|
|
*/ |
38
|
45 |
|
public function __construct(array $required, array $optional = array(), callable $input_container_factory = null ) |
39
|
|
|
{ |
40
|
45 |
|
$this->required_fields = $required; |
41
|
45 |
|
$this->optional_fields = $optional; |
42
|
45 |
|
$this->input_container_factory = $input_container_factory |
43
|
|
|
?: function( $filtered_input ) { return new InputContainer($filtered_input); }; |
44
|
45 |
|
} |
45
|
|
|
|
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param array $raw_user_input Ususally `$_POST` |
49
|
|
|
* @return InputContainer |
50
|
|
|
*/ |
51
|
30 |
|
public function __invoke( $raw_user_input, callable $input_container_factory = null ) |
52
|
|
|
{ |
53
|
|
|
// Reset status |
54
|
30 |
|
$this->setFlag(static::SUBMITTED, false); |
55
|
30 |
|
$this->setFlag(static::VALID, false); |
56
|
|
|
|
57
|
|
|
// Prepare result |
58
|
30 |
|
$filtered_input = filter_var_array( $raw_user_input, array_merge( |
59
|
30 |
|
$this->required_fields, |
60
|
30 |
|
$this->optional_fields |
61
|
6 |
|
)); |
62
|
|
|
|
63
|
|
|
// Evaluate result |
64
|
30 |
|
$is_empty_arr = array(); |
65
|
30 |
|
foreach( $this->required_fields as $required => $filter_constant): |
66
|
30 |
|
$is_empty_arr[ $required ] = !empty($filtered_input[ $required ]); |
67
|
6 |
|
endforeach; |
68
|
|
|
|
69
|
30 |
|
$submitted = in_array( true, $is_empty_arr, "strict"); |
70
|
30 |
|
$this->setFlag(static::SUBMITTED, $submitted); |
71
|
|
|
|
72
|
30 |
|
$valid = !in_array( false, $is_empty_arr, "strict"); |
73
|
30 |
|
$this->setFlag(static::VALID, $valid); |
74
|
|
|
|
75
|
|
|
// Return filtered data |
76
|
30 |
|
$factory = $input_container_factory ?: $this->input_container_factory; |
77
|
30 |
|
return $factory($filtered_input); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
|
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @implements FormValidatorInterface |
84
|
|
|
* @return boolean |
85
|
|
|
*/ |
86
|
15 |
|
public function isSubmitted() |
87
|
|
|
{ |
88
|
15 |
|
return $this->hasFlag( static::SUBMITTED ); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @implements FormValidatorInterface |
94
|
|
|
* @return boolean |
95
|
|
|
*/ |
96
|
15 |
|
public function isValid() |
97
|
|
|
{ |
98
|
15 |
|
return $this->hasFlag( static::SUBMITTED | static::VALID); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
|
102
|
|
|
|
103
|
15 |
|
protected function hasFlag($flag) |
104
|
|
|
{ |
105
|
15 |
|
return (($this->flags & $flag) == $flag); |
106
|
|
|
} |
107
|
|
|
|
108
|
30 |
|
protected function setFlag($flag, $value) |
109
|
|
|
{ |
110
|
30 |
|
if($value) { |
111
|
20 |
|
$this->flags |= $flag; |
112
|
4 |
|
} |
113
|
|
|
else { |
114
|
30 |
|
$this->flags &= ~$flag; |
115
|
|
|
} |
116
|
30 |
|
} |
117
|
|
|
|
118
|
15 |
|
public function addRequired( $field, $flag) |
119
|
|
|
{ |
120
|
15 |
|
$this->required_fields[ $field ] = $flag; |
121
|
15 |
|
$this->removeOptional( $field ); |
122
|
15 |
|
return $this; |
123
|
|
|
} |
124
|
|
|
|
125
|
15 |
|
public function removeRequired( $field ) |
126
|
|
|
{ |
127
|
15 |
|
if (array_key_exists($field, $this->required_fields)): |
128
|
15 |
|
unset($this->required_fields[ $field ]); |
129
|
3 |
|
endif; |
130
|
15 |
|
return $this; |
131
|
|
|
} |
132
|
|
|
|
133
|
15 |
|
public function addOptional( $field, $flag) |
134
|
|
|
{ |
135
|
15 |
|
$this->optional_fields[ $field ] = $flag; |
136
|
15 |
|
$this->removeRequired( $field ); |
137
|
15 |
|
return $this; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
|
141
|
15 |
|
public function removeOptional( $field ) |
142
|
|
|
{ |
143
|
15 |
|
if (array_key_exists($field, $this->optional_fields)): |
144
|
|
|
unset($this->optional_fields[ $field ]); |
145
|
|
|
endif; |
146
|
15 |
|
return $this; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
} |
150
|
|
|
|