/sPHPf/vendors/coldstarstudios/validation/Validation.php

https://github.com/alrik11es/sPHPf · PHP · 158 lines · 86 code · 17 blank · 55 comment · 17 complexity · 5a34f146bf82d44e4082eb603e91a9aa MD5 · raw file

  1. <?php
  2. namespace coldstarstudios\validation;
  3. /**
  4. * This class is designed to allow an easy use of all validations
  5. * called stack because is a stack of errors that will be checked
  6. * if valid or not.
  7. *
  8. * @author ALRIK
  9. * @license MIT
  10. */
  11. class Validation {
  12. public $isValid = true;
  13. public $errors = array();
  14. private $count = 0;
  15. // Temporary condition for generic checks.
  16. public $condition;
  17. /**
  18. * Checks if a $param is empty or not.
  19. * @param string $param
  20. * @param string $message
  21. */
  22. function isEmpty($param, $message){
  23. $this->count++;
  24. if(empty($param)){
  25. $this->isValid = false;
  26. array_push($this->errors, array('id'=>$this->count, 'message'=>$message));
  27. }
  28. }
  29. /**
  30. * Check some condition if is valid or not.
  31. * @param string $message
  32. * @param boolean $condition
  33. */
  34. function generic($message, $condition = null){
  35. $this->count++;
  36. if($condition != null)
  37. $condition = $condition;
  38. else
  39. $condition = $this->condition;
  40. if($condition){
  41. $this->isValid = false;
  42. array_push($this->errors, array('id'=>$this->count, 'message'=>$message));
  43. }
  44. }
  45. /**
  46. * Checks if a file is valid or not.
  47. * @param $_FILE $param
  48. * @param array $mime
  49. * @param int $size
  50. * @param string $message_size
  51. * @param string $message_mime
  52. */
  53. function file($param, $mime, $size, $message_size, $message_mime){
  54. $this->count++;
  55. if(!empty($param['tmp_name'])){
  56. if($param['size'] > $size) {
  57. $this->isValid = false;
  58. array_push($this->errors, array('id'=>$this->count, 'message'=>$message_size));
  59. }
  60. if(!\coldstarstudios\uploads\Upload::validateMimeType($param['type'], $mime)) {
  61. $this->isValid = false;
  62. array_push($this->errors, array('id'=>$this->count, 'message'=>$message_mime));
  63. }
  64. }
  65. }
  66. /**
  67. * Checks if two parameters are equal or not.
  68. * @param type $param1
  69. * @param type $param2
  70. * @param type $message
  71. */
  72. function equal($param1, $param2, $message){
  73. $this->count++;
  74. if($param1 != $param2) {
  75. $this->isValid = false;
  76. array_push($this->errors, array('id'=>$this->count, 'message'=>$message));
  77. }
  78. }
  79. /**
  80. * Validate if a string is smaller than length or not.
  81. * @param string $param
  82. * @param int $length
  83. * @param string $message
  84. */
  85. function minLength($param, $length, $message){
  86. $this->count++;
  87. if(strlen($param) < $length) {
  88. $this->isValid = false;
  89. array_push($this->errors, array('id'=>$this->count, 'message'=>$message));
  90. }
  91. }
  92. /**
  93. * Validate if a string is bigger than length or not.
  94. * @param string $param
  95. * @param int $length
  96. * @param string $message
  97. */
  98. function maxLength($param, $length, $message){
  99. $this->count++;
  100. if(strlen($param) > $length) {
  101. $this->isValid = false;
  102. array_push($this->errors, array('id'=>$this->count, 'message'=>$message));
  103. }
  104. }
  105. function dateRange($param1, $param2, $message){
  106. $this->count++;
  107. }
  108. function numericRange($param, $max, $min, $message){
  109. $this->count++;
  110. if($param > $max || $param < $min) {
  111. $this->isValid = false;
  112. array_push($this->errors, array('id'=>$this->count, 'message'=>$message));
  113. }
  114. }
  115. /**
  116. * Check if email is RFC 2822 Compliant
  117. * @param type $param
  118. * @param type $message
  119. */
  120. function email($param, $message){
  121. $this->count++;
  122. if(!empty($param) && !Email::validate($param)){
  123. $this->isValid = false;
  124. array_push($this->errors, array('id'=>$this->count, 'message'=>$message));
  125. }
  126. }
  127. /**
  128. * Checks if its a valid telephone number.
  129. * @param type $param
  130. * @param type $message
  131. */
  132. function telephone($param, $message){
  133. $this->count++;
  134. $match = '/^((\+)?(\d{2})[-])?(([\(])?((\d){3,5})([\)])?[-])|(\d{3,5})(\d{5,8}){1}?$/';
  135. $answer = preg_match($match, $param);
  136. if(!empty($param) && !$answer){
  137. $this->isValid = false;
  138. array_push($this->errors, array('id'=>$this->count, 'message'=>$message));
  139. }
  140. }
  141. }