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.
 
 
 
 
 

112 lines
2.4 KiB

  1. <?php
  2. namespace dokuwiki\Remote\OpenApiDoc;
  3. use Reflector;
  4. class DocBlock
  5. {
  6. /** @var Reflector The reflected object */
  7. protected $reflector;
  8. /** @var string The first line of the decription */
  9. protected $summary = '';
  10. /** @var string The description */
  11. protected $description = '';
  12. /** @var string The parsed tags */
  13. protected $tags = [];
  14. /**
  15. * Parse the given docblock
  16. *
  17. * The docblock can be of a method, class or property.
  18. *
  19. * @param Reflector $reflector
  20. */
  21. public function __construct(Reflector $reflector)
  22. {
  23. $this->reflector = $reflector;
  24. $docblock = $reflector->getDocComment();
  25. // strip asterisks and leading spaces
  26. $docblock = trim(preg_replace(
  27. ['/^[ \t]*\/\*+[ \t]*/m', '/[ \t]*\*+[ \t]*/m', '/\*+\/\s*$/m', '/\s*\/\s*$/m'],
  28. ['', '', '', ''],
  29. $docblock
  30. ));
  31. // get all tags
  32. $tags = [];
  33. if (preg_match_all('/^@(\w+)\s+(.*)$/m', $docblock, $matches, PREG_SET_ORDER)) {
  34. foreach ($matches as $match) {
  35. $tags[$match[1]][] = trim($match[2]);
  36. }
  37. }
  38. // strip the tags from the docblock
  39. $docblock = preg_replace('/^@(\w+)\s+(.*)$/m', '', $docblock);
  40. // what remains is summary and description
  41. [$summary, $description] = sexplode("\n\n", $docblock, 2, '');
  42. // store everything
  43. $this->summary = trim($summary);
  44. $this->description = trim($description);
  45. $this->tags = $tags;
  46. }
  47. /**
  48. * The class name of the declaring class
  49. *
  50. * @return string
  51. */
  52. protected function getContext()
  53. {
  54. return $this->reflector->getDeclaringClass()->getName();
  55. }
  56. /**
  57. * Get the first line of the description
  58. *
  59. * @return string
  60. */
  61. public function getSummary()
  62. {
  63. return $this->summary;
  64. }
  65. /**
  66. * Get the full description
  67. *
  68. * @return string
  69. */
  70. public function getDescription()
  71. {
  72. return $this->description;
  73. }
  74. /**
  75. * Get all tags
  76. *
  77. * @return array
  78. */
  79. public function getTags()
  80. {
  81. return $this->tags;
  82. }
  83. /**
  84. * Get a specific tag
  85. *
  86. * @param string $tag
  87. * @return array
  88. */
  89. public function getTag($tag)
  90. {
  91. if (!isset($this->tags[$tag])) return [];
  92. return $this->tags[$tag];
  93. }
  94. }