Assert.php 37 KB

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