Ubiquity 2.5.2
php rapid development framework
Loading...
Searching...
No Matches
DocParser.php
Go to the documentation of this file.
1<?php
2
4
5
7
16class DocParser {
18 private $lines;
19 private $description;
23 private $formater;
24
25 public function __construct($content,DocFormater $formater=null){
26 $this->originalContent=$content;
27 $this->description=[];
28 $this->lines=[];
29 $this->formater=$formater;
30 }
31 public function parse(){
32 $this->lines=\explode("\n", $this->originalContent);
33 foreach ($this->lines as $line){
34 $line=\trim($line);
35 $line=\preg_replace("@^(\*\*\/)|(\/\*\*)|(\*)|(\/)@i", "", $line);
36 if(UString::isNotNull($line)){
37 $line=\trim($line);
38 if(UString::startswith($line, "@")){
39 if(\preg_match("@^\@(.*?)\ @i", $line,$matches)){
40 $this->addInArray($this->lines, $matches[1], \preg_replace("@^\@".\preg_quote($matches[1])."(.*?)\ @i", "$1", $line));
41 }
42 }else{
43 $this->description[]=$line;
44 }
45 }
46 }
47 $this->description=\array_diff($this->description, ["","/"]);
48 return $this;
49 }
50
51 public function isEmpty(){
52 return \sizeof($this->lines)==0;
53 }
54
55 private function addInArray(&$array,$key,$value){
56 if(!isset($array[$key])){
57 $array[$key]=[];
58 }
59 $array[$key][]=$value;
60 }
61
62 public function getDescription(){
63 return $this->description;
64 }
65
66 public function getPart($partKey){
67 if(isset($this->lines[$partKey]))
68 return $this->lines[$partKey];
69 return [];
70 }
71
72 public function getDescriptionAsHtml($separator="<br>"){
73 $descs=$this->getDescription();
74 if(\sizeof($descs)>0){
75 $descs=self::getElementsAsHtml($descs,null);
76 if(isset($separator)){
77 return \implode($separator, $descs);
78 }
79 return $descs;
80 }
81 return null;
82 }
83
84 public function getMethodParams(){
85 return $this->getPart("param");
86 }
87
88 public function getMethodParamsReturn(){
89 return \array_merge($this->getPart("param"),$this->getPart("return"));
90 }
91
92 public function getMethodReturn(){
93 return $this->getPart("return");
94 }
95
96 public function getMethodParamsAsHtml($separator=NULL){
97 return self::getElementsAsHtml($this->getMethodParams(), $separator);
98 }
99
100 public function getMethodParamsReturnAsHtml($separator=NULL){
101 return self::getElementsAsHtml($this->getMethodParamsReturn(), $separator);
102 }
103
104
105
106 public function getMethodReturnAsHtml($separator="<br>"){
107 return self::getElementsAsHtml($this->getMethodReturn(), $separator);
108 }
109
110
111 public function getElementsAsHtml($elements,$separator="<br>"){
112 $result=[];
113 foreach ($elements as $element){
114 $result[]=$this->formater->replaceAll($element);
115 }
116 if(isset($separator))
117 return \implode($separator, $result);
118 return $result;
119 }
120
121 public static function docClassParser($classname){
122 if(\class_exists($classname)){
123 $reflect=new \ReflectionClass($classname);
124 return (new DocParser($reflect->getDocComment(),new DocFormater()))->parse();
125 }
126 }
127
128 public static function docMethodParser($classname,$method){
129 if(\class_exists($classname)){
130 if(\method_exists($classname, $method)){
131 $reflect=new \ReflectionMethod($classname,$method);
132 return (new DocParser($reflect->getDocComment(),new DocFormater()))->parse();
133 }
134 }
135 }
136}
getDescriptionAsHtml($separator="<br>")
Definition DocParser.php:72
getMethodParamsReturnAsHtml($separator=NULL)
addInArray(&$array, $key, $value)
Definition DocParser.php:55
getMethodReturnAsHtml($separator="<br>")
__construct($content, DocFormater $formater=null)
Definition DocParser.php:25
getElementsAsHtml($elements, $separator="<br>")
static docMethodParser($classname, $method)
String utilities.
Definition UString.php:15
Documentation parser.