You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

LuDocBusiness.php 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: robin
  5. * Date: 10/22/15
  6. * Time: 9:59 PM
  7. */
  8. namespace Luticate\Doc\Business;
  9. use Exception;
  10. class DocBlock {
  11. public $docblock,
  12. $description = null,
  13. $all_params = array();
  14. /**
  15. * Parses a docblock;
  16. */
  17. function __construct($docblock) {
  18. if( !is_string($docblock) ) {
  19. throw new Exception("DocBlock expects first parameter to be a string");
  20. }
  21. $this->docblock = $docblock;
  22. $this->parse_block();
  23. }
  24. /**
  25. * An alias to __call();
  26. * allows a better DSL
  27. *
  28. * @param string $param_name
  29. * @return mixed
  30. */
  31. public function __get($param_name) {
  32. return $this->$param_name();
  33. }
  34. /**
  35. * Checks if the param exists
  36. *
  37. * @param string $param_name
  38. * @return mixed
  39. */
  40. public function __call($param_name, $values = null) {
  41. if( $param_name == "description" ) {
  42. return $this->description;
  43. }else if( isset($this->all_params[$param_name]) ) {
  44. $params = $this->all_params[$param_name];
  45. if( count($params) == 1 ) {
  46. return $params[0];
  47. }else {
  48. return $params;
  49. }
  50. }
  51. return null;
  52. }
  53. /**
  54. * Parse each line in the docblock
  55. * and store the params in `$this->all_params`
  56. * and the rest in `$this->description`
  57. */
  58. private function parse_block() {
  59. // split at each line
  60. foreach(preg_split("/(\r?\n)/", $this->docblock) as $line){
  61. // if starts with an asterisk
  62. if( preg_match('/^(?=\s+?\*[^\/])(.+)/', $line, $matches) ) {
  63. $info = $matches[1];
  64. // remove wrapping whitespace
  65. $info = trim($info);
  66. // remove leading asterisk
  67. $info = preg_replace('/^(\*\s+?)/', '', $info);
  68. // if it doesn't start with an "@" symbol
  69. // then add to the description
  70. if( $info[0] !== "@" ) {
  71. $this->description .= "\n$info";
  72. continue;
  73. }else {
  74. // get the name of the param
  75. if (preg_match('/@param +\\$([^ ]+) +([^ ]+) +(.*)$/', $info, $matches) == 1) {
  76. $param_name = $matches[1];
  77. $param_type = $matches[2];
  78. $value = $matches[3];
  79. // push the param value into place
  80. $this->all_params[$param_name] = [
  81. "name" => $param_name,
  82. "type" => $param_type,
  83. "description" => $value
  84. ];
  85. continue;
  86. }
  87. }
  88. }
  89. }
  90. }
  91. }