|
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
|
|
|
/** |
|
29
|
|
|
* @param array $required Array with field and filter definitions |
|
30
|
|
|
* @param array $optional Array with field and filter definitions, default: empty array |
|
31
|
|
|
*/ |
|
32
|
6 |
|
public function __construct(array $required, array $optional = array() ) |
|
33
|
|
|
{ |
|
34
|
6 |
|
$this->required_fields = $required; |
|
35
|
6 |
|
$this->optional_fields = $optional; |
|
36
|
6 |
|
} |
|
37
|
|
|
|
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @param array $raw_user_input Ususally `$_POST` |
|
41
|
|
|
* @return array The filtered user input |
|
42
|
|
|
*/ |
|
43
|
6 |
|
public function __invoke( $raw_user_input ) |
|
44
|
|
|
{ |
|
45
|
|
|
// Reset status |
|
46
|
6 |
|
$this->setFlag(static::SUBMITTED, false); |
|
47
|
6 |
|
$this->setFlag(static::VALID, false); |
|
48
|
|
|
|
|
49
|
|
|
// Prepare result |
|
50
|
6 |
|
$filtered_input = filter_var_array( $raw_user_input, array_merge( |
|
51
|
6 |
|
$this->required_fields, |
|
52
|
6 |
|
$this->optional_fields |
|
53
|
3 |
|
)); |
|
54
|
|
|
|
|
55
|
|
|
// Evaluate result |
|
56
|
6 |
|
$is_empty_arr = array(); |
|
57
|
6 |
|
foreach( $this->required_fields as $required => $filter_constant): |
|
58
|
6 |
|
$is_empty_arr[ $required ] = !empty($filtered_input[ $required ]); |
|
59
|
3 |
|
endforeach; |
|
60
|
|
|
|
|
61
|
6 |
|
$submitted = in_array( true, $is_empty_arr, "strict"); |
|
62
|
6 |
|
$this->setFlag(static::SUBMITTED, $submitted); |
|
63
|
|
|
|
|
64
|
6 |
|
$valid = !in_array( false, $is_empty_arr, "strict"); |
|
65
|
6 |
|
$this->setFlag(static::VALID, $valid); |
|
66
|
|
|
|
|
67
|
|
|
// Return filtered data |
|
68
|
6 |
|
return new InputContainer($filtered_input); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
|
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* @implements FormValidatorInterface |
|
75
|
|
|
* @return boolean |
|
76
|
|
|
*/ |
|
77
|
6 |
|
public function isSubmitted() |
|
78
|
|
|
{ |
|
79
|
6 |
|
return $this->hasFlag( static::SUBMITTED ); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* @implements FormValidatorInterface |
|
85
|
|
|
* @return boolean |
|
86
|
|
|
*/ |
|
87
|
6 |
|
public function isValid() |
|
88
|
|
|
{ |
|
89
|
6 |
|
return $this->hasFlag( static::SUBMITTED | static::VALID); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
|
|
93
|
|
|
|
|
94
|
6 |
|
protected function hasFlag($flag) |
|
95
|
|
|
{ |
|
96
|
6 |
|
return (($this->flags & $flag) == $flag); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
6 |
|
protected function setFlag($flag, $value) |
|
100
|
|
|
{ |
|
101
|
6 |
|
if($value) { |
|
102
|
4 |
|
$this->flags |= $flag; |
|
103
|
2 |
|
} |
|
104
|
|
|
else { |
|
105
|
6 |
|
$this->flags &= ~$flag; |
|
106
|
|
|
} |
|
107
|
6 |
|
} |
|
108
|
|
|
|
|
109
|
|
|
public function addRequired( $field, $flag) |
|
110
|
|
|
{ |
|
111
|
|
|
$this->required_fields[ $field ] = $flag; |
|
112
|
|
|
$this->removeOptional( $field ); |
|
113
|
|
|
return $this; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
public function removeRequired( $field ) |
|
117
|
|
|
{ |
|
118
|
|
|
if (array_key_exists($field, $this->required_fields)): |
|
119
|
|
|
unset($this->required_fields[ $field ]); |
|
120
|
|
|
endif; |
|
121
|
|
|
return $this; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
public function addOptional( $field, $flag) |
|
125
|
|
|
{ |
|
126
|
|
|
$this->optional_fields[ $field ] = $flag; |
|
127
|
|
|
$this->removeRequired( $field ); |
|
128
|
|
|
return $this; |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
|
|
132
|
|
|
public function removeOptional( $field ) |
|
133
|
|
|
{ |
|
134
|
|
|
if (array_key_exists($field, $this->optional_fields)): |
|
135
|
|
|
unset($this->optional_fields[ $field ]); |
|
136
|
|
|
endif; |
|
137
|
|
|
return $this; |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
} |
|
141
|
|
|
|