|
23 | 23 | namespace ElementaryFramework\Annotations\Standard;
|
24 | 24 |
|
25 | 25 | use ElementaryFramework\Annotations\Annotation;
|
| 26 | +use ElementaryFramework\Annotations\AnnotationException; |
| 27 | +use ElementaryFramework\Annotations\AnnotationFile; |
| 28 | +use ElementaryFramework\Annotations\IAnnotationFileAware; |
| 29 | +use ElementaryFramework\Annotations\IAnnotationParser; |
26 | 30 |
|
27 | 31 | /**
|
28 | 32 | * Defines a magic/virtual method
|
29 | 33 | *
|
30 |
| - * @todo implement this |
| 34 | + * @usage('class' => true, 'inherited' => true, 'multiple' => true) |
31 | 35 | */
|
32 |
| -class MethodAnnotation extends Annotation |
| 36 | +class MethodAnnotation extends Annotation implements IAnnotationParser, IAnnotationFileAware |
33 | 37 | {
|
| 38 | + /** |
| 39 | + * Specifies the method return type. |
| 40 | + * |
| 41 | + * @var string |
| 42 | + */ |
| 43 | + public $type; |
| 44 | + |
| 45 | + /** |
| 46 | + * Specifies the method name. |
| 47 | + * |
| 48 | + * @var string |
| 49 | + */ |
| 50 | + public $name; |
| 51 | + |
| 52 | + /** |
| 53 | + * Specifies the parameters of the method. |
| 54 | + * |
| 55 | + * @var array |
| 56 | + */ |
| 57 | + public $parameters; |
| 58 | + |
| 59 | + /** |
| 60 | + * Specifies the method description. |
| 61 | + * |
| 62 | + * @var string |
| 63 | + */ |
| 64 | + public $description; |
| 65 | + |
| 66 | + /** |
| 67 | + * Annotation file. |
| 68 | + * |
| 69 | + * @var AnnotationFile |
| 70 | + */ |
| 71 | + protected $file; |
| 72 | + |
| 73 | + /** |
| 74 | + * Parse the standard PHP-DOC "method" annotation. |
| 75 | + * |
| 76 | + * @param string $value The raw string value of the Annotation. |
| 77 | + * |
| 78 | + * @return array ['type', 'name', 'parameters'] or ['type', 'name', 'parameters', 'description'] if description is set. |
| 79 | + */ |
| 80 | + public static function parseAnnotation(string $value): array |
| 81 | + { |
| 82 | + $matches = array(); |
| 83 | + preg_match("/(\w+) (\w+)\(([^()]*)\) *(.*)/", trim($value), $matches); |
| 84 | + |
| 85 | + if (\count($matches) < 3) { |
| 86 | + // Malformed value, let "initAnnotation" report about it. |
| 87 | + return array(); |
| 88 | + } |
| 89 | + |
| 90 | + $result = array( |
| 91 | + 'type' => $matches[1], |
| 92 | + 'name' => $matches[2], |
| 93 | + 'parameters' => array() |
| 94 | + ); |
| 95 | + |
| 96 | + if (isset($matches[3])) { |
| 97 | + if (\strlen($str = trim($matches[3])) > 0) { |
| 98 | + $params = \explode(",", $str); |
| 99 | + foreach ($params as $param) { |
| 100 | + $parts = \explode(" ", trim($param)); |
| 101 | + if (\count($parts) > 1) { |
| 102 | + $result['parameters'][] = array('type' => $parts[0], 'name' => \substr($parts[1], 1)); |
| 103 | + } else { |
| 104 | + $result['parameters'][] = array('type' => 'mixed', 'name' => \substr($parts[0], 1)); |
| 105 | + } |
| 106 | + } |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + if (isset($matches[4])) { |
| 111 | + $result['description'] = $matches[4]; |
| 112 | + } |
| 113 | + |
| 114 | + return $result; |
| 115 | + } |
| 116 | + |
| 117 | + /** |
| 118 | + * Initialize the annotation. |
| 119 | + * |
| 120 | + * @param array $properties The array of annotation properties. |
| 121 | + * |
| 122 | + * @throws AnnotationException When the type or the name of the property is missing. |
| 123 | + */ |
| 124 | + public function initAnnotation(array $properties) |
| 125 | + { |
| 126 | + $this->map($properties, array('type', 'name', 'parameters', 'description')); |
| 127 | + |
| 128 | + parent::initAnnotation($properties); |
| 129 | + |
| 130 | + if (!isset($this->type)) { |
| 131 | + throw new AnnotationException(self::class . ' requires a type property'); |
| 132 | + } |
| 133 | + |
| 134 | + if (!isset($this->name)) { |
| 135 | + throw new AnnotationException(self::class . ' requires a name property'); |
| 136 | + } |
| 137 | + |
| 138 | + $this->type = $this->file->resolveType($this->type); |
| 139 | + } |
| 140 | + |
| 141 | + /** |
| 142 | + * Provides information about file, that contains this annotation. |
| 143 | + * |
| 144 | + * @param AnnotationFile $file Annotation file. |
| 145 | + * |
| 146 | + * @return void |
| 147 | + */ |
| 148 | + public function setAnnotationFile(AnnotationFile $file) |
| 149 | + { |
| 150 | + $this->file = $file; |
| 151 | + } |
34 | 152 | }
|
0 commit comments