|
14 | 14 | namespace mindplay\annotations\standard;
|
15 | 15 |
|
16 | 16 | use mindplay\annotations\Annotation;
|
| 17 | +use mindplay\annotations\AnnotationException; |
| 18 | +use mindplay\annotations\AnnotationFile; |
| 19 | +use mindplay\annotations\IAnnotationFileAware; |
| 20 | +use mindplay\annotations\IAnnotationParser; |
| 21 | + |
17 | 22 |
|
18 | 23 | /**
|
19 |
| - * Defines a magic/virtual property and it's type |
| 24 | + * Defines a magic/virtual property and it's type. |
20 | 25 | *
|
21 |
| - * @todo implement this |
| 26 | + * @usage('class'=>true, 'inherited'=>true) |
22 | 27 | */
|
23 |
| -class PropertyAnnotation extends Annotation |
| 28 | +class PropertyAnnotation extends Annotation implements IAnnotationParser, IAnnotationFileAware |
24 | 29 | {
|
| 30 | + /** |
| 31 | + * Specifies the property type. |
| 32 | + * |
| 33 | + * @var string |
| 34 | + */ |
| 35 | + public $type; |
| 36 | + |
| 37 | + /** |
| 38 | + * Specifies the property name. |
| 39 | + * |
| 40 | + * @var string |
| 41 | + */ |
| 42 | + public $name; |
| 43 | + |
| 44 | + /** |
| 45 | + * Specifies the property description. |
| 46 | + * |
| 47 | + * @var string |
| 48 | + */ |
| 49 | + public $description; |
| 50 | + |
| 51 | + /** |
| 52 | + * Annotation file. |
| 53 | + * |
| 54 | + * @var AnnotationFile |
| 55 | + */ |
| 56 | + protected $file; |
| 57 | + |
| 58 | + /** |
| 59 | + * Parse the standard PHP-DOC "property" annotation. |
| 60 | + * |
| 61 | + * @param string $value The raw string value of the Annotation. |
| 62 | + * |
| 63 | + * @return array ['type', 'name'] or ['type', 'name', 'description'] if description is set. |
| 64 | + */ |
| 65 | + public static function parseAnnotation($value) |
| 66 | + { |
| 67 | + $parts = explode(' ', trim($value), 3); |
| 68 | + |
| 69 | + if (count($parts) < 2) { |
| 70 | + // Malformed value, let "initAnnotation" report about it. |
| 71 | + return array(); |
| 72 | + } |
| 73 | + |
| 74 | + $result = array('type' => $parts[0], 'name' => substr($parts[1], 1)); |
| 75 | + |
| 76 | + if (isset($parts[2])) { |
| 77 | + $result['description'] = $parts[2]; |
| 78 | + } |
| 79 | + |
| 80 | + return $result; |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * Initialize the annotation. |
| 85 | + */ |
| 86 | + public function initAnnotation(array $properties) |
| 87 | + { |
| 88 | + $this->map($properties, array('type', 'name', 'description')); |
| 89 | + |
| 90 | + parent::initAnnotation($properties); |
| 91 | + |
| 92 | + if (!isset($this->type)) { |
| 93 | + throw new AnnotationException(basename(__CLASS__).' requires a type property'); |
| 94 | + } |
| 95 | + |
| 96 | + if (!isset($this->name)) { |
| 97 | + throw new AnnotationException(basename(__CLASS__).' requires a name property'); |
| 98 | + } |
| 99 | + |
| 100 | + $this->type = $this->file->resolveType($this->type); |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * Provides information about file, that contains this annotation. |
| 105 | + * |
| 106 | + * @param AnnotationFile $file Annotation file. |
| 107 | + * |
| 108 | + * @return void |
| 109 | + */ |
| 110 | + public function setAnnotationFile(AnnotationFile $file) |
| 111 | + { |
| 112 | + $this->file = $file; |
| 113 | + } |
25 | 114 | }
|
0 commit comments