/core/Base/Assert.class.php

https://github.com/bassta/onphp-framework · PHP · 325 lines · 269 code · 39 blank · 17 comment · 46 complexity · 8cc9d6143906ab7da6f4d1af3b701d6f MD5 · raw file

  1. <?php
  2. /***************************************************************************
  3. * Copyright (C) 2005-2008 by Konstantin V. Arkhipov *
  4. * *
  5. * This program is free software; you can redistribute it and/or modify *
  6. * it under the terms of the GNU Lesser General Public License as *
  7. * published by the Free Software Foundation; either version 3 of the *
  8. * License, or (at your option) any later version. *
  9. * *
  10. ***************************************************************************/
  11. /**
  12. * Widely used assertions.
  13. *
  14. * @ingroup Base
  15. **/
  16. final class Assert extends StaticFactory
  17. {
  18. public static function isTrue($boolean, $message = null)
  19. {
  20. if ($boolean !== true)
  21. throw new WrongArgumentException(
  22. $message.', '.self::dumpArgument($boolean)
  23. );
  24. }
  25. public static function isFalse($boolean, $message = null)
  26. {
  27. if ($boolean !== false)
  28. throw new WrongArgumentException(
  29. $message.', '.self::dumpArgument($boolean)
  30. );
  31. }
  32. public static function isNotFalse($boolean, $message = null)
  33. {
  34. if ($boolean === false)
  35. throw new WrongArgumentException(
  36. $message.', '.self::dumpArgument($boolean)
  37. );
  38. }
  39. public static function isNull($variable, $message = null)
  40. {
  41. if ($variable !== null)
  42. throw new WrongArgumentException(
  43. $message.', '.self::dumpArgument($variable)
  44. );
  45. }
  46. public static function isEmpty($variable, $message = null)
  47. {
  48. if (!empty($variable))
  49. throw new WrongArgumentException(
  50. $message.', '.self::dumpArgument($variable)
  51. );
  52. }
  53. public static function isNotEmpty($variable, $message = null)
  54. {
  55. if (empty($variable))
  56. throw new WrongArgumentException(
  57. $message.', '.self::dumpArgument($variable)
  58. );
  59. }
  60. public static function isIndexExists($array, $key, $message = null)
  61. {
  62. Assert::isArray($array);
  63. if (!array_key_exists($key, $array))
  64. throw new WrongArgumentException(
  65. $message.', '.self::dumpArgument($key)
  66. );
  67. }
  68. public static function isNotNull($variable, $message = null)
  69. {
  70. if ($variable === null)
  71. throw new WrongArgumentException($message);
  72. }
  73. public static function isScalar($variable, $message = null)
  74. {
  75. if (!is_scalar($variable))
  76. throw new WrongArgumentException(
  77. $message.', '.self::dumpArgument($variable)
  78. );
  79. }
  80. public static function isArray($variable, $message = null)
  81. {
  82. if (!is_array($variable))
  83. throw new WrongArgumentException(
  84. $message.', '.self::dumpArgument($variable)
  85. );
  86. }
  87. public static function isNotEmptyArray(&$variable, $message = null)
  88. {
  89. self::isArray($variable, $message);
  90. if (!$variable)
  91. throw new WrongArgumentException(
  92. $message.', '.self::dumpArgument($variable)
  93. );
  94. }
  95. public static function isInteger($variable, $message = null)
  96. {
  97. if (
  98. !(
  99. is_numeric($variable)
  100. && $variable == (int) $variable
  101. )
  102. )
  103. throw new WrongArgumentException(
  104. $message.', '.self::dumpArgument($variable)
  105. );
  106. }
  107. public static function isPositiveInteger($variable, $message = null)
  108. {
  109. if (
  110. !self::checkInteger($variable)
  111. || $variable < 0
  112. )
  113. throw new WrongArgumentException(
  114. $message.', '.self::dumpArgument($variable)
  115. );
  116. }
  117. public static function isFloat($variable, $message = null)
  118. {
  119. if (!self::checkFloat($variable))
  120. throw new WrongArgumentException(
  121. $message.', '.self::dumpArgument($variable)
  122. );
  123. }
  124. public static function isString($variable, $message = null)
  125. {
  126. if (!is_string($variable))
  127. throw new WrongArgumentException(
  128. $message.', '.self::dumpArgument($variable)
  129. );
  130. }
  131. public static function isBoolean($variable, $message = null)
  132. {
  133. if (!($variable === true || $variable === false))
  134. throw new WrongArgumentException(
  135. $message.', '.self::dumpArgument($variable)
  136. );
  137. }
  138. public static function isTernaryBase($variable, $message = null)
  139. {
  140. if (
  141. !(
  142. ($variable === true)
  143. || ($variable === false)
  144. || ($variable === null)
  145. )
  146. )
  147. throw new WrongArgumentException(
  148. $message.', '.self::dumpArgument($variable)
  149. );
  150. }
  151. public static function brothers($first, $second, $message = null)
  152. {
  153. if (get_class($first) !== get_class($second))
  154. throw new WrongArgumentException(
  155. $message.', '.self::dumpOppositeArguments($first, $second)
  156. );
  157. }
  158. public static function isEqual($first, $second, $message = null)
  159. {
  160. if ($first != $second)
  161. throw new WrongArgumentException(
  162. $message.', '.self::dumpOppositeArguments($first, $second)
  163. );
  164. }
  165. public static function isNotEqual($first, $second, $message = null)
  166. {
  167. if ($first == $second)
  168. throw new WrongArgumentException(
  169. $message.', '.self::dumpOppositeArguments($first, $second)
  170. );
  171. }
  172. public static function isSame($first, $second, $message = null)
  173. {
  174. if ($first !== $second)
  175. throw new WrongArgumentException(
  176. $message.', '.self::dumpOppositeArguments($first, $second)
  177. );
  178. }
  179. public static function isNotSame($first, $second, $message = null)
  180. {
  181. if ($first === $second)
  182. throw new WrongArgumentException(
  183. $message.', '.self::dumpOppositeArguments($first, $second)
  184. );
  185. }
  186. public static function isTypelessEqual($first, $second, $message = null)
  187. {
  188. if ($first != $second)
  189. throw new WrongArgumentException(
  190. $message.', '.self::dumpOppositeArguments($first, $second)
  191. );
  192. }
  193. public static function isLesser($first, $second, $message = null)
  194. {
  195. if (!($first < $second))
  196. throw new WrongArgumentException(
  197. $message.', '.self::dumpOppositeArguments($first, $second)
  198. );
  199. }
  200. public static function isGreater($first, $second, $message = null)
  201. {
  202. if (!($first > $second))
  203. throw new WrongArgumentException(
  204. $message.', '.self::dumpOppositeArguments($first, $second)
  205. );
  206. }
  207. public static function isLesserOrEqual($first, $second, $message = null)
  208. {
  209. if (!($first <= $second))
  210. throw new WrongArgumentException(
  211. $message.', '.self::dumpOppositeArguments($first, $second)
  212. );
  213. }
  214. public static function isGreaterOrEqual($first, $second, $message = null)
  215. {
  216. if (!($first >= $second))
  217. throw new WrongArgumentException(
  218. $message.', '.self::dumpOppositeArguments($first, $second)
  219. );
  220. }
  221. public static function isInstance($first, $second, $message = null)
  222. {
  223. if (!ClassUtils::isInstanceOf($first, $second))
  224. throw new WrongArgumentException(
  225. $message.', '.self::dumpOppositeArguments($first, $second)
  226. );
  227. }
  228. public static function classExists($className, $message = null)
  229. {
  230. if (!class_exists($className, true))
  231. throw new WrongArgumentException(
  232. $message.', class "'.$className.'" does not exists'
  233. );
  234. }
  235. public static function methodExists($object, $method, $message = null)
  236. {
  237. if (!method_exists($object, $method))
  238. throw new WrongArgumentException(
  239. $message.', method "'.get_class($object).'::'.$method.'()" does not exists'
  240. );
  241. }
  242. public static function isUnreachable($message = 'unreachable code reached')
  243. {
  244. throw new WrongArgumentException($message);
  245. }
  246. public static function isObject($object, $message = null)
  247. {
  248. if (!is_object($object))
  249. throw new WrongArgumentException(
  250. $message.' not object given'
  251. );
  252. }
  253. /// exceptionless methods
  254. //@{
  255. public static function checkInteger($value)
  256. {
  257. return (
  258. is_numeric($value)
  259. && ($value == (int) $value)
  260. && (strlen($value) == strlen((int) $value))
  261. );
  262. }
  263. public static function checkFloat($value)
  264. {
  265. return (
  266. is_numeric($value)
  267. && ($value == (float) $value)
  268. );
  269. }
  270. public static function checkScalar($value)
  271. {
  272. return is_scalar($value);
  273. }
  274. public static function dumpArgument($argument)
  275. {
  276. return 'argument: ['.print_r($argument, true).']';
  277. }
  278. public static function dumpOppositeArguments($first, $second)
  279. {
  280. return
  281. 'arguments: ['.print_r($first, true).'] '
  282. .'vs. ['.print_r($second, true).'] ';
  283. }
  284. //@}
  285. }
  286. ?>