1 | <?php |
||
20 | class Validator { |
||
21 | |||
22 | /** |
||
23 | * Holds the available validators. |
||
24 | */ |
||
25 | protected $availableValidators; |
||
26 | |||
27 | /** |
||
28 | * Creates instances of the available validators. |
||
29 | * |
||
30 | * @param array $validators |
||
31 | * the validators to load, key = name, value = classname within the |
||
32 | * namespace "\Valdi\Validator" |
||
33 | */ |
||
34 | protected function createValidators(array $validators) { |
||
41 | |||
42 | /** |
||
43 | * Validates a single rule. |
||
44 | * |
||
45 | * @param string $validator |
||
46 | * the validator to use |
||
47 | * @param string[] $parameters |
||
48 | * the validation parameters, depending on the validator |
||
49 | * @param string $value |
||
50 | * the value to validate |
||
51 | * |
||
52 | * @return boolean |
||
53 | * true if the value is valid |
||
54 | */ |
||
55 | protected function isValidRule($validator, $parameters, $value) { |
||
61 | |||
62 | /** |
||
63 | * Constructor. |
||
64 | */ |
||
65 | public function __construct() { |
||
86 | |||
87 | /** |
||
88 | * Adds additional validator. It can override existing validators as well. |
||
89 | * |
||
90 | * @param string $name |
||
91 | * the name of the new validator. |
||
92 | * @param ValidatorInterface $validator |
||
93 | * the validator to add |
||
94 | */ |
||
95 | public function addValidator($name, ValidatorInterface $validator) { |
||
98 | |||
99 | /** |
||
100 | * Validates a value via the given rules. |
||
101 | * |
||
102 | * @param array $rules |
||
103 | * the validation rules |
||
104 | * @param string $value |
||
105 | * the value to validate |
||
106 | * |
||
107 | * @return string[] |
||
108 | * the fields where the validation failed |
||
109 | */ |
||
110 | public function isValidValue($rules, $value) { |
||
122 | |||
123 | /** |
||
124 | * Performs the actual validation. |
||
125 | * |
||
126 | * @param array $rules |
||
127 | * the validation rules: an array with a field name as key and an array |
||
128 | * of rules to use for this field; each rule is an array with the validator |
||
129 | * name as first element and parameters as following elements; example: |
||
130 | * array('a' => array(array('required')), 'b' => array(array('min', 1))) |
||
131 | * @param array $data |
||
132 | * the data to validate as a map |
||
133 | * |
||
134 | * @return array<string,boolean|array> |
||
135 | * the validation result having the keys "valid" (true or false) and |
||
136 | * the key "errors" containing all failed fields as keys with arrays of the |
||
137 | * failed validator names; example where the field "b" from the above sample |
||
138 | * failed due to the min validator: |
||
139 | * array('valid' => false, errors => array('b' => array('min'))) |
||
140 | */ |
||
141 | public function isValid(array $rules, array $data) { |
||
155 | |||
156 | } |
||
157 |