src/Base/AbstractTextElement.php
protected function onFormatText(string &$text): void
    {
        $lines = preg_split("/\r\n|\n|\r/", $text);
        $result = [];

        $lastEmptyLinesCount = 0;
        $hasFoundNonEmptyLines = false;

        foreach ($lines as $line) {
            if (($this->textFormatOptions & TextFormatOptions::TRIM) !== 0) {
                $line = trim($line);
            }
            if (($this->textFormatOptions & TextFormatOptions::COMPACT) !== 0) {
                $line = preg_replace('/\s+/', ' ', $line);
            }

            if ($line === '') {
                if (($this->textFormatOptions & TextFormatOptions::COMPACT_LINES) !== 0 && $lastEmptyLinesCount > 0) {
                    continue;
                }
                if (($this->textFormatOptions & TextFormatOptions::TRIM_LINES) !== 0 && !$hasFoundNonEmptyLines) {
                    continue;
                }

                $lastEmptyLinesCount++;
            } else {
                $lastEmptyLinesCount = 0;
                $hasFoundNonEmptyLines = true;
            }

            $result[] = $line;
        }

        if (($this->textFormatOptions & TextFormatOptions::TRIM_LINES) !== 0 && $lastEmptyLinesCount > 0) {
            // Trim the last empty lines (we did not know until now that those were at the end).
            array_splice($result, -$lastEmptyLinesCount);
        }

        $text = implode("\r\n", $result);
    }
protected function onFormatText(string &$text): void
    {
        $lines = preg_split("/\r\n|\n|\r/", $text);
        $result = [];

        $lastEmptyLinesCount = 0;
        $hasFoundNonEmptyLines = false;

        foreach ($lines as $line) {
            if (($this->textFormatOptions & TextFormatOptions::TRIM) !== 0) {
                $line = trim($line);
            }
            if (($this->textFormatOptions & TextFormatOptions::COMPACT) !== 0) {
                $line = preg_replace('/\s+/', ' ', $line);
            }

            if ($line === '') {
                if (($this->textFormatOptions & TextFormatOptions::COMPACT_LINES) !== 0 && $lastEmptyLinesCount > 0) {
                    continue;
                }
                if (($this->textFormatOptions & TextFormatOptions::TRIM_LINES) !== 0 && !$hasFoundNonEmptyLines) {
                    continue;
                }

                $lastEmptyLinesCount++;
            } else {
                $lastEmptyLinesCount = 0;
                $hasFoundNonEmptyLines = true;
            }

            $result[] = $line;
        }

        if (($this->textFormatOptions & TextFormatOptions::TRIM_LINES) !== 0 && $lastEmptyLinesCount > 0) {
            // Trim the last empty lines (we did not know until now that those were at the end).
            array_splice($result, -$lastEmptyLinesCount);
        }

        $text = implode("\r\n", $result);
    }
protected function onFormatText ( string & $ text ) : void { $ lines = preg_split ( "/\r\n|\n|\r/" , $ text ) ; $ result = [ ] ; $ lastEmptyLinesCount = 0 ; $ hasFoundNonEmptyLines = false ; foreach ( $ lines as $ line ) { if ( ( $ this -> textFormatOptions & TextFormatOptions :: TRIM ) !== 0 ) { $ line = trim ( $ line ) ; } if ( ( $ this -> textFormatOptions & TextFormatOptions :: COMPACT ) !== 0 ) { $ line = preg_replace ( '/\s+/' , ' ' , $ line ) ; } if ( $ line === '' ) { if ( ( $ this -> textFormatOptions & TextFormatOptions :: COMPACT_LINES ) !== 0 && $ lastEmptyLinesCount > 0 ) { continue ; } if ( ( $ this -> textFormatOptions & TextFormatOptions :: TRIM_LINES ) !== 0 && ! $ hasFoundNonEmptyLines ) { continue ; } $ lastEmptyLinesCount ++ ; } else { $ lastEmptyLinesCount = 0 ; $ hasFoundNonEmptyLines = true ; } $ result [ ] = $ line ; } if ( ( $ this -> textFormatOptions & TextFormatOptions :: TRIM_LINES ) !== 0 && $ lastEmptyLinesCount > 0 ) { // Trim the last empty lines (we did not know until now that those were at the end). array_splice ( $ result , - $ lastEmptyLinesCount ) ; } $ text = implode ( "\r\n" , $ result ) ; }
Called when text should be formatted.

@since 1.0.0

@param string $text The text.
Called when text should be formatted .