PageRenderTime 50ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/libraries/ManiaLive/Utilities/Validation.php

http://manialive.googlecode.com/
PHP | 171 lines | 94 code | 21 blank | 56 comment | 12 complexity | a5216b07a5a7f731f6d5d0ad28e312ee MD5 | raw file
Possible License(s): LGPL-3.0
  1. <?php
  2. /**
  3. * @copyright NADEO (c) 2011
  4. */
  5. namespace ManiaLive\Utilities;
  6. // FIXME When throwing exception in Validation, get the last element of the stack trace to kown where it comes from
  7. abstract class Validation
  8. {
  9. /**
  10. * Check if the data is a boolean
  11. * Returns TRUE for "1", "true", "on" and "yes".
  12. * FALSE is returned only for "0", "false", "off", "no", and "".
  13. * NULL is returned for all non-boolean values.
  14. * @param mixed $data
  15. * @return array[bool]|bool
  16. */
  17. static function bool($data)
  18. {
  19. self::validate($data, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE);
  20. }
  21. /**
  22. * Validates value as e-mail.
  23. * @param array|mixed $data
  24. * @return array[bool]|bool
  25. */
  26. static function email($data)
  27. {
  28. self::validate($data, FILTER_VALIDATE_EMAIL);
  29. }
  30. /**
  31. * Validates value as float.
  32. * @param array|mixed $data
  33. * @return array[bool]|bool
  34. */
  35. static function float($data)
  36. {
  37. self::validate($data, FILTER_VALIDATE_FLOAT);
  38. }
  39. /**
  40. * Validates value as an integer.
  41. * @param array|mixed $data
  42. * @param int $minRange optionnal parameter to set a minimal value
  43. * @param int $maxRange optionnal parameter to set a maximal value
  44. * @param bool optionnal parameter to define if octal values are valid
  45. * @param bool optionnal parameter to define if hexadecimal values are valid
  46. * @return array[bool]|bool
  47. */
  48. static function int($data, $minRange = null, $maxRange = null, $allowOctal = false, $allowHexa = false)
  49. {
  50. $options = array();
  51. if($minRange !== null)
  52. {
  53. $options['options'] = array();
  54. $options['options']['min_range'] = (int)$minRange;
  55. }
  56. if($maxRange !== null)
  57. {
  58. if(!isset($options['options']))
  59. {
  60. $options['options'] = array();
  61. }
  62. $options['options']['max_range'] = (int)$maxRange;
  63. }
  64. $options['flags'] = ($allowOctal ? FILTER_FLAG_ALLOW_OCTAL : 0 );
  65. if($allowHexa)
  66. {
  67. $options['flags'] = $options['flags'] | FILTER_FLAG_ALLOW_HEX;
  68. }
  69. if(!count($options))
  70. {
  71. $options = null;
  72. }
  73. self::validate($data, FILTER_VALIDATE_INT, $options);
  74. }
  75. /**
  76. * Validates value as IP address, optionally only IPv4 or IPv6 or not from private or reserved ranges.
  77. * @param array|mixed $data
  78. * @return array[bool]|bool
  79. */
  80. static function ip($data, $allowIpv4 = true, $allowIpv6 = true, $allowPrivate = true, $allowReserved = false)
  81. {
  82. $options = ($allowIpv4 ? FILTER_FLAG_IPV4 : ~FILTER_FLAG_IPV4);
  83. if ($allowIpv6)
  84. {
  85. $options = $options & FILTER_FLAG_IPV6;
  86. }
  87. if (!$allowPrivate)
  88. {
  89. $options = $options | FILTER_FLAG_NO_PRIV_RANGE;
  90. }
  91. if (!$allowReserved)
  92. {
  93. $options = $options | FILTER_FLAG_NO_RES_RANGE;
  94. }
  95. self::validate($data, FILTER_VALIDATE_IP, $options);
  96. }
  97. /**
  98. * Validates data against regexp, a Perl-compatible regular expression.
  99. * @param array|mixed $data
  100. * @return array[bool]|bool
  101. */
  102. static function regularExpression($data)
  103. {
  104. self::validate($data, FILTER_VALIDATE_REGEXP);
  105. }
  106. /**
  107. * Validates value as URL (according to Â? http://www.faqs.org/rfcs/rfc2396),
  108. * optionally with required components. Note that the function will only find
  109. * ASCII URLs to be valid; internationalized domain names
  110. * (containing non-ASCII characters) will fail.
  111. * @param array|mixed $data
  112. * @return array[bool]|bool
  113. */
  114. static function url($data, $requirePath = false, $requireQuery = false)
  115. {
  116. $options = ($requirePath ? FILTER_FLAG_PATH_REQUIRED : 0);
  117. if ($requireQuery)
  118. {
  119. $options = $options | FILTER_FLAG_QUERY_REQUIRED;
  120. }
  121. self::validate($data, FILTER_VALIDATE_URL, $options);
  122. }
  123. /**
  124. * validate data
  125. * @param array|string $data
  126. * @param int $filter
  127. * @param mixed $options
  128. * @return array|string the sanitized data
  129. */
  130. static function validate($data, $filter = FILTER_DEFAULT, $options = null)
  131. {
  132. if(is_array($data))
  133. {
  134. foreach ($data as $key => $value)
  135. {
  136. if(filter_var($value, $filter, $options) === false)
  137. {
  138. throw new \InvalidArgumentException();
  139. }
  140. }
  141. }
  142. else
  143. {
  144. if(filter_var($data, $filter, $options) === false)
  145. {
  146. throw new \InvalidArgumentException();
  147. }
  148. }
  149. }
  150. }