AbstractBooleanCase::isTrue()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 3
nc 3
nop 1
dl 0
loc 7
ccs 4
cts 4
cp 1
crap 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Mbright\Validation\Rule;
4
5
class AbstractBooleanCase
6
{
7
    /**
8
     * Pseudo-true representations.
9
     *
10
     * @var array
11
     */
12
    protected $true = array('1', 'on', 'true', 't', 'yes', 'y');
13
14
    /**
15
     * Pseudo-false representations; `null` and empty-string are *not* included.
16
     *
17
     * @var array
18
     */
19
    protected $false = array('0', 'off', 'false', 'f', 'no', 'n');
20
21
    /**
22
     * Is a value true-ish?
23
     *
24
     * @param mixed $value The value to check.
25
     *
26
     * @return bool
27
     */
28 225
    protected function isTrue($value)
29
    {
30 225
        if (!$this->isBoolIsh($value)) {
31 6
            return false;
32
        }
33
34 219
        return $value === true || in_array(strtolower(trim($value)), $this->true);
35
    }
36
37
    /**
38
     * Is a value false-ish?
39
     *
40
     * @param mixed $value The value to check.
41
     *
42
     * @return bool
43
     */
44 117
    protected function isFalse($value)
45
    {
46 117
        if (!$this->isBoolIsh($value)) {
47 6
            return false;
48
        }
49
50 111
        return $value === false || in_array(strtolower(trim($value)), $this->false);
51
    }
52
53
    /**
54
     * Can the value be checked for true/false-ish-ness?
55
     *
56
     * @param mixed $value The value to check.
57
     *
58
     * @return bool
59
     */
60 225
    protected function isBoolIsh($value)
61
    {
62 225
        if (is_string($value) || is_int($value) || is_bool($value)) {
63 219
            return true;
64
        }
65
66 6
        return false;
67
    }
68
}
69