Booleans   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 92
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 24
dl 0
loc 92
rs 10
c 0
b 0
f 0
wmc 14

5 Methods

Rating   Name   Duplication   Size   Complexity  
A validateAsString() 0 4 2
A convert() 0 3 2
A valueIsNullAndValid() 0 6 4
A validateTrueValuesArray() 0 15 3
A filter() 0 20 3
1
<?php
2
3
namespace TraderInteractive\Filter;
4
5
use TraderInteractive\Exceptions\FilterException;
6
7
/**
8
 * A collection of filters for booleans.
9
 */
10
final class Booleans
11
{
12
    /**
13
     * Filters $value to a boolean strictly.
14
     *
15
     * $value must be a bool or 'true' or 'false' disregarding case and whitespace.
16
     *
17
     * The return value is the bool, as expected by the \TraderInteractive\Filterer class.
18
     *
19
     * @param mixed $value       the value to filter to a boolean
20
     * @param bool  $allowNull   Set to true if NULL values are allowed. The filtered result of a NULL value is
21
     *                           NULL
22
     * @param array $trueValues  Array of values which represent the boolean true value. Values should be lower
23
     *                           cased
24
     * @param array $falseValues Array of values which represent the boolean false value. Values should be lower
25
     *                           cased
26
     *
27
     * @return bool|null the filtered $value
28
     *
29
     * @throws FilterException
30
     */
31
    public static function filter(
32
        $value,
33
        bool $allowNull = false,
34
        array $trueValues = ['true'],
35
        array $falseValues = ['false']
36
    ) {
37
        if (self::valueIsNullAndValid($allowNull, $value)) {
38
            return null;
39
        }
40
41
        if (is_bool($value)) {
42
            return $value;
43
        }
44
45
        self::validateAsString($value);
46
47
        $value = trim($value);
48
        $value = strtolower($value);
49
50
        return self::validateTrueValuesArray($value, $trueValues, $falseValues);
51
    }
52
53
    /**
54
     * Filters the boolean $value to the given $true and $false cases
55
     *
56
     * @param boolean $value The boolean value to convert.
57
     * @param mixed   $true  The value to return on the true case.
58
     * @param mixed   $false The value to return on the false case.
59
     *
60
     * @return mixed
61
     */
62
    public static function convert(bool $value, $true = 'true', $false = 'false')
63
    {
64
        return $value ? $true : $false;
65
    }
66
67
    private static function valueIsNullAndValid(bool $allowNull, $value = null) : bool
68
    {
69
        if ($allowNull === false && $value === null) {
70
            throw new FilterException('Value failed filtering, $allowNull is set to false');
71
        }
72
        return $allowNull === true && $value === null;
73
    }
74
75
    /**
76
     * @param $value
77
     *
78
     * @throws FilterException
79
     */
80
    private static function validateAsString($value)
81
    {
82
        if (!is_string($value)) {
83
            throw new FilterException('"' . var_export($value, true) . '" $value is not a string');
84
        }
85
    }
86
87
    private static function validateTrueValuesArray($value, array $trueValues, array $falseValues) : bool
88
    {
89
        if (in_array($value, $trueValues, true)) {
90
            return true;
91
        }
92
93
        if (in_array($value, $falseValues, true)) {
94
            return false;
95
        }
96
97
        throw new FilterException(
98
            sprintf(
99
                "%s is not '%s' disregarding case and whitespace",
100
                $value,
101
                implode("' or '", array_merge($trueValues, $falseValues))
102
            )
103
        );
104
    }
105
}
106