Web Interfaces of PROSUME

Assert.php 40KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187
  1. <?php
  2. /*
  3. * This file is part of the webmozart/assert package.
  4. *
  5. * (c) Bernhard Schussek <bschussek@gmail.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Webmozart\Assert;
  11. use ArrayAccess;
  12. use BadMethodCallException;
  13. use Closure;
  14. use Countable;
  15. use Exception;
  16. use InvalidArgumentException;
  17. use Throwable;
  18. use Traversable;
  19. /**
  20. * Efficient assertions to validate the input/output of your methods.
  21. *
  22. * @method static void nullOrString($value, $message = '')
  23. * @method static void nullOrStringNotEmpty($value, $message = '')
  24. * @method static void nullOrInteger($value, $message = '')
  25. * @method static void nullOrIntegerish($value, $message = '')
  26. * @method static void nullOrFloat($value, $message = '')
  27. * @method static void nullOrNumeric($value, $message = '')
  28. * @method static void nullOrNatural($value, $message = '')
  29. * @method static void nullOrBoolean($value, $message = '')
  30. * @method static void nullOrScalar($value, $message = '')
  31. * @method static void nullOrObject($value, $message = '')
  32. * @method static void nullOrResource($value, $type = null, $message = '')
  33. * @method static void nullOrIsCallable($value, $message = '')
  34. * @method static void nullOrIsArray($value, $message = '')
  35. * @method static void nullOrIsTraversable($value, $message = '')
  36. * @method static void nullOrIsArrayAccessible($value, $message = '')
  37. * @method static void nullOrIsCountable($value, $message = '')
  38. * @method static void nullOrIsIterable($value, $message = '')
  39. * @method static void nullOrIsInstanceOf($value, $class, $message = '')
  40. * @method static void nullOrNotInstanceOf($value, $class, $message = '')
  41. * @method static void nullOrIsInstanceOfAny($value, $classes, $message = '')
  42. * @method static void nullOrIsEmpty($value, $message = '')
  43. * @method static void nullOrNotEmpty($value, $message = '')
  44. * @method static void nullOrTrue($value, $message = '')
  45. * @method static void nullOrFalse($value, $message = '')
  46. * @method static void nullOrIp($value, $message = '')
  47. * @method static void nullOrIpv4($value, $message = '')
  48. * @method static void nullOrIpv6($value, $message = '')
  49. * @method static void nullOrEq($value, $value2, $message = '')
  50. * @method static void nullOrNotEq($value,$value2, $message = '')
  51. * @method static void nullOrSame($value, $value2, $message = '')
  52. * @method static void nullOrNotSame($value, $value2, $message = '')
  53. * @method static void nullOrGreaterThan($value, $value2, $message = '')
  54. * @method static void nullOrGreaterThanEq($value, $value2, $message = '')
  55. * @method static void nullOrLessThan($value, $value2, $message = '')
  56. * @method static void nullOrLessThanEq($value, $value2, $message = '')
  57. * @method static void nullOrRange($value, $min, $max, $message = '')
  58. * @method static void nullOrOneOf($value, $values, $message = '')
  59. * @method static void nullOrContains($value, $subString, $message = '')
  60. * @method static void nullOrNotContains($value, $subString, $message = '')
  61. * @method static void nullOrNotWhitespaceOnly($value, $message = '')
  62. * @method static void nullOrStartsWith($value, $prefix, $message = '')
  63. * @method static void nullOrStartsWithLetter($value, $message = '')
  64. * @method static void nullOrEndsWith($value, $suffix, $message = '')
  65. * @method static void nullOrRegex($value, $pattern, $message = '')
  66. * @method static void nullOrNotRegex($value, $pattern, $message = '')
  67. * @method static void nullOrAlpha($value, $message = '')
  68. * @method static void nullOrDigits($value, $message = '')
  69. * @method static void nullOrAlnum($value, $message = '')
  70. * @method static void nullOrLower($value, $message = '')
  71. * @method static void nullOrUpper($value, $message = '')
  72. * @method static void nullOrLength($value, $length, $message = '')
  73. * @method static void nullOrMinLength($value, $min, $message = '')
  74. * @method static void nullOrMaxLength($value, $max, $message = '')
  75. * @method static void nullOrLengthBetween($value, $min, $max, $message = '')
  76. * @method static void nullOrFileExists($value, $message = '')
  77. * @method static void nullOrFile($value, $message = '')
  78. * @method static void nullOrDirectory($value, $message = '')
  79. * @method static void nullOrReadable($value, $message = '')
  80. * @method static void nullOrWritable($value, $message = '')
  81. * @method static void nullOrClassExists($value, $message = '')
  82. * @method static void nullOrSubclassOf($value, $class, $message = '')
  83. * @method static void nullOrInterfaceExists($value, $message = '')
  84. * @method static void nullOrImplementsInterface($value, $interface, $message = '')
  85. * @method static void nullOrPropertyExists($value, $property, $message = '')
  86. * @method static void nullOrPropertyNotExists($value, $property, $message = '')
  87. * @method static void nullOrMethodExists($value, $method, $message = '')
  88. * @method static void nullOrMethodNotExists($value, $method, $message = '')
  89. * @method static void nullOrKeyExists($value, $key, $message = '')
  90. * @method static void nullOrKeyNotExists($value, $key, $message = '')
  91. * @method static void nullOrCount($value, $key, $message = '')
  92. * @method static void nullOrMinCount($value, $min, $message = '')
  93. * @method static void nullOrMaxCount($value, $max, $message = '')
  94. * @method static void nullOrIsList($value, $message = '')
  95. * @method static void nullOrIsMap($value, $message = '')
  96. * @method static void nullOrCountBetween($value, $min, $max, $message = '')
  97. * @method static void nullOrUuid($values, $message = '')
  98. * @method static void nullOrThrows($expression, $class = 'Exception', $message = '')
  99. * @method static void allString($values, $message = '')
  100. * @method static void allStringNotEmpty($values, $message = '')
  101. * @method static void allInteger($values, $message = '')
  102. * @method static void allIntegerish($values, $message = '')
  103. * @method static void allFloat($values, $message = '')
  104. * @method static void allNumeric($values, $message = '')
  105. * @method static void allNatural($values, $message = '')
  106. * @method static void allBoolean($values, $message = '')
  107. * @method static void allScalar($values, $message = '')
  108. * @method static void allObject($values, $message = '')
  109. * @method static void allResource($values, $type = null, $message = '')
  110. * @method static void allIsCallable($values, $message = '')
  111. * @method static void allIsArray($values, $message = '')
  112. * @method static void allIsTraversable($values, $message = '')
  113. * @method static void allIsArrayAccessible($values, $message = '')
  114. * @method static void allIsCountable($values, $message = '')
  115. * @method static void allIsIterable($values, $message = '')
  116. * @method static void allIsInstanceOf($values, $class, $message = '')
  117. * @method static void allNotInstanceOf($values, $class, $message = '')
  118. * @method static void allIsInstanceOfAny($values, $classes, $message = '')
  119. * @method static void allNull($values, $message = '')
  120. * @method static void allNotNull($values, $message = '')
  121. * @method static void allIsEmpty($values, $message = '')
  122. * @method static void allNotEmpty($values, $message = '')
  123. * @method static void allTrue($values, $message = '')
  124. * @method static void allFalse($values, $message = '')
  125. * @method static void allIp($values, $message = '')
  126. * @method static void allIpv4($values, $message = '')
  127. * @method static void allIpv6($values, $message = '')
  128. * @method static void allEq($values, $value2, $message = '')
  129. * @method static void allNotEq($values,$value2, $message = '')
  130. * @method static void allSame($values, $value2, $message = '')
  131. * @method static void allNotSame($values, $value2, $message = '')
  132. * @method static void allGreaterThan($values, $value2, $message = '')
  133. * @method static void allGreaterThanEq($values, $value2, $message = '')
  134. * @method static void allLessThan($values, $value2, $message = '')
  135. * @method static void allLessThanEq($values, $value2, $message = '')
  136. * @method static void allRange($values, $min, $max, $message = '')
  137. * @method static void allOneOf($values, $values, $message = '')
  138. * @method static void allContains($values, $subString, $message = '')
  139. * @method static void allNotContains($values, $subString, $message = '')
  140. * @method static void allNotWhitespaceOnly($values, $message = '')
  141. * @method static void allStartsWith($values, $prefix, $message = '')
  142. * @method static void allStartsWithLetter($values, $message = '')
  143. * @method static void allEndsWith($values, $suffix, $message = '')
  144. * @method static void allRegex($values, $pattern, $message = '')
  145. * @method static void allNotRegex($values, $pattern, $message = '')
  146. * @method static void allAlpha($values, $message = '')
  147. * @method static void allDigits($values, $message = '')
  148. * @method static void allAlnum($values, $message = '')
  149. * @method static void allLower($values, $message = '')
  150. * @method static void allUpper($values, $message = '')
  151. * @method static void allLength($values, $length, $message = '')
  152. * @method static void allMinLength($values, $min, $message = '')
  153. * @method static void allMaxLength($values, $max, $message = '')
  154. * @method static void allLengthBetween($values, $min, $max, $message = '')
  155. * @method static void allFileExists($values, $message = '')
  156. * @method static void allFile($values, $message = '')
  157. * @method static void allDirectory($values, $message = '')
  158. * @method static void allReadable($values, $message = '')
  159. * @method static void allWritable($values, $message = '')
  160. * @method static void allClassExists($values, $message = '')
  161. * @method static void allSubclassOf($values, $class, $message = '')
  162. * @method static void allInterfaceExists($values, $message = '')
  163. * @method static void allImplementsInterface($values, $interface, $message = '')
  164. * @method static void allPropertyExists($values, $property, $message = '')
  165. * @method static void allPropertyNotExists($values, $property, $message = '')
  166. * @method static void allMethodExists($values, $method, $message = '')
  167. * @method static void allMethodNotExists($values, $method, $message = '')
  168. * @method static void allKeyExists($values, $key, $message = '')
  169. * @method static void allKeyNotExists($values, $key, $message = '')
  170. * @method static void allCount($values, $key, $message = '')
  171. * @method static void allMinCount($values, $min, $message = '')
  172. * @method static void allMaxCount($values, $max, $message = '')
  173. * @method static void allCountBetween($values, $min, $max, $message = '')
  174. * @method static void allIsList($values, $message = '')
  175. * @method static void allIsMap($values, $message = '')
  176. * @method static void allUuid($values, $message = '')
  177. * @method static void allThrows($expressions, $class = 'Exception', $message = '')
  178. *
  179. * @since 1.0
  180. *
  181. * @author Bernhard Schussek <bschussek@gmail.com>
  182. */
  183. class Assert
  184. {
  185. public static function string($value, $message = '')
  186. {
  187. if (!is_string($value)) {
  188. static::reportInvalidArgument(sprintf(
  189. $message ?: 'Expected a string. Got: %s',
  190. static::typeToString($value)
  191. ));
  192. }
  193. }
  194. public static function stringNotEmpty($value, $message = '')
  195. {
  196. static::string($value, $message);
  197. static::notEq($value, '', $message);
  198. }
  199. public static function integer($value, $message = '')
  200. {
  201. if (!is_int($value)) {
  202. static::reportInvalidArgument(sprintf(
  203. $message ?: 'Expected an integer. Got: %s',
  204. static::typeToString($value)
  205. ));
  206. }
  207. }
  208. public static function integerish($value, $message = '')
  209. {
  210. if (!is_numeric($value) || $value != (int) $value) {
  211. static::reportInvalidArgument(sprintf(
  212. $message ?: 'Expected an integerish value. Got: %s',
  213. static::typeToString($value)
  214. ));
  215. }
  216. }
  217. public static function float($value, $message = '')
  218. {
  219. if (!is_float($value)) {
  220. static::reportInvalidArgument(sprintf(
  221. $message ?: 'Expected a float. Got: %s',
  222. static::typeToString($value)
  223. ));
  224. }
  225. }
  226. public static function numeric($value, $message = '')
  227. {
  228. if (!is_numeric($value)) {
  229. static::reportInvalidArgument(sprintf(
  230. $message ?: 'Expected a numeric. Got: %s',
  231. static::typeToString($value)
  232. ));
  233. }
  234. }
  235. public static function natural($value, $message = '')
  236. {
  237. if (!is_int($value) || $value < 0) {
  238. static::reportInvalidArgument(sprintf(
  239. $message ?: 'Expected a non-negative integer. Got %s',
  240. static::valueToString($value)
  241. ));
  242. }
  243. }
  244. public static function boolean($value, $message = '')
  245. {
  246. if (!is_bool($value)) {
  247. static::reportInvalidArgument(sprintf(
  248. $message ?: 'Expected a boolean. Got: %s',
  249. static::typeToString($value)
  250. ));
  251. }
  252. }
  253. public static function scalar($value, $message = '')
  254. {
  255. if (!is_scalar($value)) {
  256. static::reportInvalidArgument(sprintf(
  257. $message ?: 'Expected a scalar. Got: %s',
  258. static::typeToString($value)
  259. ));
  260. }
  261. }
  262. public static function object($value, $message = '')
  263. {
  264. if (!is_object($value)) {
  265. static::reportInvalidArgument(sprintf(
  266. $message ?: 'Expected an object. Got: %s',
  267. static::typeToString($value)
  268. ));
  269. }
  270. }
  271. public static function resource($value, $type = null, $message = '')
  272. {
  273. if (!is_resource($value)) {
  274. static::reportInvalidArgument(sprintf(
  275. $message ?: 'Expected a resource. Got: %s',
  276. static::typeToString($value)
  277. ));
  278. }
  279. if ($type && $type !== get_resource_type($value)) {
  280. static::reportInvalidArgument(sprintf(
  281. $message ?: 'Expected a resource of type %2$s. Got: %s',
  282. static::typeToString($value),
  283. $type
  284. ));
  285. }
  286. }
  287. public static function isCallable($value, $message = '')
  288. {
  289. if (!is_callable($value)) {
  290. static::reportInvalidArgument(sprintf(
  291. $message ?: 'Expected a callable. Got: %s',
  292. static::typeToString($value)
  293. ));
  294. }
  295. }
  296. public static function isArray($value, $message = '')
  297. {
  298. if (!is_array($value)) {
  299. static::reportInvalidArgument(sprintf(
  300. $message ?: 'Expected an array. Got: %s',
  301. static::typeToString($value)
  302. ));
  303. }
  304. }
  305. public static function isTraversable($value, $message = '')
  306. {
  307. @trigger_error(
  308. sprintf(
  309. 'The "%s" assertion is deprecated. You should stop using it, as it will soon be removed in 2.0 version. Use "isIterable" or "isInstanceOf" instead.',
  310. __METHOD__
  311. ),
  312. E_USER_DEPRECATED
  313. );
  314. if (!is_array($value) && !($value instanceof Traversable)) {
  315. static::reportInvalidArgument(sprintf(
  316. $message ?: 'Expected a traversable. Got: %s',
  317. static::typeToString($value)
  318. ));
  319. }
  320. }
  321. public static function isArrayAccessible($value, $message = '')
  322. {
  323. if (!is_array($value) && !($value instanceof ArrayAccess)) {
  324. static::reportInvalidArgument(sprintf(
  325. $message ?: 'Expected an array accessible. Got: %s',
  326. static::typeToString($value)
  327. ));
  328. }
  329. }
  330. public static function isCountable($value, $message = '')
  331. {
  332. if (!is_array($value) && !($value instanceof Countable)) {
  333. static::reportInvalidArgument(sprintf(
  334. $message ?: 'Expected a countable. Got: %s',
  335. static::typeToString($value)
  336. ));
  337. }
  338. }
  339. public static function isIterable($value, $message = '')
  340. {
  341. if (!is_array($value) && !($value instanceof Traversable)) {
  342. static::reportInvalidArgument(sprintf(
  343. $message ?: 'Expected an iterable. Got: %s',
  344. static::typeToString($value)
  345. ));
  346. }
  347. }
  348. public static function isInstanceOf($value, $class, $message = '')
  349. {
  350. if (!($value instanceof $class)) {
  351. static::reportInvalidArgument(sprintf(
  352. $message ?: 'Expected an instance of %2$s. Got: %s',
  353. static::typeToString($value),
  354. $class
  355. ));
  356. }
  357. }
  358. public static function notInstanceOf($value, $class, $message = '')
  359. {
  360. if ($value instanceof $class) {
  361. static::reportInvalidArgument(sprintf(
  362. $message ?: 'Expected an instance other than %2$s. Got: %s',
  363. static::typeToString($value),
  364. $class
  365. ));
  366. }
  367. }
  368. public static function isInstanceOfAny($value, array $classes, $message = '')
  369. {
  370. foreach ($classes as $class) {
  371. if ($value instanceof $class) {
  372. return;
  373. }
  374. }
  375. static::reportInvalidArgument(sprintf(
  376. $message ?: 'Expected an instance of any of %2$s. Got: %s',
  377. static::typeToString($value),
  378. implode(', ', array_map(array('static', 'valueToString'), $classes))
  379. ));
  380. }
  381. public static function isEmpty($value, $message = '')
  382. {
  383. if (!empty($value)) {
  384. static::reportInvalidArgument(sprintf(
  385. $message ?: 'Expected an empty value. Got: %s',
  386. static::valueToString($value)
  387. ));
  388. }
  389. }
  390. public static function notEmpty($value, $message = '')
  391. {
  392. if (empty($value)) {
  393. static::reportInvalidArgument(sprintf(
  394. $message ?: 'Expected a non-empty value. Got: %s',
  395. static::valueToString($value)
  396. ));
  397. }
  398. }
  399. public static function null($value, $message = '')
  400. {
  401. if (null !== $value) {
  402. static::reportInvalidArgument(sprintf(
  403. $message ?: 'Expected null. Got: %s',
  404. static::valueToString($value)
  405. ));
  406. }
  407. }
  408. public static function notNull($value, $message = '')
  409. {
  410. if (null === $value) {
  411. static::reportInvalidArgument(
  412. $message ?: 'Expected a value other than null.'
  413. );
  414. }
  415. }
  416. public static function true($value, $message = '')
  417. {
  418. if (true !== $value) {
  419. static::reportInvalidArgument(sprintf(
  420. $message ?: 'Expected a value to be true. Got: %s',
  421. static::valueToString($value)
  422. ));
  423. }
  424. }
  425. public static function false($value, $message = '')
  426. {
  427. if (false !== $value) {
  428. static::reportInvalidArgument(sprintf(
  429. $message ?: 'Expected a value to be false. Got: %s',
  430. static::valueToString($value)
  431. ));
  432. }
  433. }
  434. public static function ip($value, $message = '')
  435. {
  436. if (false === filter_var($value, FILTER_VALIDATE_IP)) {
  437. static::reportInvalidArgument(sprintf(
  438. $message ?: 'Expected a value to be an IP. Got: %s',
  439. static::valueToString($value)
  440. ));
  441. }
  442. }
  443. public static function ipv4($value, $message = '')
  444. {
  445. if (false === filter_var($value, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
  446. static::reportInvalidArgument(sprintf(
  447. $message ?: 'Expected a value to be an IPv4. Got: %s',
  448. static::valueToString($value)
  449. ));
  450. }
  451. }
  452. public static function ipv6($value, $message = '')
  453. {
  454. if (false === filter_var($value, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) {
  455. static::reportInvalidArgument(sprintf(
  456. $message ?: 'Expected a value to be an IPv6. Got %s',
  457. static::valueToString($value)
  458. ));
  459. }
  460. }
  461. public static function eq($value, $value2, $message = '')
  462. {
  463. if ($value2 != $value) {
  464. static::reportInvalidArgument(sprintf(
  465. $message ?: 'Expected a value equal to %2$s. Got: %s',
  466. static::valueToString($value),
  467. static::valueToString($value2)
  468. ));
  469. }
  470. }
  471. public static function notEq($value, $value2, $message = '')
  472. {
  473. if ($value2 == $value) {
  474. static::reportInvalidArgument(sprintf(
  475. $message ?: 'Expected a different value than %s.',
  476. static::valueToString($value2)
  477. ));
  478. }
  479. }
  480. public static function same($value, $value2, $message = '')
  481. {
  482. if ($value2 !== $value) {
  483. static::reportInvalidArgument(sprintf(
  484. $message ?: 'Expected a value identical to %2$s. Got: %s',
  485. static::valueToString($value),
  486. static::valueToString($value2)
  487. ));
  488. }
  489. }
  490. public static function notSame($value, $value2, $message = '')
  491. {
  492. if ($value2 === $value) {
  493. static::reportInvalidArgument(sprintf(
  494. $message ?: 'Expected a value not identical to %s.',
  495. static::valueToString($value2)
  496. ));
  497. }
  498. }
  499. public static function greaterThan($value, $limit, $message = '')
  500. {
  501. if ($value <= $limit) {
  502. static::reportInvalidArgument(sprintf(
  503. $message ?: 'Expected a value greater than %2$s. Got: %s',
  504. static::valueToString($value),
  505. static::valueToString($limit)
  506. ));
  507. }
  508. }
  509. public static function greaterThanEq($value, $limit, $message = '')
  510. {
  511. if ($value < $limit) {
  512. static::reportInvalidArgument(sprintf(
  513. $message ?: 'Expected a value greater than or equal to %2$s. Got: %s',
  514. static::valueToString($value),
  515. static::valueToString($limit)
  516. ));
  517. }
  518. }
  519. public static function lessThan($value, $limit, $message = '')
  520. {
  521. if ($value >= $limit) {
  522. static::reportInvalidArgument(sprintf(
  523. $message ?: 'Expected a value less than %2$s. Got: %s',
  524. static::valueToString($value),
  525. static::valueToString($limit)
  526. ));
  527. }
  528. }
  529. public static function lessThanEq($value, $limit, $message = '')
  530. {
  531. if ($value > $limit) {
  532. static::reportInvalidArgument(sprintf(
  533. $message ?: 'Expected a value less than or equal to %2$s. Got: %s',
  534. static::valueToString($value),
  535. static::valueToString($limit)
  536. ));
  537. }
  538. }
  539. public static function range($value, $min, $max, $message = '')
  540. {
  541. if ($value < $min || $value > $max) {
  542. static::reportInvalidArgument(sprintf(
  543. $message ?: 'Expected a value between %2$s and %3$s. Got: %s',
  544. static::valueToString($value),
  545. static::valueToString($min),
  546. static::valueToString($max)
  547. ));
  548. }
  549. }
  550. public static function oneOf($value, array $values, $message = '')
  551. {
  552. if (!in_array($value, $values, true)) {
  553. static::reportInvalidArgument(sprintf(
  554. $message ?: 'Expected one of: %2$s. Got: %s',
  555. static::valueToString($value),
  556. implode(', ', array_map(array('static', 'valueToString'), $values))
  557. ));
  558. }
  559. }
  560. public static function contains($value, $subString, $message = '')
  561. {
  562. if (false === strpos($value, $subString)) {
  563. static::reportInvalidArgument(sprintf(
  564. $message ?: 'Expected a value to contain %2$s. Got: %s',
  565. static::valueToString($value),
  566. static::valueToString($subString)
  567. ));
  568. }
  569. }
  570. public static function notContains($value, $subString, $message = '')
  571. {
  572. if (false !== strpos($value, $subString)) {
  573. static::reportInvalidArgument(sprintf(
  574. $message ?: '%2$s was not expected to be contained in a value. Got: %s',
  575. static::valueToString($value),
  576. static::valueToString($subString)
  577. ));
  578. }
  579. }
  580. public static function notWhitespaceOnly($value, $message = '')
  581. {
  582. if (preg_match('/^\s*$/', $value)) {
  583. static::reportInvalidArgument(sprintf(
  584. $message ?: 'Expected a non-whitespace string. Got: %s',
  585. static::valueToString($value)
  586. ));
  587. }
  588. }
  589. public static function startsWith($value, $prefix, $message = '')
  590. {
  591. if (0 !== strpos($value, $prefix)) {
  592. static::reportInvalidArgument(sprintf(
  593. $message ?: 'Expected a value to start with %2$s. Got: %s',
  594. static::valueToString($value),
  595. static::valueToString($prefix)
  596. ));
  597. }
  598. }
  599. public static function startsWithLetter($value, $message = '')
  600. {
  601. $valid = isset($value[0]);
  602. if ($valid) {
  603. $locale = setlocale(LC_CTYPE, 0);
  604. setlocale(LC_CTYPE, 'C');
  605. $valid = ctype_alpha($value[0]);
  606. setlocale(LC_CTYPE, $locale);
  607. }
  608. if (!$valid) {
  609. static::reportInvalidArgument(sprintf(
  610. $message ?: 'Expected a value to start with a letter. Got: %s',
  611. static::valueToString($value)
  612. ));
  613. }
  614. }
  615. public static function endsWith($value, $suffix, $message = '')
  616. {
  617. if ($suffix !== substr($value, -static::strlen($suffix))) {
  618. static::reportInvalidArgument(sprintf(
  619. $message ?: 'Expected a value to end with %2$s. Got: %s',
  620. static::valueToString($value),
  621. static::valueToString($suffix)
  622. ));
  623. }
  624. }
  625. public static function regex($value, $pattern, $message = '')
  626. {
  627. if (!preg_match($pattern, $value)) {
  628. static::reportInvalidArgument(sprintf(
  629. $message ?: 'The value %s does not match the expected pattern.',
  630. static::valueToString($value)
  631. ));
  632. }
  633. }
  634. public static function notRegex($value, $pattern, $message = '')
  635. {
  636. if (preg_match($pattern, $value, $matches, PREG_OFFSET_CAPTURE)) {
  637. static::reportInvalidArgument(sprintf(
  638. $message ?: 'The value %s matches the pattern %s (at offset %d).',
  639. static::valueToString($value),
  640. static::valueToString($pattern),
  641. $matches[0][1]
  642. ));
  643. }
  644. }
  645. public static function alpha($value, $message = '')
  646. {
  647. $locale = setlocale(LC_CTYPE, 0);
  648. setlocale(LC_CTYPE, 'C');
  649. $valid = !ctype_alpha($value);
  650. setlocale(LC_CTYPE, $locale);
  651. if ($valid) {
  652. static::reportInvalidArgument(sprintf(
  653. $message ?: 'Expected a value to contain only letters. Got: %s',
  654. static::valueToString($value)
  655. ));
  656. }
  657. }
  658. public static function digits($value, $message = '')
  659. {
  660. $locale = setlocale(LC_CTYPE, 0);
  661. setlocale(LC_CTYPE, 'C');
  662. $valid = !ctype_digit($value);
  663. setlocale(LC_CTYPE, $locale);
  664. if ($valid) {
  665. static::reportInvalidArgument(sprintf(
  666. $message ?: 'Expected a value to contain digits only. Got: %s',
  667. static::valueToString($value)
  668. ));
  669. }
  670. }
  671. public static function alnum($value, $message = '')
  672. {
  673. $locale = setlocale(LC_CTYPE, 0);
  674. setlocale(LC_CTYPE, 'C');
  675. $valid = !ctype_alnum($value);
  676. setlocale(LC_CTYPE, $locale);
  677. if ($valid) {
  678. static::reportInvalidArgument(sprintf(
  679. $message ?: 'Expected a value to contain letters and digits only. Got: %s',
  680. static::valueToString($value)
  681. ));
  682. }
  683. }
  684. public static function lower($value, $message = '')
  685. {
  686. $locale = setlocale(LC_CTYPE, 0);
  687. setlocale(LC_CTYPE, 'C');
  688. $valid = !ctype_lower($value);
  689. setlocale(LC_CTYPE, $locale);
  690. if ($valid) {
  691. static::reportInvalidArgument(sprintf(
  692. $message ?: 'Expected a value to contain lowercase characters only. Got: %s',
  693. static::valueToString($value)
  694. ));
  695. }
  696. }
  697. public static function upper($value, $message = '')
  698. {
  699. $locale = setlocale(LC_CTYPE, 0);
  700. setlocale(LC_CTYPE, 'C');
  701. $valid = !ctype_upper($value);
  702. setlocale(LC_CTYPE, $locale);
  703. if ($valid) {
  704. static::reportInvalidArgument(sprintf(
  705. $message ?: 'Expected a value to contain uppercase characters only. Got: %s',
  706. static::valueToString($value)
  707. ));
  708. }
  709. }
  710. public static function length($value, $length, $message = '')
  711. {
  712. if ($length !== static::strlen($value)) {
  713. static::reportInvalidArgument(sprintf(
  714. $message ?: 'Expected a value to contain %2$s characters. Got: %s',
  715. static::valueToString($value),
  716. $length
  717. ));
  718. }
  719. }
  720. public static function minLength($value, $min, $message = '')
  721. {
  722. if (static::strlen($value) < $min) {
  723. static::reportInvalidArgument(sprintf(
  724. $message ?: 'Expected a value to contain at least %2$s characters. Got: %s',
  725. static::valueToString($value),
  726. $min
  727. ));
  728. }
  729. }
  730. public static function maxLength($value, $max, $message = '')
  731. {
  732. if (static::strlen($value) > $max) {
  733. static::reportInvalidArgument(sprintf(
  734. $message ?: 'Expected a value to contain at most %2$s characters. Got: %s',
  735. static::valueToString($value),
  736. $max
  737. ));
  738. }
  739. }
  740. public static function lengthBetween($value, $min, $max, $message = '')
  741. {
  742. $length = static::strlen($value);
  743. if ($length < $min || $length > $max) {
  744. static::reportInvalidArgument(sprintf(
  745. $message ?: 'Expected a value to contain between %2$s and %3$s characters. Got: %s',
  746. static::valueToString($value),
  747. $min,
  748. $max
  749. ));
  750. }
  751. }
  752. public static function fileExists($value, $message = '')
  753. {
  754. static::string($value);
  755. if (!file_exists($value)) {
  756. static::reportInvalidArgument(sprintf(
  757. $message ?: 'The file %s does not exist.',
  758. static::valueToString($value)
  759. ));
  760. }
  761. }
  762. public static function file($value, $message = '')
  763. {
  764. static::fileExists($value, $message);
  765. if (!is_file($value)) {
  766. static::reportInvalidArgument(sprintf(
  767. $message ?: 'The path %s is not a file.',
  768. static::valueToString($value)
  769. ));
  770. }
  771. }
  772. public static function directory($value, $message = '')
  773. {
  774. static::fileExists($value, $message);
  775. if (!is_dir($value)) {
  776. static::reportInvalidArgument(sprintf(
  777. $message ?: 'The path %s is no directory.',
  778. static::valueToString($value)
  779. ));
  780. }
  781. }
  782. public static function readable($value, $message = '')
  783. {
  784. if (!is_readable($value)) {
  785. static::reportInvalidArgument(sprintf(
  786. $message ?: 'The path %s is not readable.',
  787. static::valueToString($value)
  788. ));
  789. }
  790. }
  791. public static function writable($value, $message = '')
  792. {
  793. if (!is_writable($value)) {
  794. static::reportInvalidArgument(sprintf(
  795. $message ?: 'The path %s is not writable.',
  796. static::valueToString($value)
  797. ));
  798. }
  799. }
  800. public static function classExists($value, $message = '')
  801. {
  802. if (!class_exists($value)) {
  803. static::reportInvalidArgument(sprintf(
  804. $message ?: 'Expected an existing class name. Got: %s',
  805. static::valueToString($value)
  806. ));
  807. }
  808. }
  809. public static function subclassOf($value, $class, $message = '')
  810. {
  811. if (!is_subclass_of($value, $class)) {
  812. static::reportInvalidArgument(sprintf(
  813. $message ?: 'Expected a sub-class of %2$s. Got: %s',
  814. static::valueToString($value),
  815. static::valueToString($class)
  816. ));
  817. }
  818. }
  819. public static function interfaceExists($value, $message = '')
  820. {
  821. if (!interface_exists($value)) {
  822. static::reportInvalidArgument(sprintf(
  823. $message ?: 'Expected an existing interface name. got %s',
  824. static::valueToString($value)
  825. ));
  826. }
  827. }
  828. public static function implementsInterface($value, $interface, $message = '')
  829. {
  830. if (!in_array($interface, class_implements($value))) {
  831. static::reportInvalidArgument(sprintf(
  832. $message ?: 'Expected an implementation of %2$s. Got: %s',
  833. static::valueToString($value),
  834. static::valueToString($interface)
  835. ));
  836. }
  837. }
  838. public static function propertyExists($classOrObject, $property, $message = '')
  839. {
  840. if (!property_exists($classOrObject, $property)) {
  841. static::reportInvalidArgument(sprintf(
  842. $message ?: 'Expected the property %s to exist.',
  843. static::valueToString($property)
  844. ));
  845. }
  846. }
  847. public static function propertyNotExists($classOrObject, $property, $message = '')
  848. {
  849. if (property_exists($classOrObject, $property)) {
  850. static::reportInvalidArgument(sprintf(
  851. $message ?: 'Expected the property %s to not exist.',
  852. static::valueToString($property)
  853. ));
  854. }
  855. }
  856. public static function methodExists($classOrObject, $method, $message = '')
  857. {
  858. if (!method_exists($classOrObject, $method)) {
  859. static::reportInvalidArgument(sprintf(
  860. $message ?: 'Expected the method %s to exist.',
  861. static::valueToString($method)
  862. ));
  863. }
  864. }
  865. public static function methodNotExists($classOrObject, $method, $message = '')
  866. {
  867. if (method_exists($classOrObject, $method)) {
  868. static::reportInvalidArgument(sprintf(
  869. $message ?: 'Expected the method %s to not exist.',
  870. static::valueToString($method)
  871. ));
  872. }
  873. }
  874. public static function keyExists($array, $key, $message = '')
  875. {
  876. if (!(isset($array[$key]) || array_key_exists($key, $array))) {
  877. static::reportInvalidArgument(sprintf(
  878. $message ?: 'Expected the key %s to exist.',
  879. static::valueToString($key)
  880. ));
  881. }
  882. }
  883. public static function keyNotExists($array, $key, $message = '')
  884. {
  885. if (isset($array[$key]) || array_key_exists($key, $array)) {
  886. static::reportInvalidArgument(sprintf(
  887. $message ?: 'Expected the key %s to not exist.',
  888. static::valueToString($key)
  889. ));
  890. }
  891. }
  892. public static function count($array, $number, $message = '')
  893. {
  894. static::eq(
  895. count($array),
  896. $number,
  897. $message ?: sprintf('Expected an array to contain %d elements. Got: %d.', $number, count($array))
  898. );
  899. }
  900. public static function minCount($array, $min, $message = '')
  901. {
  902. if (count($array) < $min) {
  903. static::reportInvalidArgument(sprintf(
  904. $message ?: 'Expected an array to contain at least %2$d elements. Got: %d',
  905. count($array),
  906. $min
  907. ));
  908. }
  909. }
  910. public static function maxCount($array, $max, $message = '')
  911. {
  912. if (count($array) > $max) {
  913. static::reportInvalidArgument(sprintf(
  914. $message ?: 'Expected an array to contain at most %2$d elements. Got: %d',
  915. count($array),
  916. $max
  917. ));
  918. }
  919. }
  920. public static function countBetween($array, $min, $max, $message = '')
  921. {
  922. $count = count($array);
  923. if ($count < $min || $count > $max) {
  924. static::reportInvalidArgument(sprintf(
  925. $message ?: 'Expected an array to contain between %2$d and %3$d elements. Got: %d',
  926. $count,
  927. $min,
  928. $max
  929. ));
  930. }
  931. }
  932. public static function isList($array, $message = '')
  933. {
  934. if (!is_array($array) || !$array || array_keys($array) !== range(0, count($array) - 1)) {
  935. static::reportInvalidArgument(
  936. $message ?: 'Expected list - non-associative array.'
  937. );
  938. }
  939. }
  940. public static function isMap($array, $message = '')
  941. {
  942. if (
  943. !is_array($array) ||
  944. !$array ||
  945. array_keys($array) !== array_filter(array_keys($array), function ($key) {
  946. return is_string($key);
  947. })
  948. ) {
  949. static::reportInvalidArgument(
  950. $message ?: 'Expected map - associative array with string keys.'
  951. );
  952. }
  953. }
  954. public static function uuid($value, $message = '')
  955. {
  956. $value = str_replace(array('urn:', 'uuid:', '{', '}'), '', $value);
  957. // The nil UUID is special form of UUID that is specified to have all
  958. // 128 bits set to zero.
  959. if ('00000000-0000-0000-0000-000000000000' === $value) {
  960. return;
  961. }
  962. if (!preg_match('/^[0-9A-Fa-f]{8}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{12}$/', $value)) {
  963. static::reportInvalidArgument(sprintf(
  964. $message ?: 'Value %s is not a valid UUID.',
  965. static::valueToString($value)
  966. ));
  967. }
  968. }
  969. public static function throws(Closure $expression, $class = 'Exception', $message = '')
  970. {
  971. static::string($class);
  972. $actual = 'none';
  973. try {
  974. $expression();
  975. } catch (Exception $e) {
  976. $actual = get_class($e);
  977. if ($e instanceof $class) {
  978. return;
  979. }
  980. } catch (Throwable $e) {
  981. $actual = get_class($e);
  982. if ($e instanceof $class) {
  983. return;
  984. }
  985. }
  986. static::reportInvalidArgument($message ?: sprintf(
  987. 'Expected to throw "%s", got "%s"',
  988. $class,
  989. $actual
  990. ));
  991. }
  992. public static function __callStatic($name, $arguments)
  993. {
  994. if ('nullOr' === substr($name, 0, 6)) {
  995. if (null !== $arguments[0]) {
  996. $method = lcfirst(substr($name, 6));
  997. call_user_func_array(array('static', $method), $arguments);
  998. }
  999. return;
  1000. }
  1001. if ('all' === substr($name, 0, 3)) {
  1002. static::isIterable($arguments[0]);
  1003. $method = lcfirst(substr($name, 3));
  1004. $args = $arguments;
  1005. foreach ($arguments[0] as $entry) {
  1006. $args[0] = $entry;
  1007. call_user_func_array(array('static', $method), $args);
  1008. }
  1009. return;
  1010. }
  1011. throw new BadMethodCallException('No such method: '.$name);
  1012. }
  1013. protected static function valueToString($value)
  1014. {
  1015. if (null === $value) {
  1016. return 'null';
  1017. }
  1018. if (true === $value) {
  1019. return 'true';
  1020. }
  1021. if (false === $value) {
  1022. return 'false';
  1023. }
  1024. if (is_array($value)) {
  1025. return 'array';
  1026. }
  1027. if (is_object($value)) {
  1028. if (method_exists($value, '__toString')) {
  1029. return get_class($value).': '.self::valueToString($value->__toString());
  1030. }
  1031. return get_class($value);
  1032. }
  1033. if (is_resource($value)) {
  1034. return 'resource';
  1035. }
  1036. if (is_string($value)) {
  1037. return '"'.$value.'"';
  1038. }
  1039. return (string) $value;
  1040. }
  1041. protected static function typeToString($value)
  1042. {
  1043. return is_object($value) ? get_class($value) : gettype($value);
  1044. }
  1045. protected static function strlen($value)
  1046. {
  1047. if (!function_exists('mb_detect_encoding')) {
  1048. return strlen($value);
  1049. }
  1050. if (false === $encoding = mb_detect_encoding($value)) {
  1051. return strlen($value);
  1052. }
  1053. return mb_strwidth($value, $encoding);
  1054. }
  1055. protected static function reportInvalidArgument($message)
  1056. {
  1057. throw new InvalidArgumentException($message);
  1058. }
  1059. private function __construct()
  1060. {
  1061. }
  1062. }