Enum   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 7
eloc 17
dl 0
loc 47
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
B __construct() 0 26 7
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 available values during the parsing process.
8
 *
9
 * @since  2.4
10
 * @author Fabio B. Silva <[email protected]>
11
 *
12
 * @Annotation
13
 * @Attributes({
14
 *    @Attribute("value",   required = true,  type = "array"),
15
 *    @Attribute("literal", required = false, type = "array")
16
 * })
17
 */
18
final class Enum
19
{
20
    /**
21
     * @var array
22
     */
23
    public $value;
24
25
    /**
26
     * Literal target declaration.
27
     *
28
     * @var array
29
     */
30
    public $literal;
31
32
    /**
33
     * Annotation constructor.
34
     *
35
     * @param array $values
36
     *
37
     * @throws \InvalidArgumentException
38
     */
39
    public function __construct(array $values)
40
    {
41
        if ( ! isset($values['literal'])) {
42
            $values['literal'] = [];
43
        }
44
45
        foreach ($values['value'] as $var) {
46
            if( ! is_scalar($var)) {
47
                throw new \InvalidArgumentException(sprintf(
48
                    '@Enum supports only scalar values "%s" given.',
49
                    is_object($var) ? get_class($var) : gettype($var)
50
                ));
51
            }
52
        }
53
54
        foreach ($values['literal'] as $key => $var) {
55
            if( ! in_array($key, $values['value'])) {
56
                throw new \InvalidArgumentException(sprintf(
57
                    'Undefined enumerator value "%s" for literal "%s".',
58
                    $key , $var
59
                ));
60
            }
61
        }
62
63
        $this->value    = $values['value'];
64
        $this->literal  = $values['literal'];
65
    }
66
}
67