1 | <?php |
||
2 | |||
3 | /** |
||
4 | * This file is part of CaptainHook |
||
5 | * |
||
6 | * (c) Sebastian Feldmann <[email protected]> |
||
7 | * |
||
8 | * For the full copyright and license information, please view the LICENSE |
||
9 | * file that was distributed with this source code. |
||
10 | */ |
||
11 | |||
12 | namespace CaptainHook\App\Console; |
||
13 | |||
14 | use Symfony\Component\Console\Output\OutputInterface; |
||
15 | |||
16 | /** |
||
17 | * IOUtil class |
||
18 | * |
||
19 | * @package CaptainHook |
||
20 | * @author Sebastian Feldmann <[email protected]> |
||
21 | * @link https://github.com/captainhook-git/captainhook |
||
22 | * @since Class available since Release 0.9.0 |
||
23 | */ |
||
24 | abstract class IOUtil |
||
25 | { |
||
26 | public const PREFIX_OK = '<info>✔</info>'; |
||
27 | public const PREFIX_FAIL = '<fg=red>✘</>'; |
||
28 | |||
29 | /** |
||
30 | * Maps config values to Symfony verbosity values |
||
31 | * |
||
32 | * @var array<string, 16|32|64|128|256> |
||
0 ignored issues
–
show
Documentation
Bug
introduced
by
![]() |
|||
33 | */ |
||
34 | private static array $verbosityMap = [ |
||
35 | 'quiet' => OutputInterface::VERBOSITY_QUIET, |
||
36 | 'normal' => OutputInterface::VERBOSITY_NORMAL, |
||
37 | 'verbose' => OutputInterface::VERBOSITY_VERBOSE, |
||
38 | 'very-verbose' => OutputInterface::VERBOSITY_VERY_VERBOSE, |
||
39 | 'debug' => OutputInterface::VERBOSITY_DEBUG |
||
40 | ]; |
||
41 | |||
42 | /** |
||
43 | * Return the Symfony verbosity for a given config value |
||
44 | * |
||
45 | * @param string $verbosity |
||
46 | * @return OutputInterface::VERBOSITY_* |
||
47 | */ |
||
48 | 34 | public static function mapConfigVerbosity(string $verbosity): int |
|
49 | { |
||
50 | 34 | return self::$verbosityMap[strtolower($verbosity)] ?? OutputInterface::VERBOSITY_NORMAL; |
|
0 ignored issues
–
show
|
|||
51 | } |
||
52 | |||
53 | /** |
||
54 | * Convert a user answer to boolean |
||
55 | * |
||
56 | * @param string $answer |
||
57 | * @return bool |
||
58 | */ |
||
59 | 15 | public static function answerToBool(string $answer): bool |
|
60 | { |
||
61 | 15 | return in_array(strtolower($answer), ['y', 'yes', 'ok']); |
|
62 | } |
||
63 | |||
64 | /** |
||
65 | * Create formatted cli headline |
||
66 | * |
||
67 | * >>>> HEADLINE <<<< |
||
68 | * ==== HEADLINE ==== |
||
69 | * |
||
70 | * @param string $headline |
||
71 | * @param int $length |
||
72 | * @param string $pre |
||
73 | * @param string $post |
||
74 | * @return string |
||
75 | */ |
||
76 | 2 | public static function formatHeadline(string $headline, int $length, string $pre = '=', string $post = '='): string |
|
77 | { |
||
78 | 2 | $headlineLength = mb_strlen($headline); |
|
79 | 2 | if ($headlineLength > ($length - 3)) { |
|
80 | 1 | return $headline; |
|
81 | } |
||
82 | |||
83 | 1 | $prefix = (int) floor(($length - $headlineLength - 2) / 2); |
|
84 | 1 | $suffix = (int) ceil(($length - $headlineLength - 2) / 2); |
|
85 | |||
86 | 1 | return str_repeat($pre, $prefix) . ' ' . $headline . ' ' . str_repeat($post, $suffix); |
|
87 | } |
||
88 | |||
89 | /** |
||
90 | * Convert everything to a string |
||
91 | * |
||
92 | * @param array<string>|bool|string|null $arg |
||
93 | * @param string $default |
||
94 | * @return string |
||
95 | */ |
||
96 | 40 | public static function argToString($arg, string $default = ''): string |
|
97 | { |
||
98 | 40 | return is_string($arg) ? $arg : $default; |
|
99 | } |
||
100 | |||
101 | /** |
||
102 | * Convert everything to a boolean |
||
103 | * |
||
104 | * @param array<string>|bool|string|null $arg |
||
105 | * @param bool $default |
||
106 | * @return bool |
||
107 | */ |
||
108 | 12 | public static function argToBool($arg, bool $default = false): bool |
|
109 | { |
||
110 | 12 | return is_bool($arg) ? $arg : $default; |
|
111 | } |
||
112 | } |
||
113 |