Target::__construct()   B
last analyzed

Complexity

Conditions 7
Paths 16

Size

Total Lines 30
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 18
dl 0
loc 30
rs 8.8333
c 0
b 0
f 0
cc 7
nc 16
nop 1
1
<?php
2
3
namespace Doctrine\Common\Annotations\Annotation;
4
5
/**
6
 * Annotation that can be used to signal to the parser
7
 * to check the annotation target during the parsing process.
8
 *
9
 * @author Fabio B. Silva <[email protected]>
10
 *
11
 * @Annotation
12
 */
13
final class Target
14
{
15
    const TARGET_CLASS              = 1;
16
    const TARGET_METHOD             = 2;
17
    const TARGET_PROPERTY           = 4;
18
    const TARGET_ANNOTATION         = 8;
19
    const TARGET_ALL                = 15;
20
21
    /**
22
     * @var array
23
     */
24
    private static $map = [
25
        'ALL'        => self::TARGET_ALL,
26
        'CLASS'      => self::TARGET_CLASS,
27
        'METHOD'     => self::TARGET_METHOD,
28
        'PROPERTY'   => self::TARGET_PROPERTY,
29
        'ANNOTATION' => self::TARGET_ANNOTATION,
30
    ];
31
32
    /**
33
     * @var array
34
     */
35
    public $value;
36
37
    /**
38
     * Targets as bitmask.
39
     *
40
     * @var integer
41
     */
42
    public $targets;
43
44
    /**
45
     * Literal target declaration.
46
     *
47
     * @var string
48
     */
49
    public $literal;
50
51
    /**
52
     * Annotation constructor.
53
     *
54
     * @param array $values
55
     *
56
     * @throws \InvalidArgumentException
57
     */
58
    public function __construct(array $values)
59
    {
60
        if (!isset($values['value'])){
61
            $values['value'] = null;
62
        }
63
        if (is_string($values['value'])){
64
            $values['value'] = [$values['value']];
65
        }
66
        if (!is_array($values['value'])){
67
            throw new \InvalidArgumentException(
68
                sprintf('@Target expects either a string value, or an array of strings, "%s" given.',
69
                    is_object($values['value']) ? get_class($values['value']) : gettype($values['value'])
70
                )
71
            );
72
        }
73
74
        $bitmask = 0;
75
        foreach ($values['value'] as $literal) {
76
            if(!isset(self::$map[$literal])){
77
                throw new \InvalidArgumentException(
78
                    sprintf('Invalid Target "%s". Available targets: [%s]',
79
                            $literal,  implode(', ', array_keys(self::$map)))
80
                );
81
            }
82
            $bitmask |= self::$map[$literal];
83
        }
84
85
        $this->targets  = $bitmask;
86
        $this->value    = $values['value'];
87
        $this->literal  = implode(', ', $this->value);
88
    }
89
}
90