Passed
Push — master ( 69774c...de7969 )
by Sebastian
02:46
created

IOUtil::getLineSeparator()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
ccs 1
cts 1
cp 1
rs 10
cc 1
nc 1
nop 2
crap 1
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/captainhookphp/captainhook
22
 * @since   Class available since Release 0.9.0
23
 */
24
abstract class IOUtil
25
{
26
    /**
27
     * Maps config values to Symfony verbosity values
28
     *
29
     * @var array<string, int>
30
     */
31
    private static $verbosityMap = [
32
        'quiet'        => OutputInterface::VERBOSITY_QUIET,
33
        'normal'       => OutputInterface::VERBOSITY_NORMAL,
34
        'verbose'      => OutputInterface::VERBOSITY_VERBOSE,
35
        'very-verbose' => OutputInterface::VERBOSITY_VERY_VERBOSE,
36
        'debug'        => OutputInterface::VERBOSITY_DEBUG
37
    ];
38
39
    /**
40
     * Return the Symfony verbosity for a given config value
41
     *
42
     * @param  string $verbosity
43 9
     * @return int
44
     */
45 9
    public static function mapConfigVerbosity(string $verbosity): int
46
    {
47
        return self::$verbosityMap[strtolower($verbosity)] ?? OutputInterface::VERBOSITY_NORMAL;
48
    }
49
50
    /**
51
     * Convert a user answer to boolean
52
     *
53
     * @param  string $answer
54 11
     * @return bool
55
     */
56 11
    public static function answerToBool($answer): bool
57
    {
58
        return in_array($answer, ['y', 'yes', 'ok']);
59
    }
60
61
    /**
62
     * Return cli line separator string
63
     *
64
     * @param  int    $length
65
     * @param  string $char
66 21
     * @return string
67
     */
68 21
    public static function getLineSeparator(int $length = 80, string $char = '='): string
69
    {
70
        return str_repeat($char, $length);
71
    }
72
73
    /**
74
     * Convert everything to a string
75
     *
76
     * @param  array<string>|bool|string|null $arg
77
     * @param  string                         $default
78 13
     * @return string
79
     */
80 13
    public static function argToString($arg, $default = ''): string
81
    {
82
        return is_string($arg) ? $arg : $default;
83
    }
84
85
    /**
86
     * Convert everything to a boolean
87
     *
88
     * @param  array<string>|bool|string|null $arg
89
     * @param  bool                           $default
90 4
     * @return bool
91
     */
92 4
    public static function argToBool($arg, $default = false): bool
93
    {
94
        return is_bool($arg) ? $arg : $default;
95
    }
96
}
97