Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
31 / 31 |
|
100.00% |
11 / 11 |
CRAP | |
100.00% |
1 / 1 |
| Alert | |
100.00% |
31 / 31 |
|
100.00% |
11 / 11 |
18 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| alert | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
3 | |||
| alertBox | |
100.00% |
13 / 13 |
|
100.00% |
1 / 1 |
6 | |||
| alertDanger | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| alertWarning | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| alertSuccess | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| alertInfo | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| alertPrimary | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| alertSecondary | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| alertDark | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| alertLight | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | declare(strict_types=1); |
| 3 | /** |
| 4 | * Pop PHP Framework (https://www.popphp.org/) |
| 5 | * |
| 6 | * @link https://github.com/popphp/popphp-framework |
| 7 | * @author Nick Sagona, III <nick@popphp.org> |
| 8 | * @copyright Copyright (c) 2009-2026 Nick Sagona, III |
| 9 | * @license https://www.popphp.org/license New BSD License |
| 10 | */ |
| 11 | |
| 12 | /** |
| 13 | * @namespace |
| 14 | */ |
| 15 | namespace Pop\Console; |
| 16 | |
| 17 | /** |
| 18 | * Console alert class |
| 19 | * |
| 20 | * @category Pop |
| 21 | * @package Pop\Console |
| 22 | * @author Nick Sagona, III <nick@popphp.org> |
| 23 | * @copyright Copyright (c) 2009-2026 Nick Sagona, III |
| 24 | * @license https://www.popphp.org/license New BSD License |
| 25 | * @version 5.0.0 |
| 26 | */ |
| 27 | class Alert |
| 28 | { |
| 29 | |
| 30 | use MessageTrait; |
| 31 | |
| 32 | /** |
| 33 | * Instantiate the alert object |
| 34 | * |
| 35 | * @param string $indent |
| 36 | * @param ?int $wrap |
| 37 | * @param int $width |
| 38 | * @param ?int $margin |
| 39 | */ |
| 40 | public function __construct( |
| 41 | protected string $indent = '', protected ?int $wrap = null, protected int $width = 0, protected ?int $margin = null |
| 42 | ) { } |
| 43 | |
| 44 | /** |
| 45 | * Build a colored alert box |
| 46 | * |
| 47 | * @param string $message |
| 48 | * @param int $fg |
| 49 | * @param int $bg |
| 50 | * @param int|string|null $size |
| 51 | * @param string $align |
| 52 | * @param int $innerPad |
| 53 | * @param bool $newline |
| 54 | * @return string |
| 55 | */ |
| 56 | public function alert( |
| 57 | string $message, int $fg, int $bg, int|string|null $size = null, string $align = 'center', |
| 58 | int $innerPad = 4, bool $newline = true |
| 59 | ): string |
| 60 | { |
| 61 | $size = $this->resolveSize($message, $size, $this->wrap, $this->width, $this->margin, $innerPad * 2); |
| 62 | $messageLines = $this->buildAlignedMessageLines($message, $size, $align, $innerPad); |
| 63 | |
| 64 | $alert = $this->indent . Color::colorize(str_repeat(' ', $size), $fg, $bg) . PHP_EOL; |
| 65 | foreach ($messageLines as $messageLine) { |
| 66 | $alert .= $this->indent . Color::colorize($messageLine, $fg, $bg) . PHP_EOL; |
| 67 | } |
| 68 | $alert .= $this->indent . Color::colorize(str_repeat(' ', $size), $fg, $bg) . PHP_EOL; |
| 69 | if ($newline) { |
| 70 | $alert .= PHP_EOL; |
| 71 | } |
| 72 | |
| 73 | return $alert; |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Build a colorless alert outline box |
| 78 | * |
| 79 | * @param string $message |
| 80 | * @param string $h |
| 81 | * @param ?string $v |
| 82 | * @param int|string|null $size |
| 83 | * @param string $align |
| 84 | * @param int $innerPad |
| 85 | * @param bool $newline |
| 86 | * @return string |
| 87 | */ |
| 88 | public function alertBox( |
| 89 | string $message, string $h = '-', ?string $v = '|', int|string|null $size = null, |
| 90 | string $align = 'center', int $innerPad = 4, bool $newline = true |
| 91 | ): string |
| 92 | { |
| 93 | $size = $this->resolveSize($message, $size, $this->wrap, $this->width, $this->margin, $innerPad * 2); |
| 94 | $messageLines = $this->buildAlignedMessageLines($message, $size, $align, $innerPad); |
| 95 | |
| 96 | $alert = $this->indent . str_repeat($h, $size) . PHP_EOL; |
| 97 | $alert .= $this->indent . $v . str_repeat(' ', $size - 2) . $v . PHP_EOL; |
| 98 | foreach ($messageLines as $messageLine) { |
| 99 | if (!empty($v) && str_starts_with($messageLine, ' ') && str_ends_with($messageLine, ' ')) { |
| 100 | $messageLine = $v . substr($messageLine, 1, -1) . $v; |
| 101 | } |
| 102 | $alert .= $this->indent . $messageLine . PHP_EOL; |
| 103 | } |
| 104 | $alert .= $this->indent . $v . str_repeat(' ', $size - 2) . $v . PHP_EOL; |
| 105 | $alert .= $this->indent . str_repeat($h, $size) . PHP_EOL; |
| 106 | if ($newline) { |
| 107 | $alert .= PHP_EOL; |
| 108 | } |
| 109 | |
| 110 | return $alert; |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * Build a "danger" alert box |
| 115 | * |
| 116 | * @param string $message |
| 117 | * @param int|string|null $size |
| 118 | * @param string $align |
| 119 | * @param int $innerPad |
| 120 | * @param bool $newline |
| 121 | * @return string |
| 122 | */ |
| 123 | public function alertDanger(string $message, int|string|null $size = null, string $align = 'center', int $innerPad = 4, bool $newline = true): string |
| 124 | { |
| 125 | return $this->alert($message, Color::BRIGHT_BOLD_WHITE, Color::BRIGHT_RED, $size, $align, $innerPad, $newline); |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * Build a "warning" alert box |
| 130 | * |
| 131 | * @param string $message |
| 132 | * @param int|string|null $size |
| 133 | * @param string $align |
| 134 | * @param int $innerPad |
| 135 | * @param bool $newline |
| 136 | * @return string |
| 137 | */ |
| 138 | public function alertWarning(string $message, int|string|null $size = null, string $align = 'center', int $innerPad = 4, bool $newline = true): string |
| 139 | { |
| 140 | return $this->alert($message, Color::BOLD_BLACK, Color::BRIGHT_YELLOW, $size, $align, $innerPad, $newline); |
| 141 | } |
| 142 | |
| 143 | /** |
| 144 | * Build a "success" alert box |
| 145 | * |
| 146 | * @param string $message |
| 147 | * @param int|string|null $size |
| 148 | * @param string $align |
| 149 | * @param int $innerPad |
| 150 | * @param bool $newline |
| 151 | * @return string |
| 152 | */ |
| 153 | public function alertSuccess(string $message, int|string|null $size = null, string $align = 'center', int $innerPad = 4, bool $newline = true): string |
| 154 | { |
| 155 | return $this->alert($message, Color::BOLD_BLACK, Color::GREEN, $size, $align, $innerPad, $newline); |
| 156 | } |
| 157 | |
| 158 | /** |
| 159 | * Build an "info" alert box |
| 160 | * |
| 161 | * @param string $message |
| 162 | * @param int|string|null $size |
| 163 | * @param string $align |
| 164 | * @param int $innerPad |
| 165 | * @param bool $newline |
| 166 | * @return string |
| 167 | */ |
| 168 | public function alertInfo(string $message, int|string|null $size = null, string $align = 'center', int $innerPad = 4, bool $newline = true): string |
| 169 | { |
| 170 | return $this->alert($message, Color::BRIGHT_BOLD_WHITE, Color::BRIGHT_BLUE, $size, $align, $innerPad, $newline); |
| 171 | } |
| 172 | |
| 173 | /** |
| 174 | * Build a "primary" alert box |
| 175 | * |
| 176 | * @param string $message |
| 177 | * @param int|string|null $size |
| 178 | * @param string $align |
| 179 | * @param int $innerPad |
| 180 | * @param bool $newline |
| 181 | * @return string |
| 182 | */ |
| 183 | public function alertPrimary(string $message, int|string|null $size = null, string $align = 'center', int $innerPad = 4, bool $newline = true): string |
| 184 | { |
| 185 | return $this->alert($message, Color::BRIGHT_BOLD_WHITE, Color::BLUE, $size, $align, $innerPad, $newline); |
| 186 | } |
| 187 | |
| 188 | /** |
| 189 | * Build a "secondary" alert box |
| 190 | * |
| 191 | * @param string $message |
| 192 | * @param int|string|null $size |
| 193 | * @param string $align |
| 194 | * @param int $innerPad |
| 195 | * @param bool $newline |
| 196 | * @return string |
| 197 | */ |
| 198 | public function alertSecondary(string $message, int|string|null $size = null, string $align = 'center', int $innerPad = 4, bool $newline = true): string |
| 199 | { |
| 200 | return $this->alert($message, Color::BRIGHT_BOLD_WHITE, Color::MAGENTA, $size, $align, $innerPad, $newline); |
| 201 | } |
| 202 | |
| 203 | /** |
| 204 | * Build a "dark" alert box |
| 205 | * |
| 206 | * @param string $message |
| 207 | * @param int|string|null $size |
| 208 | * @param string $align |
| 209 | * @param int $innerPad |
| 210 | * @param bool $newline |
| 211 | * @return string |
| 212 | */ |
| 213 | public function alertDark(string $message, int|string|null $size = null, string $align = 'center', int $innerPad = 4, bool $newline = true): string |
| 214 | { |
| 215 | return $this->alert($message, Color::BRIGHT_BOLD_WHITE, Color::BRIGHT_BLACK, $size, $align, $innerPad, $newline); |
| 216 | } |
| 217 | |
| 218 | /** |
| 219 | * Build a "light" alert box |
| 220 | * |
| 221 | * @param string $message |
| 222 | * @param int|string|null $size |
| 223 | * @param string $align |
| 224 | * @param int $innerPad |
| 225 | * @param bool $newline |
| 226 | * @return string |
| 227 | */ |
| 228 | public function alertLight(string $message, int|string|null $size = null, string $align = 'center', int $innerPad = 4, bool $newline = true): string |
| 229 | { |
| 230 | return $this->alert($message, Color::BOLD_BLACK, Color::WHITE, $size, $align, $innerPad, $newline); |
| 231 | } |
| 232 | |
| 233 | } |