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.

LuPropertyDocParser.php 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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 LuPropertyDocParser
  12. {
  13. /**
  14. * LuDocParser constructor.
  15. * @param $doc string
  16. */
  17. public function __construct($doc)
  18. {
  19. $this->_doc = $doc;
  20. }
  21. /**
  22. * @return LuParameterDbo
  23. */
  24. public function parse()
  25. {
  26. if (!is_string($this->_doc)) {
  27. return null;
  28. }
  29. $lines = preg_split("/(\r?\n)/", $this->_doc);
  30. $count = count($lines);
  31. if ($count > 2) {
  32. array_splice($lines, 0, 1);
  33. array_splice($lines, $count - 2, 1);
  34. }
  35. $currentParam = new LuParameterDbo();
  36. foreach ($lines as $line) {
  37. $lineMatches = [];
  38. if (preg_match("/ *\\** *(.*) */", $line, $lineMatches) === 1) {
  39. $line = $lineMatches[1];
  40. $commandMatches = [];
  41. if (preg_match("/@([^ ]+) *(.*)/", $line, $commandMatches)) {
  42. $command = strtolower($commandMatches[1]);
  43. $line = $commandMatches[2];
  44. if ($command == "var") {
  45. $paramMatches = [];
  46. if (preg_match("/([^ ]+) *([^ ]+) *(.*)/", $line, $paramMatches) === 1) {
  47. if ($paramMatches[1][0] == "$") {
  48. $currentParam->setName(substr($paramMatches[1], 1, strlen($paramMatches[1]) - 1));
  49. $currentParam->setType($paramMatches[2]);
  50. }
  51. else {
  52. $currentParam->setName(substr($paramMatches[2], 1, strlen($paramMatches[2]) - 1));
  53. $currentParam->setType($paramMatches[1]);
  54. }
  55. $currentParam->setSummary($paramMatches[3] . "\n");
  56. }
  57. }
  58. else {
  59. $methodName = $command;
  60. $constraint = new LuParameterConstraintDbo();
  61. $constraint->setMethod($methodName);
  62. $args = [];
  63. $argMatches = [];
  64. if (preg_match_all('/ *(-?(?:\d*\.\d+|\d+|true|false|null|"[^"]*"|\'[^\']*\'))/', $line, $argMatches) !== false) {
  65. $args = $argMatches[1];
  66. foreach ($args as $key => $arg) {
  67. $argLower = strtolower($arg);
  68. if ($arg[0] == '"' || $arg[0] == "'") {
  69. $args[$key] = substr($arg, 1, count($arg) - 2);
  70. }
  71. else if ($argLower == "true") {
  72. $args[$key] = true;
  73. }
  74. else if ($argLower == "false") {
  75. $args[$key] = false;
  76. }
  77. else if ($argLower == "null") {
  78. $args[$key] = null;
  79. }
  80. else if (strpos($arg, ".") !== false) {
  81. $args[$key] = floatval($arg);
  82. }
  83. else {
  84. $args[$key] = intval($arg);
  85. }
  86. }
  87. }
  88. $constraint->setArguments($args);
  89. $currentParam->addConstraint($constraint);
  90. }
  91. }
  92. else {
  93. $currentParam->setSummary($currentParam->getSummary() . $line . "\n");
  94. }
  95. }
  96. }
  97. return $currentParam;
  98. }
  99. }