Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [klasa] Klasa walidująca dane (ze zmiennych)
in5ane
post 29.10.2013, 16:39:45
Post #1





Grupa: Zarejestrowani
Postów: 1 335
Pomógł: 34
Dołączył: 9.11.2005
Skąd: Wrocław

Ostrzeżenie: (10%)
X----


Hej, chce poddać ocenie swoją klasę walidującą dane. Powiedzcie, co jest dobrze, a co źle. Co do robić, co przerobić. Proszę o opinie smile.gif

  1. <?php
  2.  
  3. /**
  4.  * Class data validation (can be used to form validation)
  5.  *
  6.  * Class data validation (can be used to form validation).
  7.  * Here is an example usage and available parameters.
  8.  * Example of use: $formValidation->validate($pass, "required|matches[$pass_c]|min_length[6]");
  9.  * Possible parameters to use:: required, numeric, matches[comparison], min_length[number], max_length[number], exact_length[number], valid_email
  10.  *
  11.  * @version 1.0
  12.  * @author Jakub Kubera <jakubkubera1992@gmail.com>
  13.  * @link www.jakubkubera.pl
  14.  * @license New BSD License
  15.  * @access public
  16.  */
  17. class ValidationData
  18. {
  19.  
  20. private $name;
  21. private $rules = array();
  22.  
  23. /**
  24.   * Assigning parameters to the class variables and run validation
  25.   *
  26.   * @param string $name Field name
  27.   * @param string $rules Field validation rules
  28.   *
  29.   * @return bool Returns whether validation was successful
  30.   */
  31. public function validate($name, $rules)
  32. {
  33. $this->name = $name;
  34.  
  35. $this->explodeRules($rules);
  36.  
  37. return $this->run();
  38. }
  39.  
  40. /**
  41.   * Returns whether validation was successful
  42.   *
  43.   * @return bool Returns whether validation was successful
  44.   */
  45. private function run()
  46. {
  47. $errors = array();
  48.  
  49. if (in_array('required', $this->rules))
  50. if ($this->checkRequired() === false)
  51. $errors[] = 'required';
  52.  
  53. if (in_array('numeric', $this->rules))
  54. if ($this->checkNumeric() === false)
  55. $errors[] = 'numeric';
  56.  
  57. if ($key = $this->pregArrayKey('/matches\[(.*)]/'))
  58. {
  59. preg_match('/\[(.*)]/', $this->rules[$key], $confirm);
  60. if ($this->checkMatches($confirm[1]) === false)
  61. $errors[] = $this->rules[$key];
  62. }
  63.  
  64. if ($key = $this->pregArrayKey('/min_length\[(.*)]/'))
  65. {
  66. preg_match('/\[(.*)]/', $this->rules[$key], $value);
  67. if ($this->checkMinLength($value[1]) === false)
  68. $errors[] = $this->rules[$key];
  69. }
  70.  
  71. if ($key = $this->pregArrayKey('/max_length\[(.*)]/'))
  72. {
  73. preg_match('/\[(.*)]/', $this->rules[$key], $value);
  74. if ($this->checkMaxLength($value[1]) === false)
  75. $errors[] = $this->rules[$key];
  76. }
  77.  
  78. if ($key = $this->pregArrayKey('/exact_length\[(.*)]/'))
  79. {
  80. preg_match('/\[(.*)]/', $this->rules[$key], $value);
  81. if ($this->checkLength($value[1]) === false)
  82. $errors[] = $this->rules[$key];
  83. }
  84.  
  85. if (in_array('valid_email', $this->rules))
  86. if ($this->checkEmail() === false)
  87. $errors[] = 'valid_email';
  88.  
  89. return (empty($errors)) ? true : $errors;
  90. }
  91.  
  92. /**
  93.   * Separation of validation rules
  94.   *
  95.   * @param $rules Field validation rules
  96.   */
  97. private function explodeRules($rules)
  98. {
  99. $this->rules = explode('|', $rules);
  100. }
  101.  
  102. /**
  103.   * Finds that there is a pattern in the rules,
  104.   * like in_array with using regular expressions
  105.   *
  106.   * @param $pattern Search pattern in the table
  107.   */
  108. private function pregArrayKey($pattern)
  109. {
  110. return key(preg_grep($pattern, $this->rules));
  111. }
  112.  
  113. /**
  114.   * Checks and returns whether the value is empty
  115.   *
  116.   * @return bool Returns whether the value is empty
  117.   */
  118. private function checkRequired()
  119. {
  120. return (!empty($this->name)) ? true : false;
  121. }
  122.  
  123. /**
  124.   * Checks and returns whether the value is numeric
  125.   *
  126.   * @return bool Returns whether the value is numeric
  127.   */
  128. private function checkNumeric()
  129. {
  130. return (is_numeric($this->name)) ? true : false;
  131. }
  132.  
  133. /**
  134.   * Checks and returns whether the specified phrases are the same
  135.   *
  136.   * @return bool Returns whether the specified phrases are the same
  137.   */
  138. private function checkMatches($confirm)
  139. {
  140. return ($this->name == $confirm) ? true : false;
  141. }
  142.  
  143. /**
  144.   * Checks and returns whether the value of a specified minimum length
  145.   *
  146.   * @return bool Returns whether the value of a specified minimum length
  147.   */
  148. private function checkMinLength($value)
  149. {
  150. return (strlen($this->name) > $value) ? true : false;
  151. }
  152.  
  153. /**
  154.   * Checks and returns whether the value of a specified maximum length
  155.   *
  156.   * @return bool Returns whether the value of a specified maximum length
  157.   */
  158. private function checkMaxLength($value)
  159. {
  160. return (strlen($this->name) < $value) ? true : false;
  161. }
  162.  
  163. /**
  164.   * Checks and returns whether the value of a specified length
  165.   *
  166.   * @return bool Returns whether the value of a specified length
  167.   */
  168. private function checkLength($value)
  169. {
  170. return (strlen($this->name) == $value) ? true : false;
  171. }
  172.  
  173. /**
  174.   * Checks and returns whether the given value is a valid email address
  175.   *
  176.   * @return bool Returns whether the given value is a valid email address
  177.   */
  178. private function checkEmail()
  179. {
  180. return (preg_match('/^([a-zA-Z0-9])+([a-zA-Z0-9\._-])*@([a-zA-Z0-9_-])+([a-zA-Z0-9\._-]+)+$/', $this->name)) ? true : false;
  181. }
  182.  
  183. }


