Skip to content

Commit eca26a6

Browse files
committed
feat: add method for retrieving a hierarchical array for a param hash array return type
1 parent b5e7d47 commit eca26a6

File tree

1 file changed

+70
-0
lines changed

1 file changed

+70
-0
lines changed

src/Formatting.php

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -522,6 +522,76 @@ public static function convertListsToMarkup($text) {
522522
return $text;
523523
}
524524

525+
/**
526+
* Returns hierarchical array for a param hash array type
527+
*
528+
* @author Evan D Shaw <[email protected]>
529+
* @param string $text
530+
* @param array $pieces
531+
* @return (int|array)[]
532+
*/
533+
public static function getParamHashArrayRecursive($text, $pieces = []) {
534+
if (!$text || '{' != $text[0]) {
535+
return [
536+
'numparsed' => 0,
537+
'pieces' => [],
538+
];
539+
}
540+
541+
$index = 0;
542+
$noprocessrange = 0;
543+
$text = trim(substr($text, 1, -1));
544+
$text = str_replace('@type', "\n@type", $text);
545+
$parts = explode("\n", $text);
546+
547+
foreach ($parts as $part) {
548+
if ($index < $noprocessrange) {
549+
$index++;
550+
continue;
551+
}
552+
553+
$part = preg_replace('/\s+/', ' ', $part);
554+
list( $wordtype, $type, $name, $rawdescription ) = explode(' ', $part . ' ', 4); // extra spaces ensure we'll always have 4 items.
555+
$description = trim($rawdescription);
556+
557+
$piece = [];
558+
if ('@type' != $wordtype) {
559+
$pieces['description'] = $part;
560+
} else {
561+
$piece['name'] = $name;
562+
$piece['type'] = $type;
563+
$piece['wordtype'] = $wordtype;
564+
$piece['description'] = $description;
565+
}
566+
567+
// Handle nested hashes.
568+
if (($description && '{' === $description[0]) || '{' === $name) {
569+
$deschashpieces = explode('{', $rawdescription);
570+
$nestedtext = join(' ', array_slice($parts, $index + 1));
571+
$nestedtext = '{' . $deschashpieces[1] . ' ' . $nestedtext;
572+
$results = self::getParamHashArrayRecursive($nestedtext, $piece);
573+
$noprocessrange = $index + $results['numparsed'];
574+
$piece['pieces'] = $results['pieces'];
575+
$pieces['pieces'][] = $piece['pieces'];
576+
} elseif ('}' === substr($description, -1)) {
577+
$pieces['pieces'][] = $piece;
578+
return [
579+
'numparsed' => $index + 1,
580+
'pieces' => $pieces,
581+
];
582+
} elseif (!empty($piece)) {
583+
$pieces['pieces'][] = $piece;
584+
}
585+
586+
$index++;
587+
}
588+
589+
return [
590+
'numparsed' => $index,
591+
'pieces' => $pieces,
592+
];
593+
}
594+
525595
/**
526596
* Formats the output of params defined using hash notation.
527597
*

0 commit comments

Comments
 (0)