PageRenderTime 44ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/www/libs/Utils/Is.php

https://bitbucket.org/Ppito/kawaiviewmodel2
PHP | 200 lines | 92 code | 15 blank | 93 comment | 33 complexity | 17121edebae3a2ed80f6714026993dea MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. /**
  3. * Validation
  4. */
  5. class Is {
  6. /**
  7. * Check is id
  8. *
  9. * @param int|string $value
  10. * @return boolean
  11. */
  12. public static function id($value) {
  13. return preg_match('#^\w+$#', $value);
  14. }
  15. /**
  16. * Check is md5
  17. *
  18. * @param string $value
  19. * @return boolean
  20. */
  21. public static function md5($value) {
  22. return preg_match('#^[0-9A-Fa-f]+$#', $value) && strlen($value) == 32;
  23. }
  24. /**
  25. * Check is integer
  26. *
  27. * @param int|string $value
  28. * @return boolean
  29. */
  30. public static function integer($value) {
  31. return preg_match('#^-?[0-9]+$#', $value);
  32. }
  33. /**
  34. * Check is string
  35. *
  36. * @param string $value
  37. * @return boolean
  38. */
  39. public static function string($value) {
  40. return is_string($value);
  41. }
  42. /**
  43. * Check is boolean
  44. *
  45. * @param string|boolean $value
  46. * @return boolean
  47. */
  48. public static function bool($value) {
  49. return preg_match('#^[01]$#', $value);
  50. }
  51. /**
  52. * Check is decimal
  53. *
  54. * @param string $value
  55. * @return boolean
  56. */
  57. public static function decimal($value) {
  58. return preg_match('#^-?(?:[0-9]+[\.,]?[0-9]*|[0-9]*[\.,]?[0-9]+)$#', $value);
  59. }
  60. /**
  61. * Check is pair
  62. *
  63. * @param int|string $value
  64. * @return boolean
  65. */
  66. public static function pair($value) {
  67. return (!($value % 2));
  68. }
  69. /**
  70. * Check is numeric
  71. *
  72. * @param int $value
  73. * @return boolean
  74. */
  75. public static function alphaNumerique($value) {
  76. return preg_match('#^\w*$#', $value);
  77. }
  78. /**
  79. * Check is blank
  80. *
  81. * @param int $value
  82. * @return boolean
  83. */
  84. public static function blank($value) {
  85. return (trim($value) == "");
  86. }
  87. /**
  88. * Check is date format dd/mm/yyyy
  89. *
  90. * @param string $value
  91. * @return boolean
  92. */
  93. public static function date($value) {
  94. $resultat = array();
  95. $resultat = preg_split('|[/.-]|', $value);
  96. if (count($resultat) == 3) {
  97. $day = $resultat[0];
  98. $month = $resultat[1];
  99. $year = $resultat[2];
  100. if (Is::integer($day) && Is::integer($month) && Is::integer($year)) {
  101. if (strlen($year) == 2)
  102. $year = '20' . $year;
  103. return checkDate($month, $day, $year);
  104. }
  105. }
  106. return false;
  107. }
  108. /**
  109. * Check is date format yyyy/mm/dd
  110. *
  111. * @param string $value
  112. * @return boolean
  113. */
  114. public static function dateUk($value) {
  115. $resultat = array();
  116. $resultat = preg_split('|[/.-]|', $value);
  117. if (count($resultat) == 3) {
  118. $year = $resultat[0];
  119. $month = $resultat[1];
  120. $day = $resultat[2];
  121. if (Is::integer($day) && Is::integer($month) && Is::integer($year)) {
  122. if (strlen($year) == 2)
  123. $year = '20' . $year;
  124. return checkDate($month, $day, $year);
  125. }
  126. }
  127. return false;
  128. }
  129. /**
  130. * Check is date time format yyyy/mm/dd H:i:s
  131. *
  132. * @param string $value
  133. * @return boolean
  134. */
  135. public static function dateTime($value) {
  136. $resultat = array();
  137. $resultat = preg_split('|\ |', $value);
  138. if (count($resultat) == 2) {
  139. if (!Is::dateUk($resultat[0])) {
  140. return false;
  141. }
  142. $matches = array();
  143. if (preg_match('#(\d{2})\:(\d{2})\:(\d{2})#', $resultat[1], $matches)) {
  144. if (!isset($matches[0]) || $matches[0] > 23 || $matches[0] < 0)
  145. return false;
  146. if (!isset($matches[1]) || $matches[1] > 59 || $matches[1] < 0)
  147. return false;
  148. if (!isset($matches[2]) || $matches[2] > 59 || $matches[2] < 0)
  149. return false;
  150. return true;
  151. }
  152. } else if (count($resultat) == 1) {
  153. return Is::dateUk($resultat[0]);
  154. }
  155. return false;
  156. }
  157. /**
  158. * Check is mail
  159. *
  160. * @param string $value
  161. * @return boolean
  162. */
  163. public static function mail($value) {
  164. return filter_var($value, FILTER_VALIDATE_EMAIL);
  165. }
  166. /**
  167. * Check is europeen phone number
  168. *
  169. * @param string $value
  170. * @return boolean
  171. */
  172. public static function phone($value) {
  173. return preg_match('#^(\+){0,1}(\d|\s|\(|\)){10,20}$#i', $value);
  174. }
  175. /**
  176. * Check is an hour
  177. *
  178. * @param string $value
  179. * @return boolean
  180. */
  181. public static function hour($value) {
  182. return preg_match('#(?:0+[0-9]|1[0-9]|2[0-3]):(?:[0-5][0-9])#', $value);
  183. }
  184. }
  185. ?>