Passed
Push — master ( a3e4b0...6ea7df )
by Sebastian
05:06
created

ConvertHelper_Bool   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 102
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 14
eloc 34
dl 0
loc 102
rs 10
c 1
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A toString() 0 20 5
A isBoolean() 0 11 3
A fromString() 0 24 6
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AppUtils;
6
7
class ConvertHelper_Bool
8
{
9
    /**
10
     * @var array<mixed,bool>
11
     */
12
    protected static $booleanStrings = array(
13
        1 => true,
14
        0 => false,
15
        'true' => true,
16
        'false' => false,
17
        'yes' => true,
18
        'no' => false
19
    );
20
21
    /**
22
     * Converts a string, number or boolean value to a boolean value.
23
     *
24
     * @param mixed $string
25
     * @throws ConvertHelper_Exception
26
     * @return bool
27
     *
28
     * @see ConvertHelper::ERROR_INVALID_BOOLEAN_STRING
29
     */
30
    public static function fromString($string) : bool
31
    {
32
        if($string === '' || $string === null || !is_scalar($string))
33
        {
34
            return false;
35
        }
36
37
        if(is_bool($string))
38
        {
39
            return $string;
40
        }
41
42
        if(array_key_exists($string, self::$booleanStrings))
43
        {
44
            return self::$booleanStrings[$string];
45
        }
46
47
        throw new ConvertHelper_Exception(
48
            'Invalid string boolean representation',
49
            sprintf(
50
                'Cannot convert [%s] to a boolean value.',
51
                parseVariable($string)->enableType()->toString()
52
            ),
53
            ConvertHelper::ERROR_INVALID_BOOLEAN_STRING
54
        );
55
    }
56
57
    /**
58
     * Converts a boolean value to a string. Defaults to returning
59
     * 'true' or 'false', with the additional parameter it can also
60
     * return the 'yes' and 'no' variants.
61
     *
62
     * @param boolean|string $boolean
63
     * @param boolean $yesno
64
     * @return string
65
     * @throws ConvertHelper_Exception
66
     */
67
    public static function toString($boolean, bool $yesno = false) : string
68
    {
69
        // allow 'yes', 'true', 'no', 'false' string notations as well
70
        if(!is_bool($boolean)) {
71
            $boolean = self::fromString($boolean);
72
        }
73
74
        if ($boolean) {
75
            if ($yesno) {
76
                return 'yes';
77
            }
78
79
            return 'true';
80
        }
81
82
        if ($yesno) {
83
            return 'no';
84
        }
85
86
        return 'false';
87
    }
88
89
    /**
90
     * Checks if the specified string is a boolean value, which
91
     * includes string representations of boolean values, like
92
     * <code>yes</code> or <code>no</code>, and <code>true</code>
93
     * or <code>false</code>.
94
     *
95
     * @param mixed $value
96
     * @return boolean
97
     */
98
    public static function isBoolean($value) : bool
99
    {
100
        if(is_bool($value)) {
101
            return true;
102
        }
103
104
        if(!is_scalar($value)) {
105
            return false;
106
        }
107
108
        return array_key_exists($value, self::$booleanStrings);
109
    }
110
}
111