1 | <?php |
||
10 | class ReflectionDocComment |
||
11 | { |
||
12 | |||
13 | /** |
||
14 | * The original document block that was parsed. |
||
15 | * |
||
16 | * @var string |
||
17 | */ |
||
18 | private $originalDocBlock = ''; |
||
19 | |||
20 | /** |
||
21 | * The parsed short description. |
||
22 | * |
||
23 | * @var string |
||
24 | */ |
||
25 | protected $shortDescription; |
||
26 | |||
27 | /** |
||
28 | * The parsed long description. |
||
29 | * |
||
30 | * @var string |
||
31 | */ |
||
32 | protected $longDescription; |
||
33 | |||
34 | /** |
||
35 | * Constructor. |
||
36 | * |
||
37 | * @param string $comment The original document block comment. |
||
38 | * @param string $trimLinePattern Pattern for trim() function applied to each line. Usefull to leave spaces or tabs. The default is the same as calling trim() without the argument. |
||
39 | */ |
||
40 | 186 | public function __construct($comment, $trimLinePattern = " \t\n\r\0\x0B") |
|
41 | { |
||
42 | 186 | $this->originalDocBlock = trim((string)$comment); |
|
43 | |||
44 | 186 | $comment = preg_replace('#^\s*\*\s?#ms', '', trim($this->originalDocBlock, '/*')); |
|
45 | 186 | $comment = preg_split('#^\s*(?=@[_a-zA-Z\x7F-\xFF][_a-zA-Z0-9\x7F-\xFF-]*)#m', $comment, 2); |
|
46 | |||
47 | 186 | if (isset($comment[0]) === true) { |
|
48 | 186 | $description = $comment[0]; |
|
49 | 186 | $description = preg_split("/\n|\n\r/", $description); |
|
50 | 186 | array_walk($description, function (& $value, $key, $trimLinePattern) { |
|
51 | 186 | $value = trim($value, $trimLinePattern); |
|
52 | 186 | }, $trimLinePattern); |
|
53 | |||
54 | 186 | foreach ($description as $key => $descLine) { |
|
55 | 186 | if ($descLine !== '') { |
|
56 | 111 | $this->shortDescription = $descLine; |
|
57 | 111 | unset($description[$key]); |
|
58 | 111 | $this->longDescription = trim(implode("\n", $description)); |
|
59 | 111 | $this->longDescription = $this->longDescription !== '' ? $this->longDescription : null; |
|
60 | 111 | break; |
|
61 | } |
||
62 | |||
63 | 75 | unset($description[$key]); |
|
64 | 62 | } |
|
65 | 62 | } |
|
66 | 186 | } |
|
67 | |||
68 | /** |
||
69 | * Get the full description. |
||
70 | * The long description (if present) will be concatenated to the short description with an empty line between. |
||
71 | * |
||
72 | * @return string |
||
73 | */ |
||
74 | 33 | public function getFullDescription() |
|
82 | |||
83 | /** |
||
84 | * Get the short description. |
||
85 | * |
||
86 | * @return string |
||
87 | */ |
||
88 | 27 | public function getShortDescription() |
|
92 | |||
93 | /** |
||
94 | * Get the long description. |
||
95 | * |
||
96 | * @return string |
||
97 | */ |
||
98 | 33 | public function getLongDescription() |
|
102 | |||
103 | /** |
||
104 | * Get the annotations collection. |
||
105 | * |
||
106 | * @return \Wingu\OctopusCore\Reflection\Annotation\AnnotationsCollection |
||
107 | */ |
||
108 | 24 | public function getAnnotationsCollection() |
|
112 | |||
113 | /** |
||
114 | * Get the original doc block. |
||
115 | * |
||
116 | * @return string |
||
117 | */ |
||
118 | 24 | public function getOriginalDocBlock() |
|
122 | |||
123 | /** |
||
124 | * Check if the documentation comment is empty or not. |
||
125 | * |
||
126 | * @return boolean |
||
127 | */ |
||
128 | 15 | public function isEmpty() |
|
132 | |||
133 | /** |
||
134 | * Magic method to print out the document. |
||
135 | * |
||
136 | * @return string |
||
137 | */ |
||
138 | 24 | public function __toString() |
|
142 | } |
||
143 |