ValidatorChain::getValidator()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 8
Ratio 100 %

Importance

Changes 0
Metric Value
dl 8
loc 8
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 1
1
<?php
2
3
/**
4
 * Copyright 2014 Jonathan Bouzekri. All rights reserved.
5
 *
6
 * @copyright Copyright 2014 Jonathan Bouzekri <[email protected]>
7
 * @license https://github.com/jbouzekri/FileUploaderBundle/blob/master/LICENSE
8
 * @link https://github.com/jbouzekri/FileUploaderBundle
9
 */
10
11
namespace Jb\Bundle\FileUploaderBundle\Service;
12
13
use Jb\Bundle\FileUploaderBundle\Exception\JbFileUploaderException;
14
use Jb\Bundle\FileUploaderBundle\Service\Validator\AbstractValidator;
15
16
/**
17
 * ValidatorChain
18
 *
19
 * @author jobou
20
 */
21 View Code Duplication
class ValidatorChain
22
{
23
    /**
24
     * @var array
25
     */
26
    private $validators;
27
28
    /**
29
     * Constructor
30
     */
31
    public function __construct()
32
    {
33
        $this->validators = array();
34
    }
35
36
    /**
37
     * Add a validator
38
     *
39
     * @param \Jb\Bundle\FileUploaderBundle\Service\Validator\AbstractValidator $validator
40
     */
41
    public function addValidator(AbstractValidator $validator, $alias)
42
    {
43
        $this->validators[$alias] = $validator;
44
    }
45
46
    /**
47
     * Get a validator by its alias
48
     *
49
     * @param string $alias
50
     *
51
     * @return \Jb\Bundle\FileUploaderBundle\Service\Validator\AbstractValidator
52
     *
53
     * @throws \Jb\Bundle\FileUploaderBundle\Exception\JbFileUploaderException
54
     */
55
    public function getValidator($alias)
56
    {
57
        if (array_key_exists($alias, $this->validators)) {
58
            return $this->validators[$alias];
59
        }
60
61
        throw new JbFileUploaderException('Unknown validator ' . $alias);
62
    }
63
}
64