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, string|array $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 114
addStyle(string $name, string|array $style)

Adds a user defined style.

Parameters

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

at line 125
protected string getTagName(string $tag)

Returns the tag name.

Parameters

string $tag Tag

Return Value

string

at line 136
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 146
protected string getSgrResetSequence()

Returns ANSI SGR escape sequence for style reset.

Return Value

string

at line 158
protected array getStyleCodes(string $tag)

Returns style codes associated with the tag name.

Parameters

string $tag Tag name

Return Value

array

Exceptions

FormatterException

at line 185
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 199
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 214
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

Exceptions

FormatterException

at line 250
protected string removeTagEscapeCharacter(string $string)

Strips escape character from escaped tags.

Parameters

string $string Input string

Return Value

string

at line 258
string format(string $string)

Returns formatted string.

Parameters

string $string String to format

Return Value

string

Exceptions

FormatterException

at line 303
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 311
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 322
string stripSGR(string $string)

Returns a string where all SGR sequences have been stripped.

Parameters

string $string String to strip

Return Value

string