1<?php
2
3/*
4 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
5 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
6 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
7 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
8 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
9 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
10 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
11 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
12 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
13 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
14 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
15 *
16 * This software consists of voluntary contributions made by many individuals
17 * and is licensed under the MIT license. For more information, see
18 * <http://www.doctrine-project.org>.
19 */
20
21namespace Doctrine\Common\Annotations\Annotation;
22
23/**
24 * Annotation that can be used to signal to the parser
25 * to check the annotation target during the parsing process.
26 *
27 * @author Fabio B. Silva <fabio.bat.silva@gmail.com>
28 *
29 * @Annotation
30 */
31final class Target
32{
33    const TARGET_CLASS              = 1;
34    const TARGET_METHOD             = 2;
35    const TARGET_PROPERTY           = 4;
36    const TARGET_ANNOTATION         = 8;
37    const TARGET_ALL                = 15;
38
39    /**
40     * @var array
41     */
42    private static $map = array(
43        'ALL'        => self::TARGET_ALL,
44        'CLASS'      => self::TARGET_CLASS,
45        'METHOD'     => self::TARGET_METHOD,
46        'PROPERTY'   => self::TARGET_PROPERTY,
47        'ANNOTATION' => self::TARGET_ANNOTATION,
48    );
49
50    /**
51     * @var array
52     */
53    public $value;
54
55    /**
56     * Targets as bitmask.
57     *
58     * @var integer
59     */
60    public $targets;
61
62    /**
63     * Literal target declaration.
64     *
65     * @var integer
66     */
67    public $literal;
68
69    /**
70     * Annotation constructor.
71     *
72     * @param array $values
73     *
74     * @throws \InvalidArgumentException
75     */
76    public function __construct(array $values)
77    {
78        if (!isset($values['value'])){
79            $values['value'] = null;
80        }
81        if (is_string($values['value'])){
82            $values['value'] = array($values['value']);
83        }
84        if (!is_array($values['value'])){
85            throw new \InvalidArgumentException(
86                sprintf('@Target expects either a string value, or an array of strings, "%s" given.',
87                    is_object($values['value']) ? get_class($values['value']) : gettype($values['value'])
88                )
89            );
90        }
91
92        $bitmask = 0;
93        foreach ($values['value'] as $literal) {
94            if(!isset(self::$map[$literal])){
95                throw new \InvalidArgumentException(
96                    sprintf('Invalid Target "%s". Available targets: [%s]',
97                            $literal,  implode(', ', array_keys(self::$map)))
98                );
99            }
100            $bitmask |= self::$map[$literal];
101        }
102
103        $this->targets  = $bitmask;
104        $this->value    = $values['value'];
105        $this->literal  = implode(', ', $this->value);
106    }
107}
108