xref: /dokuwiki/inc/Remote/OpenApiDoc/DocBlock.php (revision d48c2b252a339bed693da46fae768d93f4b3fe41)
18ddd9b69SAndreas Gohr<?php
28ddd9b69SAndreas Gohr
38ddd9b69SAndreas Gohrnamespace dokuwiki\Remote\OpenApiDoc;
48ddd9b69SAndreas Gohr
58ddd9b69SAndreas Gohruse Reflector;
68ddd9b69SAndreas Gohr
78ddd9b69SAndreas Gohrclass DocBlock
88ddd9b69SAndreas Gohr{
98ddd9b69SAndreas Gohr    /** @var Reflector The reflected object */
108ddd9b69SAndreas Gohr    protected $reflector;
118ddd9b69SAndreas Gohr
128ddd9b69SAndreas Gohr    /** @var string The first line of the decription */
138ddd9b69SAndreas Gohr    protected $summary = '';
148ddd9b69SAndreas Gohr
158ddd9b69SAndreas Gohr    /** @var string The description */
168ddd9b69SAndreas Gohr    protected $description = '';
178ddd9b69SAndreas Gohr
188ddd9b69SAndreas Gohr    /** @var string The parsed tags */
198ddd9b69SAndreas Gohr    protected $tags = [];
208ddd9b69SAndreas Gohr
218ddd9b69SAndreas Gohr    /**
228ddd9b69SAndreas Gohr     * Parse the given docblock
238ddd9b69SAndreas Gohr     *
248ddd9b69SAndreas Gohr     * The docblock can be of a method, class or property.
258ddd9b69SAndreas Gohr     *
268ddd9b69SAndreas Gohr     * @param Reflector $reflector
278ddd9b69SAndreas Gohr     */
288ddd9b69SAndreas Gohr    public function __construct(Reflector $reflector)
298ddd9b69SAndreas Gohr    {
308ddd9b69SAndreas Gohr        $this->reflector = $reflector;
318ddd9b69SAndreas Gohr        $docblock = $reflector->getDocComment();
328ddd9b69SAndreas Gohr
338ddd9b69SAndreas Gohr        // strip asterisks and leading spaces
348ddd9b69SAndreas Gohr        $docblock = trim(preg_replace(
358ddd9b69SAndreas Gohr            ['/^[ \t]*\/\*+[ \t]*/m', '/[ \t]*\*+[ \t]*/m', '/\*+\/\s*$/m', '/\s*\/\s*$/m'],
368ddd9b69SAndreas Gohr            ['', '', '', ''],
378ddd9b69SAndreas Gohr            $docblock
388ddd9b69SAndreas Gohr        ));
398ddd9b69SAndreas Gohr
408ddd9b69SAndreas Gohr        // get all tags
418ddd9b69SAndreas Gohr        $tags = [];
428ddd9b69SAndreas Gohr        if (preg_match_all('/^@(\w+)\s+(.*)$/m', $docblock, $matches, PREG_SET_ORDER)) {
438ddd9b69SAndreas Gohr            foreach ($matches as $match) {
448ddd9b69SAndreas Gohr                $tags[$match[1]][] = trim($match[2]);
458ddd9b69SAndreas Gohr            }
468ddd9b69SAndreas Gohr        }
478ddd9b69SAndreas Gohr
488ddd9b69SAndreas Gohr        // strip the tags from the docblock
498ddd9b69SAndreas Gohr        $docblock = preg_replace('/^@(\w+)\s+(.*)$/m', '', $docblock);
508ddd9b69SAndreas Gohr
518ddd9b69SAndreas Gohr        // what remains is summary and description
528ddd9b69SAndreas Gohr        [$summary, $description] = sexplode("\n\n", $docblock, 2, '');
538ddd9b69SAndreas Gohr
548ddd9b69SAndreas Gohr        // store everything
558ddd9b69SAndreas Gohr        $this->summary = trim($summary);
568ddd9b69SAndreas Gohr        $this->description = trim($description);
578ddd9b69SAndreas Gohr        $this->tags = $tags;
588ddd9b69SAndreas Gohr    }
598ddd9b69SAndreas Gohr
608ddd9b69SAndreas Gohr    /**
618ddd9b69SAndreas Gohr     * The class name of the declaring class
628ddd9b69SAndreas Gohr     *
638ddd9b69SAndreas Gohr     * @return string
648ddd9b69SAndreas Gohr     */
658ddd9b69SAndreas Gohr    protected function getContext()
668ddd9b69SAndreas Gohr    {
678ddd9b69SAndreas Gohr        return $this->reflector->getDeclaringClass()->getName();
688ddd9b69SAndreas Gohr    }
698ddd9b69SAndreas Gohr
708ddd9b69SAndreas Gohr    /**
71*d48c2b25SAndreas Gohr     * Get the first line of the description
72*d48c2b25SAndreas Gohr     *
738ddd9b69SAndreas Gohr     * @return string
748ddd9b69SAndreas Gohr     */
75*d48c2b25SAndreas Gohr    public function getSummary()
768ddd9b69SAndreas Gohr    {
778ddd9b69SAndreas Gohr        return $this->summary;
788ddd9b69SAndreas Gohr    }
798ddd9b69SAndreas Gohr
808ddd9b69SAndreas Gohr    /**
81*d48c2b25SAndreas Gohr     * Get the full description
82*d48c2b25SAndreas Gohr     *
838ddd9b69SAndreas Gohr     * @return string
848ddd9b69SAndreas Gohr     */
85*d48c2b25SAndreas Gohr    public function getDescription()
868ddd9b69SAndreas Gohr    {
878ddd9b69SAndreas Gohr        return $this->description;
888ddd9b69SAndreas Gohr    }
898ddd9b69SAndreas Gohr
908ddd9b69SAndreas Gohr    /**
918ddd9b69SAndreas Gohr     * Get all tags
928ddd9b69SAndreas Gohr     *
938ddd9b69SAndreas Gohr     * @return array
948ddd9b69SAndreas Gohr     */
958ddd9b69SAndreas Gohr    public function getTags()
968ddd9b69SAndreas Gohr    {
978ddd9b69SAndreas Gohr        return $this->tags;
988ddd9b69SAndreas Gohr    }
998ddd9b69SAndreas Gohr
1008ddd9b69SAndreas Gohr    /**
1018ddd9b69SAndreas Gohr     * Get a specific tag
1028ddd9b69SAndreas Gohr     *
1038ddd9b69SAndreas Gohr     * @param string $tag
1048ddd9b69SAndreas Gohr     * @return array
1058ddd9b69SAndreas Gohr     */
1068ddd9b69SAndreas Gohr    public function getTag($tag)
1078ddd9b69SAndreas Gohr    {
1088ddd9b69SAndreas Gohr        if (!isset($this->tags[$tag])) return [];
1098ddd9b69SAndreas Gohr        return $this->tags[$tag];
1108ddd9b69SAndreas Gohr    }
1118ddd9b69SAndreas Gohr}
112