|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace AEngine\Orchid\Annotation\Annotated; |
| 4 | + |
| 5 | +use AEngine\Orchid\Annotation\Annotation; |
| 6 | +use AEngine\Orchid\Annotation\AnnotationReader; |
| 7 | +use AEngine\Orchid\Annotation\Interfaces\AnnotatedInterface; |
| 8 | +use ReflectionClass; |
| 9 | +use ReflectionException; |
| 10 | +use ReflectionMethod; |
| 11 | +use ReflectionProperty; |
| 12 | + |
| 13 | +class AnnotatedReflectionClass extends ReflectionClass implements AnnotatedInterface |
| 14 | +{ |
| 15 | + protected $annotations; |
| 16 | + protected $methods; |
| 17 | + protected $properties; |
| 18 | + |
| 19 | + /** |
| 20 | + * Return element type |
| 21 | + * |
| 22 | + * @return string |
| 23 | + */ |
| 24 | + public function getAnnotatedElementType() |
| 25 | + { |
| 26 | + if ($this->isSubClassOf(Annotation::class)) { |
| 27 | + return 'ANNOTATION'; |
| 28 | + } |
| 29 | + |
| 30 | + return 'CLASS'; |
| 31 | + } |
| 32 | + |
| 33 | + /** |
| 34 | + * Return all annotations |
| 35 | + * |
| 36 | + * @return array |
| 37 | + * @throws ReflectionException |
| 38 | + */ |
| 39 | + public function getAnnotations() |
| 40 | + { |
| 41 | + if ($this->annotations == null) { |
| 42 | + $this->annotations = AnnotationReader::parse($this); |
| 43 | + } |
| 44 | + |
| 45 | + return $this->annotations; |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * Return annotation by strict name or array of annotations |
| 50 | + * |
| 51 | + * @param string $name |
| 52 | + * @param bool $strict |
| 53 | + * |
| 54 | + * @return array |
| 55 | + * @throws ReflectionException |
| 56 | + */ |
| 57 | + public function getAnnotation($name, $strict = true) |
| 58 | + { |
| 59 | + $annotations = $this->getAnnotations(); |
| 60 | + |
| 61 | + if ($strict) { |
| 62 | + if (isset($annotations[$name])) { |
| 63 | + return $annotations[$name]; |
| 64 | + } |
| 65 | + |
| 66 | + return null; |
| 67 | + } |
| 68 | + |
| 69 | + foreach ($annotations as $annotation => $obj) { |
| 70 | + if (strpos($annotation, $name) === false) { |
| 71 | + unset($annotations[$annotation]); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + return $annotations; |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * Check has annotations |
| 80 | + * |
| 81 | + * @param string $annotationClass |
| 82 | + * |
| 83 | + * @return bool |
| 84 | + * @throws ReflectionException |
| 85 | + */ |
| 86 | + public function hasAnnotation($annotationClass) |
| 87 | + { |
| 88 | + return !!$this->getAnnotation($annotationClass); |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * Return class methods |
| 93 | + * |
| 94 | + * @param null $filter |
| 95 | + * |
| 96 | + * @return AnnotatedReflectionMethod[] |
| 97 | + * @throws ReflectionException |
| 98 | + */ |
| 99 | + public function getMethods($filter = null) |
| 100 | + { |
| 101 | + if ($this->methods == null) { |
| 102 | + $methods = parent::getMethods(); |
| 103 | + $this->methods = []; |
| 104 | + foreach ($methods as $method) { |
| 105 | + $this->methods[$method->getName()] = new AnnotatedReflectionMethod($this->getName(), $method->getName()); |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + return $this->methods; |
| 110 | + } |
| 111 | + |
| 112 | + /** |
| 113 | + * Return class method by name |
| 114 | + * |
| 115 | + * @param string $name |
| 116 | + * |
| 117 | + * @return null|ReflectionMethod |
| 118 | + * @throws ReflectionException |
| 119 | + */ |
| 120 | + public function getMethod($name) |
| 121 | + { |
| 122 | + $all = $this->getMethods(); |
| 123 | + |
| 124 | + return (isset($all[$name]) ? $all[$name] : null); |
| 125 | + } |
| 126 | + |
| 127 | + /** |
| 128 | + * Check has method by name |
| 129 | + * |
| 130 | + * @param string $name |
| 131 | + * |
| 132 | + * @return bool |
| 133 | + * @throws ReflectionException |
| 134 | + */ |
| 135 | + public function hasMethod($name) |
| 136 | + { |
| 137 | + return $this->getMethod($name) != null; |
| 138 | + } |
| 139 | + |
| 140 | + /** |
| 141 | + * Return class properties |
| 142 | + * |
| 143 | + * @param null $filter |
| 144 | + * |
| 145 | + * @return AnnotatedReflectionProperty[] |
| 146 | + * @throws ReflectionException |
| 147 | + */ |
| 148 | + public function getProperties($filter = null) |
| 149 | + { |
| 150 | + if ($this->properties == null) { |
| 151 | + $properties = parent::getProperties(); |
| 152 | + $this->properties = []; |
| 153 | + foreach ($properties as $property) { |
| 154 | + $this->properties[$property->getName()] = new AnnotatedReflectionProperty($this->getName(), $property->getName()); |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + return $this->properties; |
| 159 | + } |
| 160 | + |
| 161 | + /** |
| 162 | + * Return class properties by name |
| 163 | + * |
| 164 | + * @param string $name |
| 165 | + * |
| 166 | + * @return null|ReflectionProperty |
| 167 | + * @throws ReflectionException |
| 168 | + */ |
| 169 | + public function getProperty($name) |
| 170 | + { |
| 171 | + $all = $this->getProperties(); |
| 172 | + |
| 173 | + return (isset($all[$name]) ? $all[$name] : null); |
| 174 | + } |
| 175 | + |
| 176 | + /** |
| 177 | + * Check has property by name |
| 178 | + * |
| 179 | + * @param string $name |
| 180 | + * |
| 181 | + * @return bool |
| 182 | + * @throws ReflectionException |
| 183 | + */ |
| 184 | + public function hasProperty($name) |
| 185 | + { |
| 186 | + return $this->getProperty($name) != null; |
| 187 | + } |
| 188 | +} |
0 commit comments