class Formatter implements FormatterInterface

Formatter.

Constants

TAG_REGEX

Regex that matches non-escaped tags.

ESCAPED_TAG_REGEX

Regex that matches escaped tags.

ANSI_SGR_SEQUENCE_REGEX

Regex that mathes ANSI SGR sequences.

Properties

protected array $styles Styles.
protected array $userStyles User styles.
protected array $openTags Open tags.

Methods

addStyle(string $name, array|string $style)

Adds a user defined style.

string
getTagName(string $tag)

Returns the tag name.

bool
isOpeningTag(string $tag)

Returns TRUE if the tag is a closing tag and FALSE if not.

string
getSgrResetSequence()

Returns ANSI SGR escape sequence for style reset.

array
getStyleCodes(string $tag)

Returns style codes associated with the tag name.

string
getSgrStyleSequence(string $tag)

Returns ANSI SGR escape sequence for the chosen style(s).

string
openStyle(string $tag)

Returns ANSI SGR escape sequence(s) for the chosen style(s) and adds the tag name to the array of open tags.

string
closeStyle(string $tag)

Returns ANSI SGR escape sequence for style reset and ANSI SGR escape sequence for parent style if the closed tag was nested.

string
removeTagEscapeCharacter(string $string)

Strips escape character from escaped tags.

string
format(string $string)

Returns formatted string.

string
escape(string $string)

Returns a string where all formatting tags have been escaped.

string
stripTags(string $string)

Returns a string where all formatting tags have been stripped.

string
stripSGR(string $string)

Returns a string where all SGR sequences have been stripped.

Details

at line 112
addStyle(string $name, array|string $style)

Adds a user defined style.

Parameters

string $name Style name
array|string $style Style or array of styles

at line 123
protected string getTagName(string $tag)

Returns the tag name.

Parameters

string $tag Tag

Return Value

string

at line 134
protected bool isOpeningTag(string $tag)

Returns TRUE if the tag is a closing tag and FALSE if not.

Parameters

string $tag Tag to check

Return Value

bool

at line 144
protected string getSgrResetSequence()

Returns ANSI SGR escape sequence for style reset.

Return Value

string

at line 155
protected array getStyleCodes(string $tag)

Returns style codes associated with the tag name.

Parameters

string $tag Tag name

Return Value

array

at line 182
protected string getSgrStyleSequence(string $tag)

Returns ANSI SGR escape sequence for the chosen style(s).

Parameters

string $tag Style name

Return Value

string

at line 196
protected string openStyle(string $tag)

Returns ANSI SGR escape sequence(s) for the chosen style(s) and adds the tag name to the array of open tags.

Parameters

string $tag Tag name

Return Value

string

at line 210
protected string closeStyle(string $tag)

Returns ANSI SGR escape sequence for style reset and ANSI SGR escape sequence for parent style if the closed tag was nested.

Parameters

string $tag Tag name

Return Value

string

at line 246
protected string removeTagEscapeCharacter(string $string)

Strips escape character from escaped tags.

Parameters

string $string Input string

Return Value

string

at line 254
string format(string $string)

Returns formatted string.

Parameters

string $string String to format

Return Value

string

at line 300
string escape(string $string)

Returns a string where all formatting tags have been escaped.

Parameters

string $string String to format

Return Value

string

at line 308
string stripTags(string $string)

Returns a string where all formatting tags have been stripped.

Parameters

string $string String to strip

Return Value

string

at line 319
string stripSGR(string $string)

Returns a string where all SGR sequences have been stripped.

Parameters

string $string String to strip

Return Value

string