Test Failed
Push — master ( 1eee01...3150c9 )
by Gabor
02:29
created

ValidatorCollection::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 2
nc 2
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * WebHemi.
4
 *
5
 * PHP version 7.1
6
 *
7
 * @copyright 2012 - 2018 Gixx-web (http://www.gixx-web.com)
8
 * @license   https://opensource.org/licenses/MIT The MIT License (MIT)
9
 *
10
 * @link http://www.gixx-web.com
11
 */
12
declare(strict_types = 1);
13
14
namespace WebHemi\Validator;
15
16
use ArrayAccess;
17
use InvalidArgumentException;
18
19
/**
20
 * Class ValidatorCollection.
21
 */
22
class ValidatorCollection implements ValidatorCollectionInterface
23
{
24
    /**
25
     * @var array
26
     */
27
    private $container = [];
28
29
    /**
30
     * ValidatorCollection constructor.
31
     * @param ValidatorInterface ...$validatorInterfaces
32
     */
33
    public function __construct(ValidatorInterface ...$validatorInterfaces)
34
    {
35
        foreach ($validatorInterfaces as $validator) {
36
            $this->addValidator($validator);
37
        }
38
    }
39
40
    /**
41
     * Adds a new validator to the collection
42
     *
43
     * @param ValidatorInterface $validator
44
     * @return void
45
     */
46
    public function addValidator(ValidatorInterface $validator) : void
47
    {
48
        if (!$validator instanceof ValidatorInterface) {
0 ignored issues
show
introduced by
$validator is always a sub-type of WebHemi\Validator\ValidatorInterface.
Loading history...
49
            $valueType = is_object($validator) ? get_class($validator) : gettype($validator);
50
51
            throw new InvalidArgumentException(
52
                sprintf(
53
                    __METHOD__.' requires parameter 1 to be an instance of ValidatorInterface, %s given.',
54
                    $valueType
55
                ),
56
                1002
57
            );
58
        }
59
60
        $offset = get_class($validator);
61
62
        $this->container[$offset] = $validator;
63
    }
64
65
    /**
66
     * Returns a new instance of the selected validator
67
     *
68
     * @param string $validatorClass
69
     * @return ValidatorInterface
70
     */
71
    public function getValidator(string $validatorClass) : ValidatorInterface
72
    {
73
        if (empty($validatorClass)
74
            || !class_exists($validatorClass)
75
            || !in_array(ValidatorInterface::class, class_implements($validatorClass))
76
        ) {
77
            throw new InvalidArgumentException(
78
                sprintf(
79
                    __METHOD__.' requires parameter 1 to be a valid class that implements the %s, %s given.',
80
                    ValidatorInterface::class,
81
                    gettype($validatorClass)
82
                ),
83
                1003
84
            );
85
        }
86
87
        return clone $this->container[$validatorClass];
88
    }
89
}
90