12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576 |
- <?php
- class Validator {
- static $errors = true;
- static function check($arr, $on = false) {
- if ($on === false) {
- $on = $_REQUEST;
- }
- foreach ($arr as $value) {
- if (empty($on[$value])) {
- self::throwError('Data is missing', 900);
- }
- }
- }
- static function int($val) {
- $val = filter_var($val, FILTER_VALIDATE_INT);
- if ($val === false) {
- self::throwError('Invalid Integer', 901);
- }
- return $val;
- }
- static function str($val, $empty=false) {
- if (!is_string($val)) {
- self::throwError('Invalid String', 902);
- }
- $val = trim(htmlspecialchars($val));
- if(strlen($val) === 0 && !$empty) self::throwError('Empty String', 907);
- return $val;
- }
- static function bool($val) {
- $val = filter_var($val, FILTER_VALIDATE_BOOLEAN);
- return $val;
- }
- static function email($val) {
- $val = filter_var($val, FILTER_VALIDATE_EMAIL);
- if ($val === false) {
- self::throwError('Invalid Email', 903);
- }
- return $val;
- }
- static function url($val) {
- $val = filter_var($val, FILTER_VALIDATE_URL);
- if ($val === false) {
- self::throwError('Invalid URL', 904);
- }
- return $val;
- }
- static function file($val, $ext = false) {
- if (($val['error'] !== UPLOAD_ERR_OK) || !is_uploaded_file($val['tmp_name'])) {
- self::throwError('File upload error',905);
- }
- if ($ext) {
- $fileName = $val['name'];
- // $fileSize = $val['size'];
- $fileNameCmps = explode(".", $fileName);
- $fileExtension = strtolower(end($fileNameCmps));
- if (strcmp($ext, $fileExtension) !== 0) {
- self::throwError('Invalid file extension',906);
- }
- }
- return $val;
- }
- static function throwError($error = 'Error In Processing', $errorCode = 0) {
- if (self::$errors === true) {
- throw new Exception($error, $errorCode);
- }
- }
- }
|