bulton-fr /
annotation
| 1 | <?php |
||
| 2 | |||
| 3 | namespace BultonFr\Annotation; |
||
| 4 | |||
| 5 | use Exception; |
||
| 6 | use BultonFr\Annotation\Parsers\AbstractManyParser; |
||
| 7 | |||
| 8 | /** |
||
| 9 | * Main Reader class. |
||
| 10 | * Take a class name, use parser to obtain annotations, and have methods |
||
| 11 | * to obtains annotations objects. |
||
| 12 | * |
||
| 13 | * @package BultonFr\Annotation |
||
| 14 | */ |
||
| 15 | class Reader |
||
| 16 | { |
||
| 17 | /** |
||
| 18 | * @const EXCEP_PARSE_NOT_EXECUTED Exception code if annotation is asked |
||
| 19 | * before a call to parse() |
||
| 20 | */ |
||
| 21 | const EXCEP_PARSE_NOT_EXECUTED = 102001; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * @const EXCEP_METHOD_NOT_EXIST Exception code if user ask annotations |
||
| 25 | * for a specific method which not exist |
||
| 26 | */ |
||
| 27 | const EXCEP_METHOD_NOT_EXIST = 102002; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @const EXCEP_PROPERTY_NOT_EXIST Exception code if user ask annotations |
||
| 31 | * for a specific property which not exist |
||
| 32 | */ |
||
| 33 | const EXCEP_PROPERTY_NOT_EXIST = 102003; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * The full class name to read |
||
| 37 | * |
||
| 38 | * @var string |
||
| 39 | */ |
||
| 40 | protected $className = ''; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * The parser manager system |
||
| 44 | * |
||
| 45 | * @var \BultonFr\Annotation\ParserManager |
||
| 46 | */ |
||
| 47 | protected $parserManager; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Construct |
||
| 51 | * |
||
| 52 | * @param string $className The class name |
||
| 53 | */ |
||
| 54 | public function __construct(string $className) |
||
| 55 | { |
||
| 56 | 1 | $this->className = $className; |
|
| 57 | 1 | $this->parserManager = new ParserManager($this); |
|
| 58 | 1 | } |
|
| 59 | |||
| 60 | /** |
||
| 61 | * Get the full class name to read |
||
| 62 | * |
||
| 63 | * @return string |
||
| 64 | */ |
||
| 65 | public function getClassName(): string |
||
| 66 | { |
||
| 67 | 1 | return $this->className; |
|
| 68 | } |
||
| 69 | |||
| 70 | /** |
||
| 71 | * Get the parser manager system |
||
| 72 | * |
||
| 73 | * @return \BultonFr\Annotation\ParserManager |
||
| 74 | */ |
||
| 75 | public function getParserManager(): ParserManager |
||
| 76 | { |
||
| 77 | 1 | return $this->parserManager; |
|
| 78 | } |
||
| 79 | |||
| 80 | /** |
||
| 81 | * Run all parser |
||
| 82 | * |
||
| 83 | * @return void |
||
| 84 | */ |
||
| 85 | public function parse() |
||
| 86 | { |
||
| 87 | 1 | $this->parserManager->run(); |
|
| 88 | 1 | } |
|
| 89 | |||
| 90 | /** |
||
| 91 | * Return an array with all annotations objects declared on the class level |
||
| 92 | * |
||
| 93 | * @return array |
||
| 94 | */ |
||
| 95 | public function obtainClassAnnotList(): array |
||
| 96 | { |
||
| 97 | 1 | $parserList = $this->parserManager->getParserList(); |
|
| 98 | 1 | if (isset($parserList['class']) === false) { |
|
| 99 | 1 | throw new Exception( |
|
| 100 | 1 | 'Please call parse() method before obtain annotations', |
|
| 101 | 1 | static::EXCEP_PARSE_NOT_EXECUTED |
|
| 102 | ); |
||
| 103 | } |
||
| 104 | |||
| 105 | 1 | return $parserList['class']->getAnnotList(); |
|
| 106 | } |
||
| 107 | |||
| 108 | /** |
||
| 109 | * Return an object which contain all methods parser |
||
| 110 | * |
||
| 111 | * @return BultonFr\Annotation\Parsers\AbstractManyParser |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 112 | */ |
||
| 113 | public function obtainMethodsList(): AbstractManyParser |
||
| 114 | { |
||
| 115 | 1 | $parserList = $this->parserManager->getParserList(); |
|
| 116 | 1 | if (isset($parserList['methods']) === false) { |
|
| 117 | 1 | throw new Exception( |
|
| 118 | 1 | 'Please call parse() method before obtain annotations', |
|
| 119 | 1 | static::EXCEP_PARSE_NOT_EXECUTED |
|
| 120 | ); |
||
| 121 | } |
||
| 122 | |||
| 123 | 1 | return $parserList['methods']; |
|
| 124 | } |
||
| 125 | |||
| 126 | /** |
||
| 127 | * Return an object which contain all properties parser |
||
| 128 | * |
||
| 129 | * @return BultonFr\Annotation\Parsers\AbstractManyParser |
||
| 130 | */ |
||
| 131 | public function obtainPropertiesList(): AbstractManyParser |
||
| 132 | { |
||
| 133 | 1 | $parserList = $this->parserManager->getParserList(); |
|
| 134 | 1 | if (isset($parserList['properties']) === false) { |
|
| 135 | 1 | throw new Exception( |
|
| 136 | 1 | 'Please call parse() method before obtain annotations', |
|
| 137 | 1 | static::EXCEP_PARSE_NOT_EXECUTED |
|
| 138 | ); |
||
| 139 | } |
||
| 140 | |||
| 141 | 1 | return $parserList['properties']; |
|
| 142 | } |
||
| 143 | |||
| 144 | /** |
||
| 145 | * Return an array with all annotations objects declared for a method |
||
| 146 | * |
||
| 147 | * @param string $methodName The method name |
||
| 148 | * |
||
| 149 | * @return array |
||
| 150 | * |
||
| 151 | * @throws Exception If the method has not been found |
||
| 152 | */ |
||
| 153 | public function obtainMethodAnnotList(string $methodName): array |
||
| 154 | { |
||
| 155 | 1 | $methodList = $this->obtainMethodsList()->getList(); |
|
| 156 | 1 | if (array_key_exists($methodName, $methodList) === false) { |
|
| 157 | 1 | throw new Exception( |
|
| 158 | 1 | 'Method '.$methodName.' not exist in the list', |
|
| 159 | 1 | static::EXCEP_METHOD_NOT_EXIST |
|
| 160 | ); |
||
| 161 | } |
||
| 162 | |||
| 163 | 1 | $methodParser = $methodList[$methodName]; |
|
| 164 | 1 | return $methodParser->getAnnotList(); |
|
| 165 | } |
||
| 166 | |||
| 167 | /** |
||
| 168 | * Return an array with all annotations objects declared for a property |
||
| 169 | * |
||
| 170 | * @param string $propertyName The property name |
||
| 171 | * |
||
| 172 | * @return array |
||
| 173 | * |
||
| 174 | * @throws Exception If the property has not been found |
||
| 175 | */ |
||
| 176 | public function obtainPropertyAnnotList(string $propertyName): array |
||
| 177 | { |
||
| 178 | 1 | $propertiesList = $this->obtainPropertiesList()->getList(); |
|
| 179 | 1 | if (array_key_exists($propertyName, $propertiesList) === false) { |
|
| 180 | 1 | throw new Exception( |
|
| 181 | 1 | 'Method '.$propertyName.' not exist in the list', |
|
| 182 | 1 | static::EXCEP_PROPERTY_NOT_EXIST |
|
| 183 | ); |
||
| 184 | } |
||
| 185 | |||
| 186 | 1 | $propertyParser = $propertiesList[$propertyName]; |
|
| 187 | 1 | return $propertyParser->getAnnotList(); |
|
| 188 | } |
||
| 189 | } |
||
| 190 |