| 1 | <?php |
||
| 8 | abstract class Validator |
||
| 9 | { |
||
| 10 | /** |
||
| 11 | * @var callable Callback on exception |
||
| 12 | */ |
||
| 13 | private $onExceptionCallback = [__CLASS__, 'defaultOnExceptionCallback']; |
||
| 14 | |||
| 15 | /** |
||
| 16 | * Validate tainted data |
||
| 17 | * |
||
| 18 | * @param mixed $tainted |
||
| 19 | * @return mixed The cleaned data |
||
| 20 | * @throws ValidationException If validation fails |
||
| 21 | */ |
||
| 22 | abstract public function validate($tainted); |
||
| 23 | |||
| 24 | /** |
||
| 25 | * Register on-exception callback |
||
| 26 | * |
||
| 27 | * The callback should take an \Exception object and proccess it as |
||
| 28 | * appropriate. This generally means throwing an exception of some kind |
||
| 29 | * or returning a replacement value. |
||
| 30 | * |
||
| 31 | * @param callable $callback |
||
| 32 | * @return self Instance for chaining |
||
| 33 | */ |
||
| 34 | public function onException(callable $callback) |
||
| 39 | |||
| 40 | /** |
||
| 41 | * Call the on-exception callback |
||
| 42 | * |
||
| 43 | * @param \Exception $exception |
||
| 44 | * @return mixed Whatever the callback returns |
||
| 45 | */ |
||
| 46 | protected function fireException(\Exception $exception) |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Simple callback that throws exceptions |
||
| 53 | * |
||
| 54 | * @param \Exception $exception |
||
| 55 | * @return void Never returns |
||
| 56 | * @throws \Exception throws supplied exception |
||
| 57 | */ |
||
| 58 | protected static function defaultOnExceptionCallback(\Exception $exception) |
||
| 62 | } |
||
| 63 |