99 */
1010namespace Framework \CLI ;
1111
12- use InvalidArgumentException ;
12+ use Framework \CLI \Styles \BackgroundColor ;
13+ use Framework \CLI \Styles \ForegroundColor ;
14+ use Framework \CLI \Styles \Format ;
1315use JetBrains \PhpStorm \Pure ;
16+ use ValueError ;
1417
1518/**
1619 * Class CLI.
@@ -77,14 +80,14 @@ public static function wrap(string $text, int $width = null) : string
7780 public static function strlen (string $ text ) : int
7881 {
7982 $ codes = [];
80- foreach (static :: $ foregroundColors as $ color ) {
81- $ codes [] = $ color ;
83+ foreach (ForegroundColor:: cases () as $ case ) {
84+ $ codes [] = $ case -> getCode () ;
8285 }
83- foreach (static :: $ backgroundColors as $ background ) {
84- $ codes [] = $ background ;
86+ foreach (BackgroundColor:: cases () as $ case ) {
87+ $ codes [] = $ case -> getCode () ;
8588 }
86- foreach (static :: $ formats as $ format ) {
87- $ codes [] = $ format ;
89+ foreach (Format:: cases () as $ case ) {
90+ $ codes [] = $ case -> getCode () ;
8891 }
8992 $ codes [] = static ::$ reset ;
9093 $ text = \str_replace ($ codes , '' , $ text );
@@ -95,39 +98,36 @@ public static function strlen(string $text) : int
9598 * Applies styles to a text.
9699 *
97100 * @param string $text The text to be styled
98- * @param string|null $color Foreground color. One of the FG_* constants
99- * @param string|null $background Background color. One of the BG_* constants
100- * @param array<int, string> $formats The text format. A list of FM_* constants
101+ * @param ForegroundColor| string|null $color Foreground color
102+ * @param BackgroundColor| string|null $background Background color
103+ * @param array<Format| string> $formats The text formats
101104 *
102- * @throws InvalidArgumentException For invalid color, background or format
105+ * @throws ValueError For invalid color, background or format
103106 *
104107 * @return string Returns the styled text
105108 */
106109 public static function style (
107110 string $ text ,
108- string $ color = null ,
109- string $ background = null ,
111+ ForegroundColor | string $ color = null ,
112+ BackgroundColor | string $ background = null ,
110113 array $ formats = []
111114 ) : string {
112115 $ string = '' ;
113116 if ($ color !== null ) {
114- if (empty (static ::$ foregroundColors [$ color ])) {
115- throw new InvalidArgumentException ('Invalid color: ' . $ color );
116- }
117- $ string = static ::$ foregroundColors [$ color ];
117+ $ string = \is_string ($ color )
118+ ? ForegroundColor::from ($ color )->getCode ()
119+ : $ color ->getCode ();
118120 }
119121 if ($ background !== null ) {
120- if (empty (static ::$ backgroundColors [$ background ])) {
121- throw new InvalidArgumentException ('Invalid background color: ' . $ background );
122- }
123- $ string .= static ::$ backgroundColors [$ background ];
122+ $ string .= \is_string ($ background )
123+ ? BackgroundColor::from ($ background )->getCode ()
124+ : $ background ->getCode ();
124125 }
125126 if ($ formats ) {
126127 foreach ($ formats as $ format ) {
127- if (empty (static ::$ formats [$ format ])) {
128- throw new InvalidArgumentException ('Invalid format: ' . $ format );
129- }
130- $ string .= static ::$ formats [$ format ];
128+ $ string .= \is_string ($ format )
129+ ? Format::from ($ format )->getCode ()
130+ : $ format ->getCode ();
131131 }
132132 }
133133 $ string .= $ text . static ::$ reset ;
@@ -140,14 +140,14 @@ public static function style(
140140 * Optionally with styles and width wrapping.
141141 *
142142 * @param string $text The text to be written
143- * @param string|null $color Foreground color. One of the FG_* constants
144- * @param string|null $background Background color. One of the BG_* constants
143+ * @param ForegroundColor| string|null $color Foreground color
144+ * @param BackgroundColor| string|null $background Background color
145145 * @param int|null $width Width to wrap the text. Null to do not wrap.
146146 */
147147 public static function write (
148148 string $ text ,
149- string $ color = null ,
150- string $ background = null ,
149+ ForegroundColor | string $ color = null ,
150+ BackgroundColor | string $ background = null ,
151151 int $ width = null
152152 ) : void {
153153 if ($ width !== null ) {
@@ -214,13 +214,13 @@ public static function beep(int $times = 1, int $usleep = 0) : void
214214 * Writes a message box.
215215 *
216216 * @param array<int,string>|string $lines One line as string or multi-lines as array
217- * @param string $background Background color. One of the BG_* constants
218- * @param string $color Foreground color. One of the FG_* constants
217+ * @param BackgroundColor| string $background Background color
218+ * @param ForegroundColor| string $color Foreground color
219219 */
220220 public static function box (
221221 array | string $ lines ,
222- string $ background = CLI :: BG_BLACK ,
223- string $ color = CLI :: FG_WHITE
222+ BackgroundColor | string $ background = BackgroundColor::black ,
223+ ForegroundColor | string $ color = ForegroundColor::white
224224 ) : void {
225225 $ width = static ::getWidth ();
226226 $ width -= 2 ;
@@ -260,7 +260,7 @@ public static function box(
260260 public static function error (string $ message , ?int $ exitCode = 1 ) : void
261261 {
262262 static ::beep ();
263- \fwrite (\STDERR , static ::style ($ message , static :: FG_RED ) . \PHP_EOL );
263+ \fwrite (\STDERR , static ::style ($ message , ForegroundColor::red ) . \PHP_EOL );
264264 if ($ exitCode !== null ) {
265265 exit ($ exitCode );
266266 }
@@ -319,7 +319,7 @@ public static function prompt(string $question, array | string $options = null)
319319 }
320320 if ($ options ) {
321321 $ opt = $ options ;
322- $ opt [0 ] = static ::style ($ opt [0 ], null , null , [static :: FM_BOLD ]);
322+ $ opt [0 ] = static ::style ($ opt [0 ], null , null , [Format::bold ]);
323323 $ optionsText = isset ($ opt [1 ])
324324 ? \implode (', ' , $ opt )
325325 : $ opt [0 ];
0 commit comments