public function format(string $text) : string
    {
        if ($text === '') {
            return $text;
        }

        $startCodes = [];
        $endCodes = [];

        if ($this->foregroundColor !== null) {
            $startCodes[] = self::$supportedForegroundColors[$this->foregroundColor][0];
            $endCodes[] = self::$supportedForegroundColors[$this->foregroundColor][1];
        }

        if ($this->backgroundColor !== null) {
            $startCodes[] = self::$supportedBackgroundColors[$this->backgroundColor][0];
            $endCodes[] = self::$supportedBackgroundColors[$this->backgroundColor][1];
        }

        foreach ($this->textStyles as $style) {
            $startCodes[] = self::$supportedTextStyles[$style][0];
            $endCodes[] = self::$supportedTextStyles[$style][1];
        }

        if (count($startCodes) === 0 && count($endCodes) === 0) {
            // No point in trying to format the text
            return $text;
        }

        return sprintf(
            "\033[%sm%s\033[%sm",
            implode(';', $startCodes),
            $text,
            implode(';', $endCodes)
        );
    }
public function format(string $text) : string
    {
        if ($text === '') {
            return $text;
        }

        $startCodes = [];
        $endCodes = [];

        if ($this->foregroundColor !== null) {
            $startCodes[] = self::$supportedForegroundColors[$this->foregroundColor][0];
            $endCodes[] = self::$supportedForegroundColors[$this->foregroundColor][1];
        }

        if ($this->backgroundColor !== null) {
            $startCodes[] = self::$supportedBackgroundColors[$this->backgroundColor][0];
            $endCodes[] = self::$supportedBackgroundColors[$this->backgroundColor][1];
        }

        foreach ($this->textStyles as $style) {
            $startCodes[] = self::$supportedTextStyles[$style][0];
            $endCodes[] = self::$supportedTextStyles[$style][1];
        }

        if (count($startCodes) === 0 && count($endCodes) === 0) {
            // No point in trying to format the text
            return $text;
        }

        return sprintf(
            "\033[%sm%s\033[%sm",
            implode(';', $startCodes),
            $text,
            implode(';', $endCodes)
        );
    }
public function format ( string $ text ) : string { if ( $ text === '' ) { return $ text ; } $ startCodes = [ ] ; $ endCodes = [ ] ; if ( $ this -> foregroundColor !== null ) { $ startCodes [ ] = self :: $ supportedForegroundColors [ $ this -> foregroundColor ] [ 0 ] ; $ endCodes [ ] = self :: $ supportedForegroundColors [ $ this -> foregroundColor ] [ 1 ] ; } if ( $ this -> backgroundColor !== null ) { $ startCodes [ ] = self :: $ supportedBackgroundColors [ $ this -> backgroundColor ] [ 0 ] ; $ endCodes [ ] = self :: $ supportedBackgroundColors [ $ this -> backgroundColor ] [ 1 ] ; } foreach ( $ this -> textStyles as $ style ) { $ startCodes [ ] = self :: $ supportedTextStyles [ $ style ] [ 0 ] ; $ endCodes [ ] = self :: $ supportedTextStyles [ $ style ] [ 1 ] ; } if ( count ( $ startCodes ) === 0 && count ( $ endCodes ) === 0 ) { // No point in trying to format the text return $ text ; } return sprintf ( "\033[%sm%s\033[%sm" , implode ( ';' , $ startCodes ) , $ text , implode ( ';' , $ endCodes ) ) ; }
Formats text with the the currently-set styles

@param string $text The text to format
@return string The formatted text
Formats text with the the currently - set styles