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