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