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