Completed
Push — master ( b5c7fa...23b509 )
by Taosikai
13:59
created

Validator::validate()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
c 0
b 0
f 0
rs 9.9666
cc 3
nc 3
nop 1
1
<?php
2
3
/*
4
 * This file is part of the slince/upload package.
5
 *
6
 * (c) Slince <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Slince\Upload;
13
14
use Slince\Upload\Constraint\ConstraintInterface;
15
use Slince\Upload\Exception\ConstraintException;
16
use Symfony\Component\HttpFoundation\File\UploadedFile;
17
18
final class Validator
19
{
20
    /**
21
     * Array of constraints
22
     *
23
     * @var ConstraintInterface[]
24
     */
25
    protected $constraints = [];
26
27
    /**
28
     * Add a constraint
29
     * @param ConstraintInterface $constraint
30
     */
31
    public function addConstraint(ConstraintInterface $constraint)
32
    {
33
        $this->constraints[] = $constraint;
34
    }
35
36
    /**
37
     * Validate the file
38
     *
39
     * @param UploadedFile $file
40
     * @return true
41
     * @throws ConstraintException
42
     */
43
    public function validate(UploadedFile $file)
44
    {
45
        foreach ($this->constraints as $constraint) {
46
            if (!$constraint->validate($file)) {
47
                throw new ConstraintException($constraint, $file);
48
            }
49
        }
50
        return true;
51
    }
52
}