/Front_End/vendor/webmozart/assert/src/Assert.php

https://gitlab.com/Sigpot/AirSpot · PHP · 903 lines · 669 code · 98 blank · 136 comment · 82 complexity · 1dfd4c1c5573ba303d5fc7118d7a915b MD5 · raw file

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