Advertisement
Guest User

Untitled

a guest
Oct 31st, 2014
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.06 KB | None | 0 0
  1. <?php
  2. /**
  3. Validation Class
  4. * Class to to validate any Data send by user depend in Data type and Rules
  5. * @package Front
  6. */
  7. //namespace Front;
  8.  
  9. class ValidationSystem {
  10.  
  11. /**
  12. * @var Array for Validation Error in System
  13. * @access private
  14. */
  15. private Static $validation_errors;
  16.  
  17. /**
  18. *
  19. * Class Construct Function
  20. * @return void
  21. * @access public
  22. */
  23. public function __construct() {
  24. self::$validation_errors = array();
  25. }
  26.  
  27. /**
  28. * function for Validate input Value by Validation Result like boolean value
  29. * @param type $input_value
  30. * @return boolean
  31. * @access public
  32. */
  33. public static function do_validation($input_value) {
  34. $validation_result = TRUE;
  35. foreach ($input_value as $obj_name => $data_obj) {
  36.  
  37. switch ($data_obj['rule']) {
  38. case 'EMAIL':
  39. if (!self::is_valid_email($data_obj['value'], $obj_name)) {
  40. $validation_result = FALSE;
  41. }
  42. break;
  43. case 'URL':
  44. if (!self::is_valid_url($data_obj['value'], $obj_name)) {
  45. $validation_result = FALSE;
  46. }
  47. break;
  48. case 'NorwayMobile':
  49. if (!self::is_norwegian_number($data_obj['value'], $obj_name)) {
  50. $validation_result = FALSE;
  51. }
  52. break;
  53. default :
  54. self::$validation_errors[] = "INVALID_RULE_ERROR_MESSAGE";
  55. $validation_result = FALSE;
  56. break;
  57. }
  58. }
  59. return $validation_result;
  60. }
  61.  
  62. /**
  63. * function for return validation Error
  64. * @return array Error Array
  65. * @access public
  66. */
  67. public static function get_validation_error_message() {
  68. return self::$validation_errors;
  69. }
  70.  
  71. /**
  72. * function for Validate Email Value;
  73. * @param String $email_str Email Text value
  74. * @return Boolean
  75. * @access Public
  76. */
  77. public function is_valid_email($email_str, $obj_name) {
  78. if (!filter_var($email_str, FILTER_VALIDATE_EMAIL)) {
  79. self::$validation_errors[$obj_name] = "INVALIED_EMAIL_ERROR_MESSAGE";
  80. return FALSE;
  81. }
  82. return TRUE;
  83. }
  84.  
  85. /**
  86. * Function for Validate URL Value
  87. * @param String $url_str URL Text Value
  88. * @return boolean
  89. * @access public
  90. */
  91. public function is_valid_url($url_str, $obj_name) {
  92.  
  93. $url_str = trim($url_str);
  94. //add Http to link if not have any protocal
  95. $url_str = parse_url($url_str, PHP_URL_SCHEME) === null ? 'http://' . $url_str : $url_str;
  96. // check if is invalid URL or Not
  97. $is_valid = TRUE;
  98. //first check with php's FILTER_VALIDATE_URL
  99. if (!filter_var($url_str, FILTER_VALIDATE_URL, FILTER_FLAG_SCHEME_REQUIRED | FILTER_FLAG_HOST_REQUIRED)) {
  100. $is_valid = FALSE;
  101. } else {
  102. //check host format is valid ex : www.domainName.com or domain.com
  103. $host = parse_url($url_str, PHP_URL_HOST);
  104. $dotcount = substr_count($host, '.');
  105. if ($dotcount > 0) {
  106. if ($dotcount == 1) {
  107. if (strpos($host, 'www.') === 0) {
  108. $is_valid = FALSE;
  109. }
  110. }
  111. } else {
  112. $is_valid = FALSE;
  113. }
  114. }
  115. if ($is_valid) {
  116. return TRUE;
  117. } else {
  118. self::$validation_errors[$obj_name] = "INVALIED_URL_ERROR_MESSAGE";
  119. return FALSE;
  120. }
  121. }
  122.  
  123. /**
  124. * function for check mobile Number as mobile Number Norway Format
  125. * @param integer $phone_number
  126. * @return boolean
  127. * @access Public
  128. */
  129. public function is_norwegian_number($phone_number, $obj_name) {
  130. if (preg_match("/^(((4|9){1})d{7})|(((59){1})d{6})$/", $phone_number) == 0) {
  131. self::$validation_errors[$obj_name] = "INVILAD_NORWAY_MOBILE_ERROR_MESSAGE";
  132. return FALSE;
  133. }
  134. return TRUE;
  135. }
  136.  
  137. /**
  138. * function for testing class with some Defult value for systems
  139. * @return Output Testing Result;
  140. * @access public
  141. */
  142. public static function validation_test() {
  143. $email_info = "nobody@exmple.com";
  144. $url_info = "domain.com";
  145. $norwegian_mobile = "41111111";
  146.  
  147. /* make test with above Default Value */
  148. $vildation_Result = self::do_validation(
  149. array(
  150. 'email_Filed' => array('rule' => 'EMAIL', 'value' => $email_info),
  151. "url_field" => array('rule' => "URL", "value" => $url_info),
  152. "mobile_feild" => array("rule" => 'NorwayMobile', "value" => $norwegian_mobile)
  153. )
  154. );
  155. if ($vildation_Result) {
  156. echo "vildation was corrct and all Field input correcttly ";
  157. } else {
  158. echo "<pre>";
  159. print_r(self::get_validation_error_message());
  160. echo "</pre>";
  161. }
  162. }
  163.  
  164. }
  165.  
  166. ?>
  167.  
  168. <?php error_reporting(0);
  169. /**
  170. * Validate.php
  171. * Control for Validation submited Data and Return valdition Result as JOSN object .
  172. * @package Front
  173. * @example ../validate.phpemail=nobady@dmainname.com&url=domainname.com&mobile=95001122
  174. */
  175. //add Validation Calss
  176. require_once('ValidationSystem.php');
  177.  
  178. /**
  179. * @var Array Array for All Text Message in System to make easy for translate and edit
  180. */
  181. $message_txt_EN = array();
  182. $message_txt_EN['INVALID_EMPTY_DATA_MESSAGE'] = "No any object to Valid it";
  183. $message_txt_EN['INVALID_RULE_ERROR_MESSAGE'] = "No any of submit objects is URL ,E-mail or Mobile Number.";
  184. $message_txt_EN['INVALIED_EMAIL_ERROR_MESSAGE'] = " E-mail is invalid format, please check it again. ";
  185. $message_txt_EN['INVALIED_URL_ERROR_MESSAGE'] = "URL is invalid format, please check it again. ";
  186. $message_txt_EN['INVILAD_NORWAY_MOBILE_ERROR_MESSAGE'] = "Mobile number is not Vaild mobile number in Norway";
  187. $message_txt_EN['SUCCESS_VALIDATION_MESSAGE'] = "Congratulation all data you were submitted is valid format.";
  188.  
  189. // get sumbmited Data form Api (get) or index page "POST"
  190. if (isset($_POST) && !empty($_POST)) {
  191. $object_data = $_POST;
  192. } else {
  193. $object_data = $_GET;
  194. }
  195. /* Decodes URL */
  196. foreach ($object_data as $data_key => $data_value) {
  197. $object_data[$data_key] = urldecode($data_value);
  198. }
  199. /**
  200. * @var Array Validation Result
  201. */
  202. $respond_result = array();
  203. /**
  204. * check is Data not Empty and fileds is correct and make Validation process on it
  205. */
  206. if (!empty($object_data)) {
  207. $vildate_data = array();
  208. /* create rule name for every object to validate depend on it . */
  209. $vaid_fields = array('email' => 'EMAIL', 'url' => 'URL', 'mobile' => 'NorwayMobile');
  210.  
  211. foreach ($object_data as $obj_key => $obj_value) {
  212.  
  213. if (!empty($obj_value)) {
  214.  
  215. if (array_key_exists($obj_key, $vaid_fields)) {
  216.  
  217. $vildate_data [$obj_key] = array('rule' => $vaid_fields[$obj_key], 'value' => $obj_value);
  218. }
  219. }
  220. }
  221.  
  222. /* check if Data is valid or not and return Json Result */
  223. if (!empty($vildate_data)) {
  224.  
  225. if (ValidationSystem::do_validation($vildate_data)) {
  226.  
  227. $respond_result = array('result' => TRUE, "message" => array($message_txt_EN['SUCCESS_VALIDATION_MESSAGE']));
  228. } else {
  229. $error_result = ValidationSystem::get_validation_error_message();
  230.  
  231. foreach ($error_result as $er_key => $err_txt) {
  232.  
  233. $error_result[$er_key] = $message_txt_EN[$err_txt];
  234. }
  235. $respond_result = array('result' => FALSE, 'message' => $error_result);
  236. }
  237. } else {
  238.  
  239. $respond_result = array('result' => FALSE, "message" => array($message_txt_EN['INVALID_EMPTY_DATA_MESSAGE']));
  240. }
  241. } else {
  242.  
  243. $respond_result = array('result' => FALSE, "message" => array($message_txt_EN['INVALID_EMPTY_DATA_MESSAGE']));
  244. }
  245. echo json_encode($respond_result);
  246. exit();
  247. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement