Assert.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. /**
  3. * Assert
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the MIT license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * If you did not receive a copy of the license and are unable to
  10. * obtain it through the world-wide-web, please send an email
  11. * to kontakt@beberlei.de so I can send you a copy immediately.
  12. */
  13. namespace Assert;
  14. /**
  15. * AssertionChain factory.
  16. */
  17. abstract class Assert
  18. {
  19. /** @var string */
  20. protected static $lazyAssertionExceptionClass = 'Assert\LazyAssertionException';
  21. /** @var string */
  22. protected static $assertionClass = 'Assert\Assertion';
  23. /**
  24. * Start validation on a value, returns {@link AssertionChain}.
  25. *
  26. * The invocation of this method starts an assertion chain
  27. * that is happening on the passed value.
  28. *
  29. * @example
  30. *
  31. * Assert::that($value)->notEmpty()->integer();
  32. * Assert::that($value)->nullOr()->string()->startsWith("Foo");
  33. *
  34. * The assertion chain can be stateful, that means be careful when you reuse
  35. * it. You should never pass around the chain.
  36. *
  37. * @param mixed $value
  38. * @param string $defaultMessage
  39. * @param string $defaultPropertyPath
  40. *
  41. * @return \Assert\AssertionChain
  42. */
  43. public static function that($value, $defaultMessage = null, $defaultPropertyPath = null)
  44. {
  45. $assertionChain = new AssertionChain($value, $defaultMessage, $defaultPropertyPath);
  46. return $assertionChain
  47. ->setAssertionClassName(static::$assertionClass)
  48. ;
  49. }
  50. /**
  51. * Start validation on a set of values, returns {@link AssertionChain}.
  52. *
  53. * @param mixed $values
  54. * @param string $defaultMessage
  55. * @param string $defaultPropertyPath
  56. *
  57. * @return \Assert\AssertionChain
  58. */
  59. public static function thatAll($values, $defaultMessage = null, $defaultPropertyPath = null)
  60. {
  61. return static::that($values, $defaultMessage, $defaultPropertyPath)->all();
  62. }
  63. /**
  64. * Start validation and allow NULL, returns {@link AssertionChain}.
  65. *
  66. * @param mixed $value
  67. * @param string $defaultMessage
  68. * @param string $defaultPropertyPath
  69. *
  70. * @return \Assert\AssertionChain
  71. */
  72. public static function thatNullOr($value, $defaultMessage = null, $defaultPropertyPath = null)
  73. {
  74. return static::that($value, $defaultMessage, $defaultPropertyPath)->nullOr();
  75. }
  76. /**
  77. * Create a lazy assertion object.
  78. *
  79. * @return \Assert\LazyAssertion
  80. */
  81. public static function lazy()
  82. {
  83. $lazyAssertion = new LazyAssertion();
  84. return $lazyAssertion
  85. ->setAssertClass(\get_called_class())
  86. ->setExceptionClass(static::$lazyAssertionExceptionClass)
  87. ;
  88. }
  89. }