-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathValidatorFactory.php
More file actions
58 lines (50 loc) · 1.66 KB
/
ValidatorFactory.php
File metadata and controls
58 lines (50 loc) · 1.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
<?php
/**
* This file is part of the Vection-Framework project.
* Visit project at https://github.com/Vection-Framework/Vection
*
* (c) Vection-Framework <vection@appsdock.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace Vection\Component\Validator;
use ReflectionClass;
use RuntimeException;
use Vection\Contracts\Validator\ValidatorInterface;
/**
* Class ValidatorFactory
*
* @package Vection\Component\Validator
* @author David Lung <vection@davidlung.de>
*/
class ValidatorFactory
{
/**
* @param string $name
* @param mixed[] $constraints
*
* @return ValidatorInterface
*/
public function create(string $name, array $constraints = []): ValidatorInterface
{
$validatorClassName = __NAMESPACE__ .'\\Validator\\'. ucfirst($name);
if (!class_exists($validatorClassName)) {
throw new RuntimeException('Validator does not exists - '.$validatorClassName);
}
if (is_int(array_key_first($constraints))) {
$parameters = array_values($constraints);
} else {
$parameters = [];
if ($constructor = (new ReflectionClass($validatorClassName))->getConstructor()) {
foreach ($constructor->getParameters() as $constructorParameter) {
if (array_key_exists($constructorParameter->name, $constraints)) {
$parameters[] = $constraints[$constructorParameter->name];
}
}
}
}
return new $validatorClassName(...$parameters);
}
}