Passed
Push — develop ( 48165e...a94b80 )
by nguereza
02:15
created

Validator::reset()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Platine Upload
5
 *
6
 * Platine Upload provides a flexible file uploads with extensible
7
 * validation and storage strategies.
8
 *
9
 * This content is released under the MIT License (MIT)
10
 *
11
 * Copyright (c) 2020 Platine Upload
12
 *
13
 * @author      Josh Lockhart <[email protected]>
14
 * @copyright   2012 Josh Lockhart
15
 * @link        http://www.joshlockhart.com
16
 * @version     2.0.0
17
 *
18
 * Permission is hereby granted, free of charge, to any person obtaining a copy
19
 * of this software and associated documentation files (the "Software"), to deal
20
 * in the Software without restriction, including without limitation the rights
21
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
22
 * copies of the Software, and to permit persons to whom the Software is
23
 * furnished to do so, subject to the following conditions:
24
 *
25
 * The above copyright notice and this permission notice shall be included in all
26
 * copies or substantial portions of the Software.
27
 *
28
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
29
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
30
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
31
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
32
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
33
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
34
 * SOFTWARE.
35
 */
36
37
/**
38
 *  @file Validator.php
39
 *
40
 *  The Upload Validator class
41
 *
42
 *  @package    Platine\Upload\Validator
43
 *  @author Platine Developers Team
44
 *  @copyright  Copyright (c) 2020
45
 *  @license    http://opensource.org/licenses/MIT  MIT License
46
 *  @link   http://www.iacademy.cf
47
 *  @version 1.0.0
48
 *  @filesource
49
 */
50
51
declare(strict_types=1);
52
53
namespace Platine\Upload\Validator;
54
55
use InvalidArgumentException;
56
57
/**
58
 * Class Validator
59
 * @package Platine\Upload\Validator
60
 */
61
class Validator
62
{
63
    /**
64
     * The validate rules
65
     * @var array<int, RuleInterface>
66
     */
67
    protected array $rules = [];
68
69
    /**
70
     * Create new instance
71
     * @param array<int, RuleInterface> $rules
72
     */
73
    public function __construct(array $rules = [])
74
    {
75
        $this->rules = $rules;
76
    }
77
78
    /**
79
     * Add validation rule
80
     * @param RuleInterface $rule
81
     * @return $this
82
     */
83
    public function addRule(RuleInterface $rule): self
84
    {
85
        $this->rules[] = $rule;
86
87
        return $this;
88
    }
89
90
    /**
91
     * Add array of rules
92
     * @param array<int, RuleInterface> $rules
93
     * @return $this
94
     */
95
    public function addRules(array $rules): self
96
    {
97
        foreach ($rules as $rule) {
98
            if (!$rule instanceof RuleInterface) {
99
                throw new InvalidArgumentException(sprintf(
100
                    'Each rule must be an instance of [%s]',
101
                    RuleInterface::class
102
                ));
103
            }
104
105
            $this->addRule($rule);
106
        }
107
108
        return $this;
109
    }
110
111
    /**
112
     * Return the validation rules
113
     * @return array<int, RuleInterface>
114
     */
115
    public function getRules(): array
116
    {
117
        return $this->rules;
118
    }
119
120
    /**
121
     * Reset the validation instance
122
     * @return $this
123
     */
124
    public function reset(): self
125
    {
126
        $this->rules = [];
127
128
        return $this;
129
    }
130
}
131