123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105 |
- <?php
- /**
- * Created by PhpStorm.
- * User: robin
- * Date: 10/22/15
- * Time: 9:59 PM
- */
-
- namespace Luticate\Doc\Business;
-
- use Exception;
-
- class DocBlock {
-
- public $docblock;
- public $description = null;
- public $all_params = array();
-
- /**
- * Parses a docblock;
- */
- function __construct($docblock)
- {
- if (!is_string($docblock)) {
- throw new Exception("DocBlock expects first parameter to be a string");
- }
-
- $this->docblock = $docblock;
- $this->parse_block();
- }
-
- /**
- * An alias to __call();
- * allows a better DSL
- *
- * @param string $param_name
- * @return mixed
- */
- public function __get($param_name) {
- return $this->$param_name();
- }
-
- /**
- * Checks if the param exists
- *
- * @param string $param_name
- * @return mixed
- */
- public function __call($param_name, $values = null)
- {
- if ($param_name == "description") {
- return $this->description;
- }
- else if (isset($this->all_params[$param_name])) {
- $params = $this->all_params[$param_name];
-
- if (count($params) == 1) {
- return $params[0];
- }
- else {
- return $params;
- }
- }
- return null;
- }
-
- /**
- * Parse each line in the docblock
- * and store the params in `$this->all_params`
- * and the rest in `$this->description`
- */
- private function parse_block() {
- foreach(preg_split("/(\r?\n)/", $this->docblock) as $line) {
-
- if (preg_match('/^(?=\s+?\*[^\/])(.+)/', $line, $matches)) {
-
- $info = $matches[1];
- $info = trim($info);
- $info = preg_replace('/^(\*\s+?)/', '', $info);
-
- if ($info[0] !== "@") {
- $this->description .= "\n$info";
- }
- else if (preg_match('/@param +([^ ]+) +([^ ]+) +(.*)$/', $info, $matches) == 1) {
- if ($matches[1][0] == "$") {
- $param_name = $matches[1];
- $param_type = $matches[2];
- }
- else {
- $param_name = $matches[2];
- $param_type = $matches[1];
- }
- $value = $matches[3];
- $param_name = substr($param_name, 1);
-
- $this->all_params[$param_name] = [
- "name" => $param_name,
- "type" => $param_type,
- "description" => $value
- ];
- }
- }
- }
- }
- }
|