Skip to content

Commit 8a07ada

Browse files
jcheronaik099
authored andcommitted
PropertyAnnotation implementation (php-annotations#120)
Implementation of `@property` annotation
1 parent 52299b4 commit 8a07ada

File tree

1 file changed

+92
-3
lines changed

1 file changed

+92
-3
lines changed

src/annotations/standard/PropertyAnnotation.php

Lines changed: 92 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,101 @@
1414
namespace mindplay\annotations\standard;
1515

1616
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+
1722

1823
/**
19-
* Defines a magic/virtual property and it's type
24+
* Defines a magic/virtual property and it's type.
2025
*
21-
* @todo implement this
26+
* @usage('class'=>true, 'inherited'=>true)
2227
*/
23-
class PropertyAnnotation extends Annotation
28+
class PropertyAnnotation extends Annotation implements IAnnotationParser, IAnnotationFileAware
2429
{
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+
}
25114
}

0 commit comments

Comments
 (0)