Issues (24)

src/Annotations/AbstractAnnotation.php (1 issue)

1
<?php
2
3
namespace BultonFr\Annotation\Annotations;
4
5
use Exception;
6
7
use BultonFr\Annotation\Parsers\Annotations\Info;
8
use BultonFr\Annotation\Reader;
9
10
/**
11
 * Abstract class for all annotation dedicated object
12
 *
13
 * @package BultonFr\Annotation
14
 */
15
abstract class AbstractAnnotation
16
{
17
    /**
18
     * @const EXCEP_KEY_NOT_EXIST Exception code if a asked attribute not exist
19
     *
20
     * @see README.md for code format
21
     */
22
    const EXCEP_KEY_NOT_EXIST = 201001;
23
    
24
    /**
25
     * @var Reader The main Reader instance
26
     */
27
    protected $reader;
28
    
29
    /**
30
     * The name of the item which belongs annotations
31
     *
32
     * @var string
33
     */
34
    protected $itemName;
35
    
36
    /**
37
     * @var \BultonFr\Annotation\Parsers\Annotations\Info The annotation info
38
     * object which provide data about the annotation
39
     */
40
    protected $info;
41
    
42
    /**
43
     * Construct
44
     *
45
     * @param Reader $reader
46
     * @param string $itemName
47
     * @param Info $Info
48
     */
49
    public function __construct(
50
        Reader $reader,
51
        string $itemName,
52
        Info $info
53
    ) {
54 1
        $this->reader   = $reader;
55 1
        $this->itemName = $itemName;
56 1
        $this->info     = $info;
57
        
58 1
        $this->parseValue();
59 1
    }
60
    
61
    /**
62
     * Get the main Reader instance
63
     *
64
     * @return Reader
65
     */
66
    public function getReader(): Reader
67
    {
68 1
        return $this->reader;
69
    }
70
71
    /**
72
     * Get the name of the item which belongs annotations
73
     *
74
     * @return void
75
     */
76
    public function getItemName()
77
    {
78 1
        return $this->itemName;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->itemName returns the type string which is incompatible with the documented return type void.
Loading history...
79
    }
80
81
    /**
82
     * Get the annotation info object which provide data about the annotation
83
     *
84
     * @return Info
85
     */
86
    public function getInfo(): Info
87
    {
88 1
        return $this->info;
89
    }
90
91
    /**
92
     * Parse values to populate properties
93
     *
94
     * @return void
95
     */
96
    abstract protected function parseValue();
97
    
98
    /**
99
     * Obtain the value of a key/attribute
100
     *
101
     * @param string $askedKey The asked key/attribute
102
     *
103
     * @return mixed
104
     *
105
     * @throws Exception If the key not exist
106
     */
107
    protected function obtainValueKey(string $askedKey)
108
    {
109 1
        $valueList = $this->info->getValues();
110
        
111 1
        if (array_key_exists($askedKey, $valueList) === false) {
112 1
            throw new Exception(
113 1
                'The key value '.$askedKey.' not exist into the annotation '.$this->info->getName(),
114 1
                self::EXCEP_KEY_NOT_EXIST
115
            );
116
        }
117
        
118 1
        return $valueList[$askedKey];
119
    }
120
    
121
    /**
122
     * Check if a key/attribute exist
123
     *
124
     * @param string $askedKey The asked key/attribute to check
125
     *
126
     * @return boolean
127
     */
128
    protected function hasValueKey(string $askedKey): bool
129
    {
130 1
        $valueList = $this->info->getValues();
131
        
132 1
        return array_key_exists($askedKey, $valueList);
133
    }
134
}
135