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.

LuMethodDocParser.php 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: robin
  5. * Date: 5/29/16
  6. * Time: 7:42 PM
  7. */
  8. namespace Luticate\Utils\Business;
  9. use Luticate\Utils\Dbo\LuParameterConstraintDbo;
  10. use Luticate\Utils\Dbo\LuParameterDbo;
  11. class LuMethodDocParser
  12. {
  13. /**
  14. * @var string
  15. */
  16. private $_doc;
  17. /**
  18. * @return string
  19. */
  20. public function getSummary()
  21. {
  22. return $this->_summary;
  23. }
  24. /**
  25. * @var string
  26. */
  27. private $_summary = "";
  28. /**
  29. * @var LuParameterDbo[]
  30. */
  31. private $_params = [];
  32. /**
  33. * @var string
  34. */
  35. private $_returnType = "";
  36. /**
  37. * @return string
  38. */
  39. public function getDoc()
  40. {
  41. return $this->_doc;
  42. }
  43. /**
  44. * @return \Luticate\Utils\Dbo\LuParameterDbo[]
  45. */
  46. public function getParams()
  47. {
  48. return $this->_params;
  49. }
  50. /**
  51. * @return string
  52. */
  53. public function getReturnType()
  54. {
  55. return $this->_returnType;
  56. }
  57. /**
  58. * LuDocParser constructor.
  59. * @param $doc string
  60. */
  61. public function __construct($doc)
  62. {
  63. $this->_doc = $doc;
  64. }
  65. public function parse()
  66. {
  67. if (!is_string($this->_doc)) {
  68. return;
  69. }
  70. $lines = preg_split("/(\r?\n)/", $this->_doc);
  71. $count = count($lines);
  72. if ($count > 2) {
  73. array_splice($lines, 0, 1);
  74. array_splice($lines, $count - 2, 1);
  75. }
  76. $currentParam = null;
  77. foreach ($lines as $line) {
  78. $lineMatches = [];
  79. if (preg_match("/ *\\** *(.*) */", $line, $lineMatches) === 1) {
  80. $line = $lineMatches[1];
  81. $commandMatches = [];
  82. if (preg_match("/@([^ ]+) *(.*)/", $line, $commandMatches)) {
  83. $command = strtolower($commandMatches[1]);
  84. $line = $commandMatches[2];
  85. if ($command == "param") {
  86. $currentParam = new LuParameterDbo();
  87. $paramMatches = [];
  88. if (preg_match("/([^ ]+) *([^ ]+) *(.*)/", $line, $paramMatches) === 1) {
  89. if ($paramMatches[1][0] == "$") {
  90. $currentParam->setName(substr($paramMatches[1], 1, strlen($paramMatches[1]) - 1));
  91. $currentParam->setType($paramMatches[2]);
  92. }
  93. else {
  94. $currentParam->setName(substr($paramMatches[2], 1, strlen($paramMatches[2]) - 1));
  95. $currentParam->setType($paramMatches[1]);
  96. }
  97. $currentParam->setSummary($paramMatches[3] . "\n");
  98. }
  99. $this->_params[$currentParam->getName()] = $currentParam;
  100. }
  101. else if ($command == "deprecated") {
  102. }
  103. else if ($command == "inheritdoc") {
  104. }
  105. else if ($command == "internal") {
  106. }
  107. else if ($command == "link") {
  108. }
  109. else if ($command == "return") {
  110. $this->_returnType = $line;
  111. }
  112. else if ($command == "see") {
  113. }
  114. else if ($command == "throws") {
  115. }
  116. else if (!is_null($currentParam)) {
  117. $methodName = $command;
  118. if ($methodName == "nullable") {
  119. $currentParam->setNotNull(false);
  120. }
  121. else {
  122. $constraint = new LuParameterConstraintDbo();
  123. $constraint->setMethod($methodName);
  124. $args = [];
  125. $argMatches = [];
  126. if (preg_match_all('/ *(-?(?:\d*\.\d+|\d+|true|false|null|"[^"]*"|\'[^\']*\'))/', $line, $argMatches) !== false) {
  127. $args = $argMatches[1];
  128. foreach ($args as $key => $arg) {
  129. $argLower = strtolower($arg);
  130. if ($arg[0] == '"' || $arg[0] == "'") {
  131. $args[$key] = substr($arg, 1, count($arg) - 2);
  132. } else if ($argLower == "true") {
  133. $args[$key] = true;
  134. } else if ($argLower == "false") {
  135. $args[$key] = false;
  136. } else if ($argLower == "null") {
  137. $args[$key] = null;
  138. } else if (strpos($arg, ".") !== false) {
  139. $args[$key] = floatval($arg);
  140. } else {
  141. $args[$key] = intval($arg);
  142. }
  143. }
  144. }
  145. $constraint->setArguments($args);
  146. $currentParam->addConstraint($constraint);
  147. }
  148. }
  149. }
  150. else {
  151. if (is_null($currentParam)) {
  152. $this->_summary .= $line . "\n";
  153. }
  154. else {
  155. $currentParam->setSummary($currentParam->getSummary() . $line . "\n");
  156. }
  157. }
  158. }
  159. }
  160. }
  161. }