Passed
Push — master ( 5d747e...1a64d0 )
by Sebastian
02:02
created

IOUtil::argToString()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 2
crap 2
1
<?php
2
/**
3
 * This file is part of CaptainHook.
4
 *
5
 * (c) Sebastian Feldmann <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
namespace CaptainHook\App\Console;
11
12
/**
13
 * IOUtil class
14
 *
15
 * @package CaptainHook
16
 * @author  Sebastian Feldmann <[email protected]>
17
 * @link    https://github.com/captainhookphp/captainhook
18
 * @since   Class available since Release 0.9.0
19
 */
20
abstract class IOUtil
21
{
22
    /**
23
     * Convert a user answer to boolean
24
     *
25
     * @param  string $answer
26
     * @return bool
27
     */
28 11
    public static function answerToBool($answer) : bool
29
    {
30 11
        return in_array($answer, ['y', 'yes', 'ok']);
31
    }
32
33
    /**
34
     * Return cli line separator string
35
     *
36
     * @param  int    $length
37
     * @param  string $char
38
     * @return string
39
     */
40 20
    public static function getLineSeparator(int $length = 80, string $char = '=') : string
41
    {
42 20
        return str_repeat($char, $length);
43
    }
44
45
    /**
46
     * Convert everything to a string
47
     *
48
     * @param  array<string>|bool|string|null $arg
49
     * @param  string                        $default
50
     * @return string
51
     */
52 14
    public static function argToString($arg, $default = '') : string
53
    {
54 14
        return is_string($arg) ? $arg : $default;
55
    }
56
57
    /**
58
     * Convert everything to a boolean
59
     *
60
     * @param  array<string>|bool|string|null $arg
61
     * @param  bool                           $default
62
     * @return bool
63
     */
64 4
    public static function argToBool($arg, $default = false) : bool
65
    {
66 4
        return is_bool($arg) ? $arg : $default;
67
    }
68
}
69