dimtrovich /
validation
| 1 | <?php |
||
| 2 | |||
| 3 | /** |
||
| 4 | * This file is part of Dimtrovich/Validation. |
||
| 5 | * |
||
| 6 | * (c) 2023 Dimitri Sitchet Tomkeu <[email protected]> |
||
| 7 | * |
||
| 8 | * For the full copyright and license information, please view |
||
| 9 | * the LICENSE file that was distributed with this source code. |
||
| 10 | */ |
||
| 11 | |||
| 12 | namespace Dimtrovich\Validation; |
||
| 13 | |||
| 14 | use Dimtrovich\Validation\Exceptions\ValidationException; |
||
| 15 | use InvalidArgumentException; |
||
| 16 | use Rakit\Validation\Rule; |
||
|
0 ignored issues
–
show
|
|||
| 17 | |||
| 18 | class Validator |
||
| 19 | { |
||
| 20 | /** |
||
| 21 | * Class to use for validation. |
||
| 22 | * Useful if we have modified certain behaviors in the validation class (adding a new validator for example) |
||
| 23 | * |
||
| 24 | * @var class-string<Validation> |
||
|
0 ignored issues
–
show
|
|||
| 25 | */ |
||
| 26 | protected static string $validationClass = Validation::class; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * Initializes the validation process |
||
| 30 | */ |
||
| 31 | public static function make(array $data, array $rules, array $messages = [], array $alias = []): Validation |
||
| 32 | { |
||
| 33 | 132 | self::ensureValidValidationClass(); |
|
| 34 | |||
| 35 | 132 | $instance = new static::$validationClass(); |
|
| 36 | |||
| 37 | 132 | $instance->data($data); |
|
| 38 | 132 | $instance->rules($rules); |
|
| 39 | 132 | $instance->messages($messages); |
|
| 40 | 132 | $instance->alias($alias); |
|
| 41 | |||
| 42 | 132 | return $instance; |
|
| 43 | } |
||
| 44 | |||
| 45 | /** |
||
| 46 | * Create and return a validator on the fly |
||
| 47 | * |
||
| 48 | * @return Rule |
||
| 49 | */ |
||
| 50 | public static function rule(string $rule) |
||
| 51 | { |
||
| 52 | 20 | self::ensureValidValidationClass(); |
|
| 53 | |||
| 54 | 20 | $args = func_get_args(); |
|
| 55 | 20 | $rule = array_shift($args); |
|
| 56 | 20 | $params = $args; |
|
| 57 | |||
| 58 | 20 | $validator = static::$validationClass::instance()->getValidator($rule); |
|
| 59 | if (! ($validator instanceof Rule)) { |
||
| 60 | 20 | throw ValidationException::ruleNotFound($rule); |
|
| 61 | } |
||
| 62 | |||
| 63 | 20 | $clonedValidator = clone $validator; |
|
| 64 | 20 | $clonedValidator->fillParameters($params); |
|
| 65 | |||
| 66 | 20 | return $clonedValidator; |
|
| 67 | } |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Make sure the class to use for validation is a subclass of Validation |
||
| 71 | */ |
||
| 72 | private static function ensureValidValidationClass(): void |
||
| 73 | { |
||
| 74 | if (! is_a(static::$validationClass, Validation::class, true)) { |
||
| 75 | 132 | throw new InvalidArgumentException('Static property $validationClass must be a subclass of ' . Validation::class); |
|
| 76 | } |
||
| 77 | } |
||
| 78 | } |
||
| 79 |
Let?s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let?s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: