Final Class yii\apidoc\helpers\TextHelper

Inheritanceyii\apidoc\helpers\TextHelper
Source Code https://github.com/yiisoft/yii2-apidoc/blob/master/helpers/TextHelper.php

An auxiliary class for working with texts.

Public Methods

Hide inherited methods

Method Description Defined By
extractFirstSentence() Tries to extract the first sentence from the text. yii\apidoc\helpers\TextHelper
getDescriptionsByFullDescription() Gets a short and detailed description based on the full description of the tag. yii\apidoc\helpers\TextHelper

Method Details

Hide inherited methods

extractFirstSentence() public static method

Tries to extract the first sentence from the text.

Note: Function may not handle some abbreviations correctly.

public static string extractFirstSentence ( string $text )
$text string

                public static function extractFirstSentence(string $text): string
{
    $text = trim($text);
    if ($text === '') {
        return '';
    }
    $text = preg_replace('/\R/', ' ', $text);
    $length = mb_strlen((string) $text, 'UTF-8');
    for ($i = 0; $i < $length; $i++) {
        $char = mb_substr((string) $text, $i, 1, 'UTF-8');
        if (!in_array($char, ['.', '!', '?'], true)) {
            continue;
        }
        $endPos = $i;
        if ($char === '.') {
            // Numbers like 1.2.3
            if (
                $i > 0
                && $i + 1 < $length
                && is_numeric(mb_substr((string) $text, $i - 1, 1, 'UTF-8'))
                && is_numeric(mb_substr((string) $text, $i + 1, 1, 'UTF-8'))
            ) {
                continue;
            }
            // Ellipsis
            while ($endPos + 1 < $length && mb_substr((string) $text, $endPos + 1, 1, 'UTF-8') === '.') {
                $endPos++;
            }
        }
        $nextIndex = $endPos + 1;
        while ($nextIndex < $length) {
            $c = mb_substr((string) $text, $nextIndex, 1, 'UTF-8');
            if ($c === ' ' || $c === "\t") {
                $nextIndex++;
                continue;
            }
            break;
        }
        $nextChar = mb_substr((string) $text, $nextIndex, 1, 'UTF-8');
        if (preg_match('/\p{Lu}/u', $nextChar)) {
            return trim(mb_substr((string) $text, 0, $endPos + 1, 'UTF-8'));
        }
        $i = $endPos;
    }
    return $text;
}

            
getDescriptionsByFullDescription() public static method

Gets a short and detailed description based on the full description of the tag.

Needed for cases where there is only a full description of the tag (i.e., no summary).

public static array{short: string, detailed: string} getDescriptionsByFullDescription ( string $fullDescription )
$fullDescription string

                public static function getDescriptionsByFullDescription(string $fullDescription): array
{
    $fullDescription = trim($fullDescription);
    $shortDescription = self::extractFirstSentence($fullDescription);
    $description = $shortDescription
        ? mb_substr($fullDescription, mb_strlen($shortDescription, 'UTF-8'), null, 'UTF-8')
        : '';
    return [
        'short' => StringHelper::mb_ucfirst($shortDescription),
        'detailed' => trim($description),
    ];
}