Przykład użycia:
  1. <?php
  2. $formValidation = new ValidationData;
  3.  
  4. $age = '123';
  5. var_dump($formValidation->validate($age, "required|numeric|max_length[4]"));
  6.  
  7. $name = 'Kuba';
  8. var_dump($formValidation->validate($name, "required|min_length[5]"));
  9.  
  10. $pass = 'test';
  11. $pass_c = 'test';
  12. var_dump($formValidation->validate($pass, "required|matches[$pass_c]|exact_length[4]"));
  13.  
  14. $email = 'test1test.pl';
  15. if ($formValidation->validate($email, "required|valid_email") === true)
  16. echo 'Adres e-mail '.$email.' poprawnie zostal zwalidowany';
  17. else {
  18. echo 'Blad, adres e-mail '.$email.' nie zostal poprawnie zwalidowany, ponizej bledy w tablicy $errors:<br />';
  19. var_dump($formValidation->validate($email, "required|valid_email"));
  20. }


Ten post edytował in5ane 29.10.2013, 17:07:57


--------------------
> > > Tworzenie stron < < <
Go to the top of the page
+Quote Post

Posty w temacie


Reply to this topicStart new topic
1 Użytkowników czyta ten temat (1 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Wersja Lo-Fi Aktualny czas: 14.08.2025 - 10:59