1 | <?php |
||
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 ) |
|
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 ) |
|
79 | |||
80 | |||
81 | |||
82 | /** |
||
83 | * @implements FormValidatorInterface |
||
84 | * @return boolean |
||
85 | */ |
||
86 | 15 | public function isSubmitted() |
|
90 | |||
91 | |||
92 | /** |
||
93 | * @implements FormValidatorInterface |
||
94 | * @return boolean |
||
95 | */ |
||
96 | 15 | public function isValid() |
|
100 | |||
101 | |||
102 | |||
103 | 15 | protected function hasFlag($flag) |
|
107 | |||
108 | 30 | protected function setFlag($flag, $value) |
|
117 | |||
118 | 15 | public function addRequired( $field, $flag) |
|
124 | |||
125 | 15 | public function removeRequired( $field ) |
|
132 | |||
133 | 15 | public function addOptional( $field, $flag) |
|
139 | |||
140 | |||
141 | 15 | public function removeOptional( $field ) |
|
148 | |||
149 | } |
||
150 |