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