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 kontakt@beberlei.de so I can send you a copy immediately.
13 */
14
15namespace Assert;
16
17/**
18 * AssertionChain factory.
19 */
20abstract 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     * @param string|null $defaultPropertyPath
37     *
38     * @return AssertionChain
39     *
40     * @example
41     *
42     *  Assert::that($value)->notEmpty()->integer();
43     *  Assert::that($value)->nullOr()->string()->startsWith("Foo");
44     *
45     * The assertion chain can be stateful, that means be careful when you reuse
46     * it. You should never pass around the chain.
47     */
48    public static function that($value, $defaultMessage = null, string $defaultPropertyPath = null): AssertionChain
49    {
50        $assertionChain = new AssertionChain($value, $defaultMessage, $defaultPropertyPath);
51
52        return $assertionChain->setAssertionClassName(static::$assertionClass);
53    }
54
55    /**
56     * Start validation on a set of values, returns {@link AssertionChain}.
57     *
58     * @param mixed $values
59     * @param string|callable|null $defaultMessage
60     * @param string|null $defaultPropertyPath
61     *
62     * @return AssertionChain
63     */
64    public static function thatAll($values, $defaultMessage = null, string $defaultPropertyPath = null): AssertionChain
65    {
66        return static::that($values, $defaultMessage, $defaultPropertyPath)->all();
67    }
68
69    /**
70     * Start validation and allow NULL, returns {@link AssertionChain}.
71     *
72     * @param mixed $value
73     * @param string|callable|null $defaultMessage
74     * @param string|null $defaultPropertyPath
75     *
76     * @return AssertionChain
77     */
78    public static function thatNullOr($value, $defaultMessage = null, string $defaultPropertyPath = null): AssertionChain
79    {
80        return static::that($value, $defaultMessage, $defaultPropertyPath)->nullOr();
81    }
82
83    /**
84     * Create a lazy assertion object.
85     *
86     * @return LazyAssertion
87     */
88    public static function lazy(): LazyAssertion
89    {
90        $lazyAssertion = new LazyAssertion();
91
92        return $lazyAssertion
93            ->setAssertClass(\get_called_class())
94            ->setExceptionClass(static::$lazyAssertionExceptionClass);
95    }
96}
97