1 | <?php namespace nyx\console\output\formatting; |
||
28 | class Formatter implements interfaces\Formatter |
||
29 | { |
||
30 | /** |
||
31 | * The formatting tag matching pattern. |
||
32 | */ |
||
33 | protected const PATTERN = '[a-z][a-z0-9,_:;\s-]*+'; |
||
34 | |||
35 | /** |
||
36 | * @var styles\Map The Styles available to this Formatter. |
||
37 | */ |
||
38 | private $styles; |
||
39 | |||
40 | /** |
||
41 | * @var styles\Stack The style processing Stack for this Formatter. |
||
42 | */ |
||
43 | private $stack; |
||
44 | |||
45 | /** |
||
46 | * Constructs a new Output Formatter. |
||
47 | * |
||
48 | * @param styles\Map $styles A Map of Styles to be used instead of the default. |
||
49 | * @param interfaces\Style $default The default text styling. |
||
50 | */ |
||
51 | public function __construct(styles\Map $styles = null, interfaces\Style $default = null) |
||
56 | |||
57 | /** |
||
58 | * {@inheritdoc} |
||
59 | */ |
||
60 | public function format(string $text, bool $decorated = true) : string |
||
103 | |||
104 | /** |
||
105 | * Returns the Styles in use by this Formatter. |
||
106 | * |
||
107 | * @return styles\Map |
||
108 | */ |
||
109 | public function getStyles() : styles\Map |
||
113 | |||
114 | /** |
||
115 | * Returns the style processing Stack in use by this Formatter. |
||
116 | * |
||
117 | * @return styles\Stack |
||
118 | */ |
||
119 | public function getStack() : styles\Stack |
||
123 | |||
124 | /** |
||
125 | * Creates a default Map of Styles to be used by this Formatter. |
||
126 | * |
||
127 | * @return styles\Map |
||
128 | */ |
||
129 | protected function createDefaultStyles() : styles\Map |
||
139 | |||
140 | /** |
||
141 | * Handles an encountered formatting tag by determining what Style it refers to (if applicable) |
||
142 | * and pushing/popping the Style from the Stack, depending on whether it was an opening tag. |
||
143 | * |
||
144 | * @param string $tag The encountered formatting tag. |
||
145 | * @param bool $opening Whether it was an opening tag. |
||
146 | * @return interfaces\Style The Style matching the tag, if applicable. |
||
147 | */ |
||
148 | protected function handleFormattingTag(string $tag, bool $opening) : ?interfaces\Style |
||
164 | |||
165 | /** |
||
166 | * Applies formatting to the given text. |
||
167 | * |
||
168 | * @param string $text The text that should be formatted. |
||
169 | * @param bool $decorated Whether decorations, like colors, should be applied to the text. |
||
170 | * @return string The resulting text. |
||
171 | */ |
||
172 | protected function apply(string $text, bool $decorated) : string |
||
176 | } |
||
177 |
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: