截流自动化的商城平台
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

Assert.php 55KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080
  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 DateTime;
  16. use DateTimeImmutable;
  17. use Exception;
  18. use ResourceBundle;
  19. use SimpleXMLElement;
  20. use Throwable;
  21. use Traversable;
  22. /**
  23. * Efficient assertions to validate the input/output of your methods.
  24. *
  25. * @since 1.0
  26. *
  27. * @author Bernhard Schussek <bschussek@gmail.com>
  28. */
  29. class Assert
  30. {
  31. use Mixin;
  32. /**
  33. * @psalm-pure
  34. * @psalm-assert string $value
  35. *
  36. * @param mixed $value
  37. * @param string $message
  38. *
  39. * @throws InvalidArgumentException
  40. */
  41. public static function string($value, $message = '')
  42. {
  43. if (!\is_string($value)) {
  44. static::reportInvalidArgument(\sprintf(
  45. $message ?: 'Expected a string. Got: %s',
  46. static::typeToString($value)
  47. ));
  48. }
  49. }
  50. /**
  51. * @psalm-pure
  52. * @psalm-assert non-empty-string $value
  53. *
  54. * @param mixed $value
  55. * @param string $message
  56. *
  57. * @throws InvalidArgumentException
  58. */
  59. public static function stringNotEmpty($value, $message = '')
  60. {
  61. static::string($value, $message);
  62. static::notEq($value, '', $message);
  63. }
  64. /**
  65. * @psalm-pure
  66. * @psalm-assert int $value
  67. *
  68. * @param mixed $value
  69. * @param string $message
  70. *
  71. * @throws InvalidArgumentException
  72. */
  73. public static function integer($value, $message = '')
  74. {
  75. if (!\is_int($value)) {
  76. static::reportInvalidArgument(\sprintf(
  77. $message ?: 'Expected an integer. Got: %s',
  78. static::typeToString($value)
  79. ));
  80. }
  81. }
  82. /**
  83. * @psalm-pure
  84. * @psalm-assert numeric $value
  85. *
  86. * @param mixed $value
  87. * @param string $message
  88. *
  89. * @throws InvalidArgumentException
  90. */
  91. public static function integerish($value, $message = '')
  92. {
  93. if (!\is_numeric($value) || $value != (int) $value) {
  94. static::reportInvalidArgument(\sprintf(
  95. $message ?: 'Expected an integerish value. Got: %s',
  96. static::typeToString($value)
  97. ));
  98. }
  99. }
  100. /**
  101. * @psalm-pure
  102. * @psalm-assert positive-int $value
  103. *
  104. * @param mixed $value
  105. * @param string $message
  106. *
  107. * @throws InvalidArgumentException
  108. */
  109. public static function positiveInteger($value, $message = '')
  110. {
  111. if (!(\is_int($value) && $value > 0)) {
  112. static::reportInvalidArgument(\sprintf(
  113. $message ?: 'Expected a positive integer. Got: %s',
  114. static::valueToString($value)
  115. ));
  116. }
  117. }
  118. /**
  119. * @psalm-pure
  120. * @psalm-assert float $value
  121. *
  122. * @param mixed $value
  123. * @param string $message
  124. *
  125. * @throws InvalidArgumentException
  126. */
  127. public static function float($value, $message = '')
  128. {
  129. if (!\is_float($value)) {
  130. static::reportInvalidArgument(\sprintf(
  131. $message ?: 'Expected a float. Got: %s',
  132. static::typeToString($value)
  133. ));
  134. }
  135. }
  136. /**
  137. * @psalm-pure
  138. * @psalm-assert numeric $value
  139. *
  140. * @param mixed $value
  141. * @param string $message
  142. *
  143. * @throws InvalidArgumentException
  144. */
  145. public static function numeric($value, $message = '')
  146. {
  147. if (!\is_numeric($value)) {
  148. static::reportInvalidArgument(\sprintf(
  149. $message ?: 'Expected a numeric. Got: %s',
  150. static::typeToString($value)
  151. ));
  152. }
  153. }
  154. /**
  155. * @psalm-pure
  156. * @psalm-assert positive-int|0 $value
  157. *
  158. * @param mixed $value
  159. * @param string $message
  160. *
  161. * @throws InvalidArgumentException
  162. */
  163. public static function natural($value, $message = '')
  164. {
  165. if (!\is_int($value) || $value < 0) {
  166. static::reportInvalidArgument(\sprintf(
  167. $message ?: 'Expected a non-negative integer. Got: %s',
  168. static::valueToString($value)
  169. ));
  170. }
  171. }
  172. /**
  173. * @psalm-pure
  174. * @psalm-assert bool $value
  175. *
  176. * @param mixed $value
  177. * @param string $message
  178. *
  179. * @throws InvalidArgumentException
  180. */
  181. public static function boolean($value, $message = '')
  182. {
  183. if (!\is_bool($value)) {
  184. static::reportInvalidArgument(\sprintf(
  185. $message ?: 'Expected a boolean. Got: %s',
  186. static::typeToString($value)
  187. ));
  188. }
  189. }
  190. /**
  191. * @psalm-pure
  192. * @psalm-assert scalar $value
  193. *
  194. * @param mixed $value
  195. * @param string $message
  196. *
  197. * @throws InvalidArgumentException
  198. */
  199. public static function scalar($value, $message = '')
  200. {
  201. if (!\is_scalar($value)) {
  202. static::reportInvalidArgument(\sprintf(
  203. $message ?: 'Expected a scalar. Got: %s',
  204. static::typeToString($value)
  205. ));
  206. }
  207. }
  208. /**
  209. * @psalm-pure
  210. * @psalm-assert object $value
  211. *
  212. * @param mixed $value
  213. * @param string $message
  214. *
  215. * @throws InvalidArgumentException
  216. */
  217. public static function object($value, $message = '')
  218. {
  219. if (!\is_object($value)) {
  220. static::reportInvalidArgument(\sprintf(
  221. $message ?: 'Expected an object. Got: %s',
  222. static::typeToString($value)
  223. ));
  224. }
  225. }
  226. /**
  227. * @psalm-pure
  228. * @psalm-assert resource $value
  229. *
  230. * @param mixed $value
  231. * @param string|null $type type of resource this should be. @see https://www.php.net/manual/en/function.get-resource-type.php
  232. * @param string $message
  233. *
  234. * @throws InvalidArgumentException
  235. */
  236. public static function resource($value, $type = null, $message = '')
  237. {
  238. if (!\is_resource($value)) {
  239. static::reportInvalidArgument(\sprintf(
  240. $message ?: 'Expected a resource. Got: %s',
  241. static::typeToString($value)
  242. ));
  243. }
  244. if ($type && $type !== \get_resource_type($value)) {
  245. static::reportInvalidArgument(\sprintf(
  246. $message ?: 'Expected a resource of type %2$s. Got: %s',
  247. static::typeToString($value),
  248. $type
  249. ));
  250. }
  251. }
  252. /**
  253. * @psalm-pure
  254. * @psalm-assert callable $value
  255. *
  256. * @param mixed $value
  257. * @param string $message
  258. *
  259. * @throws InvalidArgumentException
  260. */
  261. public static function isCallable($value, $message = '')
  262. {
  263. if (!\is_callable($value)) {
  264. static::reportInvalidArgument(\sprintf(
  265. $message ?: 'Expected a callable. Got: %s',
  266. static::typeToString($value)
  267. ));
  268. }
  269. }
  270. /**
  271. * @psalm-pure
  272. * @psalm-assert array $value
  273. *
  274. * @param mixed $value
  275. * @param string $message
  276. *
  277. * @throws InvalidArgumentException
  278. */
  279. public static function isArray($value, $message = '')
  280. {
  281. if (!\is_array($value)) {
  282. static::reportInvalidArgument(\sprintf(
  283. $message ?: 'Expected an array. Got: %s',
  284. static::typeToString($value)
  285. ));
  286. }
  287. }
  288. /**
  289. * @psalm-pure
  290. * @psalm-assert iterable $value
  291. *
  292. * @deprecated use "isIterable" or "isInstanceOf" instead
  293. *
  294. * @param mixed $value
  295. * @param string $message
  296. *
  297. * @throws InvalidArgumentException
  298. */
  299. public static function isTraversable($value, $message = '')
  300. {
  301. @\trigger_error(
  302. \sprintf(
  303. '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.',
  304. __METHOD__
  305. ),
  306. \E_USER_DEPRECATED
  307. );
  308. if (!\is_array($value) && !($value instanceof Traversable)) {
  309. static::reportInvalidArgument(\sprintf(
  310. $message ?: 'Expected a traversable. Got: %s',
  311. static::typeToString($value)
  312. ));
  313. }
  314. }
  315. /**
  316. * @psalm-pure
  317. * @psalm-assert array|ArrayAccess $value
  318. *
  319. * @param mixed $value
  320. * @param string $message
  321. *
  322. * @throws InvalidArgumentException
  323. */
  324. public static function isArrayAccessible($value, $message = '')
  325. {
  326. if (!\is_array($value) && !($value instanceof ArrayAccess)) {
  327. static::reportInvalidArgument(\sprintf(
  328. $message ?: 'Expected an array accessible. Got: %s',
  329. static::typeToString($value)
  330. ));
  331. }
  332. }
  333. /**
  334. * @psalm-pure
  335. * @psalm-assert countable $value
  336. *
  337. * @param mixed $value
  338. * @param string $message
  339. *
  340. * @throws InvalidArgumentException
  341. */
  342. public static function isCountable($value, $message = '')
  343. {
  344. if (
  345. !\is_array($value)
  346. && !($value instanceof Countable)
  347. && !($value instanceof ResourceBundle)
  348. && !($value instanceof SimpleXMLElement)
  349. ) {
  350. static::reportInvalidArgument(\sprintf(
  351. $message ?: 'Expected a countable. Got: %s',
  352. static::typeToString($value)
  353. ));
  354. }
  355. }
  356. /**
  357. * @psalm-pure
  358. * @psalm-assert iterable $value
  359. *
  360. * @param mixed $value
  361. * @param string $message
  362. *
  363. * @throws InvalidArgumentException
  364. */
  365. public static function isIterable($value, $message = '')
  366. {
  367. if (!\is_array($value) && !($value instanceof Traversable)) {
  368. static::reportInvalidArgument(\sprintf(
  369. $message ?: 'Expected an iterable. Got: %s',
  370. static::typeToString($value)
  371. ));
  372. }
  373. }
  374. /**
  375. * @psalm-pure
  376. * @psalm-template ExpectedType of object
  377. * @psalm-param class-string<ExpectedType> $class
  378. * @psalm-assert ExpectedType $value
  379. *
  380. * @param mixed $value
  381. * @param string|object $class
  382. * @param string $message
  383. *
  384. * @throws InvalidArgumentException
  385. */
  386. public static function isInstanceOf($value, $class, $message = '')
  387. {
  388. if (!($value instanceof $class)) {
  389. static::reportInvalidArgument(\sprintf(
  390. $message ?: 'Expected an instance of %2$s. Got: %s',
  391. static::typeToString($value),
  392. $class
  393. ));
  394. }
  395. }
  396. /**
  397. * @psalm-pure
  398. * @psalm-template ExpectedType of object
  399. * @psalm-param class-string<ExpectedType> $class
  400. * @psalm-assert !ExpectedType $value
  401. *
  402. * @param mixed $value
  403. * @param string|object $class
  404. * @param string $message
  405. *
  406. * @throws InvalidArgumentException
  407. */
  408. public static function notInstanceOf($value, $class, $message = '')
  409. {
  410. if ($value instanceof $class) {
  411. static::reportInvalidArgument(\sprintf(
  412. $message ?: 'Expected an instance other than %2$s. Got: %s',
  413. static::typeToString($value),
  414. $class
  415. ));
  416. }
  417. }
  418. /**
  419. * @psalm-pure
  420. * @psalm-param array<class-string> $classes
  421. *
  422. * @param mixed $value
  423. * @param array<object|string> $classes
  424. * @param string $message
  425. *
  426. * @throws InvalidArgumentException
  427. */
  428. public static function isInstanceOfAny($value, array $classes, $message = '')
  429. {
  430. foreach ($classes as $class) {
  431. if ($value instanceof $class) {
  432. return;
  433. }
  434. }
  435. static::reportInvalidArgument(\sprintf(
  436. $message ?: 'Expected an instance of any of %2$s. Got: %s',
  437. static::typeToString($value),
  438. \implode(', ', \array_map(array(static::class, 'valueToString'), $classes))
  439. ));
  440. }
  441. /**
  442. * @psalm-pure
  443. * @psalm-template ExpectedType of object
  444. * @psalm-param class-string<ExpectedType> $class
  445. * @psalm-assert ExpectedType|class-string<ExpectedType> $value
  446. *
  447. * @param object|string $value
  448. * @param string $class
  449. * @param string $message
  450. *
  451. * @throws InvalidArgumentException
  452. */
  453. public static function isAOf($value, $class, $message = '')
  454. {
  455. static::string($class, 'Expected class as a string. Got: %s');
  456. if (!\is_a($value, $class, \is_string($value))) {
  457. static::reportInvalidArgument(sprintf(
  458. $message ?: 'Expected an instance of this class or to this class among its parents "%2$s". Got: %s',
  459. static::valueToString($value),
  460. $class
  461. ));
  462. }
  463. }
  464. /**
  465. * @psalm-pure
  466. * @psalm-template UnexpectedType of object
  467. * @psalm-param class-string<UnexpectedType> $class
  468. * @psalm-assert !UnexpectedType $value
  469. * @psalm-assert !class-string<UnexpectedType> $value
  470. *
  471. * @param object|string $value
  472. * @param string $class
  473. * @param string $message
  474. *
  475. * @throws InvalidArgumentException
  476. */
  477. public static function isNotA($value, $class, $message = '')
  478. {
  479. static::string($class, 'Expected class as a string. Got: %s');
  480. if (\is_a($value, $class, \is_string($value))) {
  481. static::reportInvalidArgument(sprintf(
  482. $message ?: 'Expected an instance of this class or to this class among its parents other than "%2$s". Got: %s',
  483. static::valueToString($value),
  484. $class
  485. ));
  486. }
  487. }
  488. /**
  489. * @psalm-pure
  490. * @psalm-param array<class-string> $classes
  491. *
  492. * @param object|string $value
  493. * @param string[] $classes
  494. * @param string $message
  495. *
  496. * @throws InvalidArgumentException
  497. */
  498. public static function isAnyOf($value, array $classes, $message = '')
  499. {
  500. foreach ($classes as $class) {
  501. static::string($class, 'Expected class as a string. Got: %s');
  502. if (\is_a($value, $class, \is_string($value))) {
  503. return;
  504. }
  505. }
  506. static::reportInvalidArgument(sprintf(
  507. $message ?: 'Expected an instance of any of this classes or any of those classes among their parents "%2$s". Got: %s',
  508. static::valueToString($value),
  509. \implode(', ', $classes)
  510. ));
  511. }
  512. /**
  513. * @psalm-pure
  514. * @psalm-assert empty $value
  515. *
  516. * @param mixed $value
  517. * @param string $message
  518. *
  519. * @throws InvalidArgumentException
  520. */
  521. public static function isEmpty($value, $message = '')
  522. {
  523. if (!empty($value)) {
  524. static::reportInvalidArgument(\sprintf(
  525. $message ?: 'Expected an empty value. Got: %s',
  526. static::valueToString($value)
  527. ));
  528. }
  529. }
  530. /**
  531. * @psalm-pure
  532. * @psalm-assert !empty $value
  533. *
  534. * @param mixed $value
  535. * @param string $message
  536. *
  537. * @throws InvalidArgumentException
  538. */
  539. public static function notEmpty($value, $message = '')
  540. {
  541. if (empty($value)) {
  542. static::reportInvalidArgument(\sprintf(
  543. $message ?: 'Expected a non-empty value. Got: %s',
  544. static::valueToString($value)
  545. ));
  546. }
  547. }
  548. /**
  549. * @psalm-pure
  550. * @psalm-assert null $value
  551. *
  552. * @param mixed $value
  553. * @param string $message
  554. *
  555. * @throws InvalidArgumentException
  556. */
  557. public static function null($value, $message = '')
  558. {
  559. if (null !== $value) {
  560. static::reportInvalidArgument(\sprintf(
  561. $message ?: 'Expected null. Got: %s',
  562. static::valueToString($value)
  563. ));
  564. }
  565. }
  566. /**
  567. * @psalm-pure
  568. * @psalm-assert !null $value
  569. *
  570. * @param mixed $value
  571. * @param string $message
  572. *
  573. * @throws InvalidArgumentException
  574. */
  575. public static function notNull($value, $message = '')
  576. {
  577. if (null === $value) {
  578. static::reportInvalidArgument(
  579. $message ?: 'Expected a value other than null.'
  580. );
  581. }
  582. }
  583. /**
  584. * @psalm-pure
  585. * @psalm-assert true $value
  586. *
  587. * @param mixed $value
  588. * @param string $message
  589. *
  590. * @throws InvalidArgumentException
  591. */
  592. public static function true($value, $message = '')
  593. {
  594. if (true !== $value) {
  595. static::reportInvalidArgument(\sprintf(
  596. $message ?: 'Expected a value to be true. Got: %s',
  597. static::valueToString($value)
  598. ));
  599. }
  600. }
  601. /**
  602. * @psalm-pure
  603. * @psalm-assert false $value
  604. *
  605. * @param mixed $value
  606. * @param string $message
  607. *
  608. * @throws InvalidArgumentException
  609. */
  610. public static function false($value, $message = '')
  611. {
  612. if (false !== $value) {
  613. static::reportInvalidArgument(\sprintf(
  614. $message ?: 'Expected a value to be false. Got: %s',
  615. static::valueToString($value)
  616. ));
  617. }
  618. }
  619. /**
  620. * @psalm-pure
  621. * @psalm-assert !false $value
  622. *
  623. * @param mixed $value
  624. * @param string $message
  625. *
  626. * @throws InvalidArgumentException
  627. */
  628. public static function notFalse($value, $message = '')
  629. {
  630. if (false === $value) {
  631. static::reportInvalidArgument(
  632. $message ?: 'Expected a value other than false.'
  633. );
  634. }
  635. }
  636. /**
  637. * @param mixed $value
  638. * @param string $message
  639. *
  640. * @throws InvalidArgumentException
  641. */
  642. public static function ip($value, $message = '')
  643. {
  644. if (false === \filter_var($value, \FILTER_VALIDATE_IP)) {
  645. static::reportInvalidArgument(\sprintf(
  646. $message ?: 'Expected a value to be an IP. Got: %s',
  647. static::valueToString($value)
  648. ));
  649. }
  650. }
  651. /**
  652. * @param mixed $value
  653. * @param string $message
  654. *
  655. * @throws InvalidArgumentException
  656. */
  657. public static function ipv4($value, $message = '')
  658. {
  659. if (false === \filter_var($value, \FILTER_VALIDATE_IP, \FILTER_FLAG_IPV4)) {
  660. static::reportInvalidArgument(\sprintf(
  661. $message ?: 'Expected a value to be an IPv4. Got: %s',
  662. static::valueToString($value)
  663. ));
  664. }
  665. }
  666. /**
  667. * @param mixed $value
  668. * @param string $message
  669. *
  670. * @throws InvalidArgumentException
  671. */
  672. public static function ipv6($value, $message = '')
  673. {
  674. if (false === \filter_var($value, \FILTER_VALIDATE_IP, \FILTER_FLAG_IPV6)) {
  675. static::reportInvalidArgument(\sprintf(
  676. $message ?: 'Expected a value to be an IPv6. Got: %s',
  677. static::valueToString($value)
  678. ));
  679. }
  680. }
  681. /**
  682. * @param mixed $value
  683. * @param string $message
  684. *
  685. * @throws InvalidArgumentException
  686. */
  687. public static function email($value, $message = '')
  688. {
  689. if (false === \filter_var($value, FILTER_VALIDATE_EMAIL)) {
  690. static::reportInvalidArgument(\sprintf(
  691. $message ?: 'Expected a value to be a valid e-mail address. Got: %s',
  692. static::valueToString($value)
  693. ));
  694. }
  695. }
  696. /**
  697. * Does non strict comparisons on the items, so ['3', 3] will not pass the assertion.
  698. *
  699. * @param array $values
  700. * @param string $message
  701. *
  702. * @throws InvalidArgumentException
  703. */
  704. public static function uniqueValues(array $values, $message = '')
  705. {
  706. $allValues = \count($values);
  707. $uniqueValues = \count(\array_unique($values));
  708. if ($allValues !== $uniqueValues) {
  709. $difference = $allValues - $uniqueValues;
  710. static::reportInvalidArgument(\sprintf(
  711. $message ?: 'Expected an array of unique values, but %s of them %s duplicated',
  712. $difference,
  713. (1 === $difference ? 'is' : 'are')
  714. ));
  715. }
  716. }
  717. /**
  718. * @param mixed $value
  719. * @param mixed $expect
  720. * @param string $message
  721. *
  722. * @throws InvalidArgumentException
  723. */
  724. public static function eq($value, $expect, $message = '')
  725. {
  726. if ($expect != $value) {
  727. static::reportInvalidArgument(\sprintf(
  728. $message ?: 'Expected a value equal to %2$s. Got: %s',
  729. static::valueToString($value),
  730. static::valueToString($expect)
  731. ));
  732. }
  733. }
  734. /**
  735. * @param mixed $value
  736. * @param mixed $expect
  737. * @param string $message
  738. *
  739. * @throws InvalidArgumentException
  740. */
  741. public static function notEq($value, $expect, $message = '')
  742. {
  743. if ($expect == $value) {
  744. static::reportInvalidArgument(\sprintf(
  745. $message ?: 'Expected a different value than %s.',
  746. static::valueToString($expect)
  747. ));
  748. }
  749. }
  750. /**
  751. * @psalm-pure
  752. *
  753. * @param mixed $value
  754. * @param mixed $expect
  755. * @param string $message
  756. *
  757. * @throws InvalidArgumentException
  758. */
  759. public static function same($value, $expect, $message = '')
  760. {
  761. if ($expect !== $value) {
  762. static::reportInvalidArgument(\sprintf(
  763. $message ?: 'Expected a value identical to %2$s. Got: %s',
  764. static::valueToString($value),
  765. static::valueToString($expect)
  766. ));
  767. }
  768. }
  769. /**
  770. * @psalm-pure
  771. *
  772. * @param mixed $value
  773. * @param mixed $expect
  774. * @param string $message
  775. *
  776. * @throws InvalidArgumentException
  777. */
  778. public static function notSame($value, $expect, $message = '')
  779. {
  780. if ($expect === $value) {
  781. static::reportInvalidArgument(\sprintf(
  782. $message ?: 'Expected a value not identical to %s.',
  783. static::valueToString($expect)
  784. ));
  785. }
  786. }
  787. /**
  788. * @psalm-pure
  789. *
  790. * @param mixed $value
  791. * @param mixed $limit
  792. * @param string $message
  793. *
  794. * @throws InvalidArgumentException
  795. */
  796. public static function greaterThan($value, $limit, $message = '')
  797. {
  798. if ($value <= $limit) {
  799. static::reportInvalidArgument(\sprintf(
  800. $message ?: 'Expected a value greater than %2$s. Got: %s',
  801. static::valueToString($value),
  802. static::valueToString($limit)
  803. ));
  804. }
  805. }
  806. /**
  807. * @psalm-pure
  808. *
  809. * @param mixed $value
  810. * @param mixed $limit
  811. * @param string $message
  812. *
  813. * @throws InvalidArgumentException
  814. */
  815. public static function greaterThanEq($value, $limit, $message = '')
  816. {
  817. if ($value < $limit) {
  818. static::reportInvalidArgument(\sprintf(
  819. $message ?: 'Expected a value greater than or equal to %2$s. Got: %s',
  820. static::valueToString($value),
  821. static::valueToString($limit)
  822. ));
  823. }
  824. }
  825. /**
  826. * @psalm-pure
  827. *
  828. * @param mixed $value
  829. * @param mixed $limit
  830. * @param string $message
  831. *
  832. * @throws InvalidArgumentException
  833. */
  834. public static function lessThan($value, $limit, $message = '')
  835. {
  836. if ($value >= $limit) {
  837. static::reportInvalidArgument(\sprintf(
  838. $message ?: 'Expected a value less than %2$s. Got: %s',
  839. static::valueToString($value),
  840. static::valueToString($limit)
  841. ));
  842. }
  843. }
  844. /**
  845. * @psalm-pure
  846. *
  847. * @param mixed $value
  848. * @param mixed $limit
  849. * @param string $message
  850. *
  851. * @throws InvalidArgumentException
  852. */
  853. public static function lessThanEq($value, $limit, $message = '')
  854. {
  855. if ($value > $limit) {
  856. static::reportInvalidArgument(\sprintf(
  857. $message ?: 'Expected a value less than or equal to %2$s. Got: %s',
  858. static::valueToString($value),
  859. static::valueToString($limit)
  860. ));
  861. }
  862. }
  863. /**
  864. * Inclusive range, so Assert::(3, 3, 5) passes.
  865. *
  866. * @psalm-pure
  867. *
  868. * @param mixed $value
  869. * @param mixed $min
  870. * @param mixed $max
  871. * @param string $message
  872. *
  873. * @throws InvalidArgumentException
  874. */
  875. public static function range($value, $min, $max, $message = '')
  876. {
  877. if ($value < $min || $value > $max) {
  878. static::reportInvalidArgument(\sprintf(
  879. $message ?: 'Expected a value between %2$s and %3$s. Got: %s',
  880. static::valueToString($value),
  881. static::valueToString($min),
  882. static::valueToString($max)
  883. ));
  884. }
  885. }
  886. /**
  887. * A more human-readable alias of Assert::inArray().
  888. *
  889. * @psalm-pure
  890. *
  891. * @param mixed $value
  892. * @param array $values
  893. * @param string $message
  894. *
  895. * @throws InvalidArgumentException
  896. */
  897. public static function oneOf($value, array $values, $message = '')
  898. {
  899. static::inArray($value, $values, $message);
  900. }
  901. /**
  902. * Does strict comparison, so Assert::inArray(3, ['3']) does not pass the assertion.
  903. *
  904. * @psalm-pure
  905. *
  906. * @param mixed $value
  907. * @param array $values
  908. * @param string $message
  909. *
  910. * @throws InvalidArgumentException
  911. */
  912. public static function inArray($value, array $values, $message = '')
  913. {
  914. if (!\in_array($value, $values, true)) {
  915. static::reportInvalidArgument(\sprintf(
  916. $message ?: 'Expected one of: %2$s. Got: %s',
  917. static::valueToString($value),
  918. \implode(', ', \array_map(array(static::class, 'valueToString'), $values))
  919. ));
  920. }
  921. }
  922. /**
  923. * @psalm-pure
  924. *
  925. * @param string $value
  926. * @param string $subString
  927. * @param string $message
  928. *
  929. * @throws InvalidArgumentException
  930. */
  931. public static function contains($value, $subString, $message = '')
  932. {
  933. if (false === \strpos($value, $subString)) {
  934. static::reportInvalidArgument(\sprintf(
  935. $message ?: 'Expected a value to contain %2$s. Got: %s',
  936. static::valueToString($value),
  937. static::valueToString($subString)
  938. ));
  939. }
  940. }
  941. /**
  942. * @psalm-pure
  943. *
  944. * @param string $value
  945. * @param string $subString
  946. * @param string $message
  947. *
  948. * @throws InvalidArgumentException
  949. */
  950. public static function notContains($value, $subString, $message = '')
  951. {
  952. if (false !== \strpos($value, $subString)) {
  953. static::reportInvalidArgument(\sprintf(
  954. $message ?: '%2$s was not expected to be contained in a value. Got: %s',
  955. static::valueToString($value),
  956. static::valueToString($subString)
  957. ));
  958. }
  959. }
  960. /**
  961. * @psalm-pure
  962. *
  963. * @param string $value
  964. * @param string $message
  965. *
  966. * @throws InvalidArgumentException
  967. */
  968. public static function notWhitespaceOnly($value, $message = '')
  969. {
  970. if (\preg_match('/^\s*$/', $value)) {
  971. static::reportInvalidArgument(\sprintf(
  972. $message ?: 'Expected a non-whitespace string. Got: %s',
  973. static::valueToString($value)
  974. ));
  975. }
  976. }
  977. /**
  978. * @psalm-pure
  979. *
  980. * @param string $value
  981. * @param string $prefix
  982. * @param string $message
  983. *
  984. * @throws InvalidArgumentException
  985. */
  986. public static function startsWith($value, $prefix, $message = '')
  987. {
  988. if (0 !== \strpos($value, $prefix)) {
  989. static::reportInvalidArgument(\sprintf(
  990. $message ?: 'Expected a value to start with %2$s. Got: %s',
  991. static::valueToString($value),
  992. static::valueToString($prefix)
  993. ));
  994. }
  995. }
  996. /**
  997. * @psalm-pure
  998. *
  999. * @param string $value
  1000. * @param string $prefix
  1001. * @param string $message
  1002. *
  1003. * @throws InvalidArgumentException
  1004. */
  1005. public static function notStartsWith($value, $prefix, $message = '')
  1006. {
  1007. if (0 === \strpos($value, $prefix)) {
  1008. static::reportInvalidArgument(\sprintf(
  1009. $message ?: 'Expected a value not to start with %2$s. Got: %s',
  1010. static::valueToString($value),
  1011. static::valueToString($prefix)
  1012. ));
  1013. }
  1014. }
  1015. /**
  1016. * @psalm-pure
  1017. *
  1018. * @param mixed $value
  1019. * @param string $message
  1020. *
  1021. * @throws InvalidArgumentException
  1022. */
  1023. public static function startsWithLetter($value, $message = '')
  1024. {
  1025. static::string($value);
  1026. $valid = isset($value[0]);
  1027. if ($valid) {
  1028. $locale = \setlocale(LC_CTYPE, 0);
  1029. \setlocale(LC_CTYPE, 'C');
  1030. $valid = \ctype_alpha($value[0]);
  1031. \setlocale(LC_CTYPE, $locale);
  1032. }
  1033. if (!$valid) {
  1034. static::reportInvalidArgument(\sprintf(
  1035. $message ?: 'Expected a value to start with a letter. Got: %s',
  1036. static::valueToString($value)
  1037. ));
  1038. }
  1039. }
  1040. /**
  1041. * @psalm-pure
  1042. *
  1043. * @param string $value
  1044. * @param string $suffix
  1045. * @param string $message
  1046. *
  1047. * @throws InvalidArgumentException
  1048. */
  1049. public static function endsWith($value, $suffix, $message = '')
  1050. {
  1051. if ($suffix !== \substr($value, -\strlen($suffix))) {
  1052. static::reportInvalidArgument(\sprintf(
  1053. $message ?: 'Expected a value to end with %2$s. Got: %s',
  1054. static::valueToString($value),
  1055. static::valueToString($suffix)
  1056. ));
  1057. }
  1058. }
  1059. /**
  1060. * @psalm-pure
  1061. *
  1062. * @param string $value
  1063. * @param string $suffix
  1064. * @param string $message
  1065. *
  1066. * @throws InvalidArgumentException
  1067. */
  1068. public static function notEndsWith($value, $suffix, $message = '')
  1069. {
  1070. if ($suffix === \substr($value, -\strlen($suffix))) {
  1071. static::reportInvalidArgument(\sprintf(
  1072. $message ?: 'Expected a value not to end with %2$s. Got: %s',
  1073. static::valueToString($value),
  1074. static::valueToString($suffix)
  1075. ));
  1076. }
  1077. }
  1078. /**
  1079. * @psalm-pure
  1080. *
  1081. * @param string $value
  1082. * @param string $pattern
  1083. * @param string $message
  1084. *
  1085. * @throws InvalidArgumentException
  1086. */
  1087. public static function regex($value, $pattern, $message = '')
  1088. {
  1089. if (!\preg_match($pattern, $value)) {
  1090. static::reportInvalidArgument(\sprintf(
  1091. $message ?: 'The value %s does not match the expected pattern.',
  1092. static::valueToString($value)
  1093. ));
  1094. }
  1095. }
  1096. /**
  1097. * @psalm-pure
  1098. *
  1099. * @param string $value
  1100. * @param string $pattern
  1101. * @param string $message
  1102. *
  1103. * @throws InvalidArgumentException
  1104. */
  1105. public static function notRegex($value, $pattern, $message = '')
  1106. {
  1107. if (\preg_match($pattern, $value, $matches, PREG_OFFSET_CAPTURE)) {
  1108. static::reportInvalidArgument(\sprintf(
  1109. $message ?: 'The value %s matches the pattern %s (at offset %d).',
  1110. static::valueToString($value),
  1111. static::valueToString($pattern),
  1112. $matches[0][1]
  1113. ));
  1114. }
  1115. }
  1116. /**
  1117. * @psalm-pure
  1118. *
  1119. * @param mixed $value
  1120. * @param string $message
  1121. *
  1122. * @throws InvalidArgumentException
  1123. */
  1124. public static function unicodeLetters($value, $message = '')
  1125. {
  1126. static::string($value);
  1127. if (!\preg_match('/^\p{L}+$/u', $value)) {
  1128. static::reportInvalidArgument(\sprintf(
  1129. $message ?: 'Expected a value to contain only Unicode letters. Got: %s',
  1130. static::valueToString($value)
  1131. ));
  1132. }
  1133. }
  1134. /**
  1135. * @psalm-pure
  1136. *
  1137. * @param mixed $value
  1138. * @param string $message
  1139. *
  1140. * @throws InvalidArgumentException
  1141. */
  1142. public static function alpha($value, $message = '')
  1143. {
  1144. static::string($value);
  1145. $locale = \setlocale(LC_CTYPE, 0);
  1146. \setlocale(LC_CTYPE, 'C');
  1147. $valid = !\ctype_alpha($value);
  1148. \setlocale(LC_CTYPE, $locale);
  1149. if ($valid) {
  1150. static::reportInvalidArgument(\sprintf(
  1151. $message ?: 'Expected a value to contain only letters. Got: %s',
  1152. static::valueToString($value)
  1153. ));
  1154. }
  1155. }
  1156. /**
  1157. * @psalm-pure
  1158. *
  1159. * @param string $value
  1160. * @param string $message
  1161. *
  1162. * @throws InvalidArgumentException
  1163. */
  1164. public static function digits($value, $message = '')
  1165. {
  1166. $locale = \setlocale(LC_CTYPE, 0);
  1167. \setlocale(LC_CTYPE, 'C');
  1168. $valid = !\ctype_digit($value);
  1169. \setlocale(LC_CTYPE, $locale);
  1170. if ($valid) {
  1171. static::reportInvalidArgument(\sprintf(
  1172. $message ?: 'Expected a value to contain digits only. Got: %s',
  1173. static::valueToString($value)
  1174. ));
  1175. }
  1176. }
  1177. /**
  1178. * @psalm-pure
  1179. *
  1180. * @param string $value
  1181. * @param string $message
  1182. *
  1183. * @throws InvalidArgumentException
  1184. */
  1185. public static function alnum($value, $message = '')
  1186. {
  1187. $locale = \setlocale(LC_CTYPE, 0);
  1188. \setlocale(LC_CTYPE, 'C');
  1189. $valid = !\ctype_alnum($value);
  1190. \setlocale(LC_CTYPE, $locale);
  1191. if ($valid) {
  1192. static::reportInvalidArgument(\sprintf(
  1193. $message ?: 'Expected a value to contain letters and digits only. Got: %s',
  1194. static::valueToString($value)
  1195. ));
  1196. }
  1197. }
  1198. /**
  1199. * @psalm-pure
  1200. * @psalm-assert lowercase-string $value
  1201. *
  1202. * @param string $value
  1203. * @param string $message
  1204. *
  1205. * @throws InvalidArgumentException
  1206. */
  1207. public static function lower($value, $message = '')
  1208. {
  1209. $locale = \setlocale(LC_CTYPE, 0);
  1210. \setlocale(LC_CTYPE, 'C');
  1211. $valid = !\ctype_lower($value);
  1212. \setlocale(LC_CTYPE, $locale);
  1213. if ($valid) {
  1214. static::reportInvalidArgument(\sprintf(
  1215. $message ?: 'Expected a value to contain lowercase characters only. Got: %s',
  1216. static::valueToString($value)
  1217. ));
  1218. }
  1219. }
  1220. /**
  1221. * @psalm-pure
  1222. * @psalm-assert !lowercase-string $value
  1223. *
  1224. * @param string $value
  1225. * @param string $message
  1226. *
  1227. * @throws InvalidArgumentException
  1228. */
  1229. public static function upper($value, $message = '')
  1230. {
  1231. $locale = \setlocale(LC_CTYPE, 0);
  1232. \setlocale(LC_CTYPE, 'C');
  1233. $valid = !\ctype_upper($value);
  1234. \setlocale(LC_CTYPE, $locale);
  1235. if ($valid) {
  1236. static::reportInvalidArgument(\sprintf(
  1237. $message ?: 'Expected a value to contain uppercase characters only. Got: %s',
  1238. static::valueToString($value)
  1239. ));
  1240. }
  1241. }
  1242. /**
  1243. * @psalm-pure
  1244. *
  1245. * @param string $value
  1246. * @param int $length
  1247. * @param string $message
  1248. *
  1249. * @throws InvalidArgumentException
  1250. */
  1251. public static function length($value, $length, $message = '')
  1252. {
  1253. if ($length !== static::strlen($value)) {
  1254. static::reportInvalidArgument(\sprintf(
  1255. $message ?: 'Expected a value to contain %2$s characters. Got: %s',
  1256. static::valueToString($value),
  1257. $length
  1258. ));
  1259. }
  1260. }
  1261. /**
  1262. * Inclusive min.
  1263. *
  1264. * @psalm-pure
  1265. *
  1266. * @param string $value
  1267. * @param int|float $min
  1268. * @param string $message
  1269. *
  1270. * @throws InvalidArgumentException
  1271. */
  1272. public static function minLength($value, $min, $message = '')
  1273. {
  1274. if (static::strlen($value) < $min) {
  1275. static::reportInvalidArgument(\sprintf(
  1276. $message ?: 'Expected a value to contain at least %2$s characters. Got: %s',
  1277. static::valueToString($value),
  1278. $min
  1279. ));
  1280. }
  1281. }
  1282. /**
  1283. * Inclusive max.
  1284. *
  1285. * @psalm-pure
  1286. *
  1287. * @param string $value
  1288. * @param int|float $max
  1289. * @param string $message
  1290. *
  1291. * @throws InvalidArgumentException
  1292. */
  1293. public static function maxLength($value, $max, $message = '')
  1294. {
  1295. if (static::strlen($value) > $max) {
  1296. static::reportInvalidArgument(\sprintf(
  1297. $message ?: 'Expected a value to contain at most %2$s characters. Got: %s',
  1298. static::valueToString($value),
  1299. $max
  1300. ));
  1301. }
  1302. }
  1303. /**
  1304. * Inclusive , so Assert::lengthBetween('asd', 3, 5); passes the assertion.
  1305. *
  1306. * @psalm-pure
  1307. *
  1308. * @param string $value
  1309. * @param int|float $min
  1310. * @param int|float $max
  1311. * @param string $message
  1312. *
  1313. * @throws InvalidArgumentException
  1314. */
  1315. public static function lengthBetween($value, $min, $max, $message = '')
  1316. {
  1317. $length = static::strlen($value);
  1318. if ($length < $min || $length > $max) {
  1319. static::reportInvalidArgument(\sprintf(
  1320. $message ?: 'Expected a value to contain between %2$s and %3$s characters. Got: %s',
  1321. static::valueToString($value),
  1322. $min,
  1323. $max
  1324. ));
  1325. }
  1326. }
  1327. /**
  1328. * Will also pass if $value is a directory, use Assert::file() instead if you need to be sure it is a file.
  1329. *
  1330. * @param mixed $value
  1331. * @param string $message
  1332. *
  1333. * @throws InvalidArgumentException
  1334. */
  1335. public static function fileExists($value, $message = '')
  1336. {
  1337. static::string($value);
  1338. if (!\file_exists($value)) {
  1339. static::reportInvalidArgument(\sprintf(
  1340. $message ?: 'The file %s does not exist.',
  1341. static::valueToString($value)
  1342. ));
  1343. }
  1344. }
  1345. /**
  1346. * @param mixed $value
  1347. * @param string $message
  1348. *
  1349. * @throws InvalidArgumentException
  1350. */
  1351. public static function file($value, $message = '')
  1352. {
  1353. static::fileExists($value, $message);
  1354. if (!\is_file($value)) {
  1355. static::reportInvalidArgument(\sprintf(
  1356. $message ?: 'The path %s is not a file.',
  1357. static::valueToString($value)
  1358. ));
  1359. }
  1360. }
  1361. /**
  1362. * @param mixed $value
  1363. * @param string $message
  1364. *
  1365. * @throws InvalidArgumentException
  1366. */
  1367. public static function directory($value, $message = '')
  1368. {
  1369. static::fileExists($value, $message);
  1370. if (!\is_dir($value)) {
  1371. static::reportInvalidArgument(\sprintf(
  1372. $message ?: 'The path %s is no directory.',
  1373. static::valueToString($value)
  1374. ));
  1375. }
  1376. }
  1377. /**
  1378. * @param string $value
  1379. * @param string $message
  1380. *
  1381. * @throws InvalidArgumentException
  1382. */
  1383. public static function readable($value, $message = '')
  1384. {
  1385. if (!\is_readable($value)) {
  1386. static::reportInvalidArgument(\sprintf(
  1387. $message ?: 'The path %s is not readable.',
  1388. static::valueToString($value)
  1389. ));
  1390. }
  1391. }
  1392. /**
  1393. * @param string $value
  1394. * @param string $message
  1395. *
  1396. * @throws InvalidArgumentException
  1397. */
  1398. public static function writable($value, $message = '')
  1399. {
  1400. if (!\is_writable($value)) {
  1401. static::reportInvalidArgument(\sprintf(
  1402. $message ?: 'The path %s is not writable.',
  1403. static::valueToString($value)
  1404. ));
  1405. }
  1406. }
  1407. /**
  1408. * @psalm-assert class-string $value
  1409. *
  1410. * @param mixed $value
  1411. * @param string $message
  1412. *
  1413. * @throws InvalidArgumentException
  1414. */
  1415. public static function classExists($value, $message = '')
  1416. {
  1417. if (!\class_exists($value)) {
  1418. static::reportInvalidArgument(\sprintf(
  1419. $message ?: 'Expected an existing class name. Got: %s',
  1420. static::valueToString($value)
  1421. ));
  1422. }
  1423. }
  1424. /**
  1425. * @psalm-pure
  1426. * @psalm-template ExpectedType of object
  1427. * @psalm-param class-string<ExpectedType> $class
  1428. * @psalm-assert class-string<ExpectedType>|ExpectedType $value
  1429. *
  1430. * @param mixed $value
  1431. * @param string|object $class
  1432. * @param string $message
  1433. *
  1434. * @throws InvalidArgumentException
  1435. */
  1436. public static function subclassOf($value, $class, $message = '')
  1437. {
  1438. if (!\is_subclass_of($value, $class)) {
  1439. static::reportInvalidArgument(\sprintf(
  1440. $message ?: 'Expected a sub-class of %2$s. Got: %s',
  1441. static::valueToString($value),
  1442. static::valueToString($class)
  1443. ));
  1444. }
  1445. }
  1446. /**
  1447. * @psalm-assert class-string $value
  1448. *
  1449. * @param mixed $value
  1450. * @param string $message
  1451. *
  1452. * @throws InvalidArgumentException
  1453. */
  1454. public static function interfaceExists($value, $message = '')
  1455. {
  1456. if (!\interface_exists($value)) {
  1457. static::reportInvalidArgument(\sprintf(
  1458. $message ?: 'Expected an existing interface name. got %s',
  1459. static::valueToString($value)
  1460. ));
  1461. }
  1462. }
  1463. /**
  1464. * @psalm-pure
  1465. * @psalm-template ExpectedType of object
  1466. * @psalm-param class-string<ExpectedType> $interface
  1467. * @psalm-assert class-string<ExpectedType> $value
  1468. *
  1469. * @param mixed $value
  1470. * @param mixed $interface
  1471. * @param string $message
  1472. *
  1473. * @throws InvalidArgumentException
  1474. */
  1475. public static function implementsInterface($value, $interface, $message = '')
  1476. {
  1477. if (!\in_array($interface, \class_implements($value))) {
  1478. static::reportInvalidArgument(\sprintf(
  1479. $message ?: 'Expected an implementation of %2$s. Got: %s',
  1480. static::valueToString($value),
  1481. static::valueToString($interface)
  1482. ));
  1483. }
  1484. }
  1485. /**
  1486. * @psalm-pure
  1487. * @psalm-param class-string|object $classOrObject
  1488. *
  1489. * @param string|object $classOrObject
  1490. * @param mixed $property
  1491. * @param string $message
  1492. *
  1493. * @throws InvalidArgumentException
  1494. */
  1495. public static function propertyExists($classOrObject, $property, $message = '')
  1496. {
  1497. if (!\property_exists($classOrObject, $property)) {
  1498. static::reportInvalidArgument(\sprintf(
  1499. $message ?: 'Expected the property %s to exist.',
  1500. static::valueToString($property)
  1501. ));
  1502. }
  1503. }
  1504. /**
  1505. * @psalm-pure
  1506. * @psalm-param class-string|object $classOrObject
  1507. *
  1508. * @param string|object $classOrObject
  1509. * @param mixed $property
  1510. * @param string $message
  1511. *
  1512. * @throws InvalidArgumentException
  1513. */
  1514. public static function propertyNotExists($classOrObject, $property, $message = '')
  1515. {
  1516. if (\property_exists($classOrObject, $property)) {
  1517. static::reportInvalidArgument(\sprintf(
  1518. $message ?: 'Expected the property %s to not exist.',
  1519. static::valueToString($property)
  1520. ));
  1521. }
  1522. }
  1523. /**
  1524. * @psalm-pure
  1525. * @psalm-param class-string|object $classOrObject
  1526. *
  1527. * @param string|object $classOrObject
  1528. * @param mixed $method
  1529. * @param string $message
  1530. *
  1531. * @throws InvalidArgumentException
  1532. */
  1533. public static function methodExists($classOrObject, $method, $message = '')
  1534. {
  1535. if (!(\is_string($classOrObject) || \is_object($classOrObject)) || !\method_exists($classOrObject, $method)) {
  1536. static::reportInvalidArgument(\sprintf(
  1537. $message ?: 'Expected the method %s to exist.',
  1538. static::valueToString($method)
  1539. ));
  1540. }
  1541. }
  1542. /**
  1543. * @psalm-pure
  1544. * @psalm-param class-string|object $classOrObject
  1545. *
  1546. * @param string|object $classOrObject
  1547. * @param mixed $method
  1548. * @param string $message
  1549. *
  1550. * @throws InvalidArgumentException
  1551. */
  1552. public static function methodNotExists($classOrObject, $method, $message = '')
  1553. {
  1554. if ((\is_string($classOrObject) || \is_object($classOrObject)) && \method_exists($classOrObject, $method)) {
  1555. static::reportInvalidArgument(\sprintf(
  1556. $message ?: 'Expected the method %s to not exist.',
  1557. static::valueToString($method)
  1558. ));
  1559. }
  1560. }
  1561. /**
  1562. * @psalm-pure
  1563. *
  1564. * @param array $array
  1565. * @param string|int $key
  1566. * @param string $message
  1567. *
  1568. * @throws InvalidArgumentException
  1569. */
  1570. public static function keyExists($array, $key, $message = '')
  1571. {
  1572. if (!(isset($array[$key]) || \array_key_exists($key, $array))) {
  1573. static::reportInvalidArgument(\sprintf(
  1574. $message ?: 'Expected the key %s to exist.',
  1575. static::valueToString($key)
  1576. ));
  1577. }
  1578. }
  1579. /**
  1580. * @psalm-pure
  1581. *
  1582. * @param array $array
  1583. * @param string|int $key
  1584. * @param string $message
  1585. *
  1586. * @throws InvalidArgumentException
  1587. */
  1588. public static function keyNotExists($array, $key, $message = '')
  1589. {
  1590. if (isset($array[$key]) || \array_key_exists($key, $array)) {
  1591. static::reportInvalidArgument(\sprintf(
  1592. $message ?: 'Expected the key %s to not exist.',
  1593. static::valueToString($key)
  1594. ));
  1595. }
  1596. }
  1597. /**
  1598. * Checks if a value is a valid array key (int or string).
  1599. *
  1600. * @psalm-pure
  1601. * @psalm-assert array-key $value
  1602. *
  1603. * @param mixed $value
  1604. * @param string $message
  1605. *
  1606. * @throws InvalidArgumentException
  1607. */
  1608. public static function validArrayKey($value, $message = '')
  1609. {
  1610. if (!(\is_int($value) || \is_string($value))) {
  1611. static::reportInvalidArgument(\sprintf(
  1612. $message ?: 'Expected string or integer. Got: %s',
  1613. static::typeToString($value)
  1614. ));
  1615. }
  1616. }
  1617. /**
  1618. * Does not check if $array is countable, this can generate a warning on php versions after 7.2.
  1619. *
  1620. * @param Countable|array $array
  1621. * @param int $number
  1622. * @param string $message
  1623. *
  1624. * @throws InvalidArgumentException
  1625. */
  1626. public static function count($array, $number, $message = '')
  1627. {
  1628. static::eq(
  1629. \count($array),
  1630. $number,
  1631. \sprintf(
  1632. $message ?: 'Expected an array to contain %d elements. Got: %d.',
  1633. $number,
  1634. \count($array)
  1635. )
  1636. );
  1637. }
  1638. /**
  1639. * Does not check if $array is countable, this can generate a warning on php versions after 7.2.
  1640. *
  1641. * @param Countable|array $array
  1642. * @param int|float $min
  1643. * @param string $message
  1644. *
  1645. * @throws InvalidArgumentException
  1646. */
  1647. public static function minCount($array, $min, $message = '')
  1648. {
  1649. if (\count($array) < $min) {
  1650. static::reportInvalidArgument(\sprintf(
  1651. $message ?: 'Expected an array to contain at least %2$d elements. Got: %d',
  1652. \count($array),
  1653. $min
  1654. ));
  1655. }
  1656. }
  1657. /**
  1658. * Does not check if $array is countable, this can generate a warning on php versions after 7.2.
  1659. *
  1660. * @param Countable|array $array
  1661. * @param int|float $max
  1662. * @param string $message
  1663. *
  1664. * @throws InvalidArgumentException
  1665. */
  1666. public static function maxCount($array, $max, $message = '')
  1667. {
  1668. if (\count($array) > $max) {
  1669. static::reportInvalidArgument(\sprintf(
  1670. $message ?: 'Expected an array to contain at most %2$d elements. Got: %d',
  1671. \count($array),
  1672. $max
  1673. ));
  1674. }
  1675. }
  1676. /**
  1677. * Does not check if $array is countable, this can generate a warning on php versions after 7.2.
  1678. *
  1679. * @param Countable|array $array
  1680. * @param int|float $min
  1681. * @param int|float $max
  1682. * @param string $message
  1683. *
  1684. * @throws InvalidArgumentException
  1685. */
  1686. public static function countBetween($array, $min, $max, $message = '')
  1687. {
  1688. $count = \count($array);
  1689. if ($count < $min || $count > $max) {
  1690. static::reportInvalidArgument(\sprintf(
  1691. $message ?: 'Expected an array to contain between %2$d and %3$d elements. Got: %d',
  1692. $count,
  1693. $min,
  1694. $max
  1695. ));
  1696. }
  1697. }
  1698. /**
  1699. * @psalm-pure
  1700. * @psalm-assert list $array
  1701. *
  1702. * @param mixed $array
  1703. * @param string $message
  1704. *
  1705. * @throws InvalidArgumentException
  1706. */
  1707. public static function isList($array, $message = '')
  1708. {
  1709. if (!\is_array($array)) {
  1710. static::reportInvalidArgument(
  1711. $message ?: 'Expected list - non-associative array.'
  1712. );
  1713. }
  1714. if ($array === \array_values($array)) {
  1715. return;
  1716. }
  1717. $nextKey = -1;
  1718. foreach ($array as $k => $v) {
  1719. if ($k !== ++$nextKey) {
  1720. static::reportInvalidArgument(
  1721. $message ?: 'Expected list - non-associative array.'
  1722. );
  1723. }
  1724. }
  1725. }
  1726. /**
  1727. * @psalm-pure
  1728. * @psalm-assert non-empty-list $array
  1729. *
  1730. * @param mixed $array
  1731. * @param string $message
  1732. *
  1733. * @throws InvalidArgumentException
  1734. */
  1735. public static function isNonEmptyList($array, $message = '')
  1736. {
  1737. static::isList($array, $message);
  1738. static::notEmpty($array, $message);
  1739. }
  1740. /**
  1741. * @psalm-pure
  1742. * @psalm-template T
  1743. * @psalm-param mixed|array<T> $array
  1744. * @psalm-assert array<string, T> $array
  1745. *
  1746. * @param mixed $array
  1747. * @param string $message
  1748. *
  1749. * @throws InvalidArgumentException
  1750. */
  1751. public static function isMap($array, $message = '')
  1752. {
  1753. if (
  1754. !\is_array($array) ||
  1755. \array_keys($array) !== \array_filter(\array_keys($array), '\is_string')
  1756. ) {
  1757. static::reportInvalidArgument(
  1758. $message ?: 'Expected map - associative array with string keys.'
  1759. );
  1760. }
  1761. }
  1762. /**
  1763. * @psalm-pure
  1764. * @psalm-template T
  1765. * @psalm-param mixed|array<T> $array
  1766. * @psalm-assert array<string, T> $array
  1767. * @psalm-assert !empty $array
  1768. *
  1769. * @param mixed $array
  1770. * @param string $message
  1771. *
  1772. * @throws InvalidArgumentException
  1773. */
  1774. public static function isNonEmptyMap($array, $message = '')
  1775. {
  1776. static::isMap($array, $message);
  1777. static::notEmpty($array, $message);
  1778. }
  1779. /**
  1780. * @psalm-pure
  1781. *
  1782. * @param string $value
  1783. * @param string $message
  1784. *
  1785. * @throws InvalidArgumentException
  1786. */
  1787. public static function uuid($value, $message = '')
  1788. {
  1789. $value = \str_replace(array('urn:', 'uuid:', '{', '}'), '', $value);
  1790. // The nil UUID is special form of UUID that is specified to have all
  1791. // 128 bits set to zero.
  1792. if ('00000000-0000-0000-0000-000000000000' === $value) {
  1793. return;
  1794. }
  1795. 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)) {
  1796. static::reportInvalidArgument(\sprintf(
  1797. $message ?: 'Value %s is not a valid UUID.',
  1798. static::valueToString($value)
  1799. ));
  1800. }
  1801. }
  1802. /**
  1803. * @psalm-param class-string<Throwable> $class
  1804. *
  1805. * @param Closure $expression
  1806. * @param string $class
  1807. * @param string $message
  1808. *
  1809. * @throws InvalidArgumentException
  1810. */
  1811. public static function throws(Closure $expression, $class = 'Exception', $message = '')
  1812. {
  1813. static::string($class);
  1814. $actual = 'none';
  1815. try {
  1816. $expression();
  1817. } catch (Exception $e) {
  1818. $actual = \get_class($e);
  1819. if ($e instanceof $class) {
  1820. return;
  1821. }
  1822. } catch (Throwable $e) {
  1823. $actual = \get_class($e);
  1824. if ($e instanceof $class) {
  1825. return;
  1826. }
  1827. }
  1828. static::reportInvalidArgument($message ?: \sprintf(
  1829. 'Expected to throw "%s", got "%s"',
  1830. $class,
  1831. $actual
  1832. ));
  1833. }
  1834. /**
  1835. * @throws BadMethodCallException
  1836. */
  1837. public static function __callStatic($name, $arguments)
  1838. {
  1839. if ('nullOr' === \substr($name, 0, 6)) {
  1840. if (null !== $arguments[0]) {
  1841. $method = \lcfirst(\substr($name, 6));
  1842. \call_user_func_array(array(static::class, $method), $arguments);
  1843. }
  1844. return;
  1845. }
  1846. if ('all' === \substr($name, 0, 3)) {
  1847. static::isIterable($arguments[0]);
  1848. $method = \lcfirst(\substr($name, 3));
  1849. $args = $arguments;
  1850. foreach ($arguments[0] as $entry) {
  1851. $args[0] = $entry;
  1852. \call_user_func_array(array(static::class, $method), $args);
  1853. }
  1854. return;
  1855. }
  1856. throw new BadMethodCallException('No such method: '.$name);
  1857. }
  1858. /**
  1859. * @param mixed $value
  1860. *
  1861. * @return string
  1862. */
  1863. protected static function valueToString($value)
  1864. {
  1865. if (null === $value) {
  1866. return 'null';
  1867. }
  1868. if (true === $value) {
  1869. return 'true';
  1870. }
  1871. if (false === $value) {
  1872. return 'false';
  1873. }
  1874. if (\is_array($value)) {
  1875. return 'array';
  1876. }
  1877. if (\is_object($value)) {
  1878. if (\method_exists($value, '__toString')) {
  1879. return \get_class($value).': '.self::valueToString($value->__toString());
  1880. }
  1881. if ($value instanceof DateTime || $value instanceof DateTimeImmutable) {
  1882. return \get_class($value).': '.self::valueToString($value->format('c'));
  1883. }
  1884. return \get_class($value);
  1885. }
  1886. if (\is_resource($value)) {
  1887. return 'resource';
  1888. }
  1889. if (\is_string($value)) {
  1890. return '"'.$value.'"';
  1891. }
  1892. return (string) $value;
  1893. }
  1894. /**
  1895. * @param mixed $value
  1896. *
  1897. * @return string
  1898. */
  1899. protected static function typeToString($value)
  1900. {
  1901. return \is_object($value) ? \get_class($value) : \gettype($value);
  1902. }
  1903. protected static function strlen($value)
  1904. {
  1905. if (!\function_exists('mb_detect_encoding')) {
  1906. return \strlen($value);
  1907. }
  1908. if (false === $encoding = \mb_detect_encoding($value)) {
  1909. return \strlen($value);
  1910. }
  1911. return \mb_strlen($value, $encoding);
  1912. }
  1913. /**
  1914. * @param string $message
  1915. *
  1916. * @throws InvalidArgumentException
  1917. *
  1918. * @psalm-pure this method is not supposed to perform side-effects
  1919. * @psalm-return never
  1920. */
  1921. protected static function reportInvalidArgument($message)
  1922. {
  1923. throw new InvalidArgumentException($message);
  1924. }
  1925. private function __construct()
  1926. {
  1927. }
  1928. }