AbstractBoolean::isTrue()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 5
nc 3
nop 1
crap 3
1
<?php
2
/**
3
 *
4
 * This file is part of Aura for PHP.
5
 *
6
 * @license http://opensource.org/licenses/bsd-license.php BSD
7
 *
8
 */
9
namespace Aura\Filter\Rule;
10
11
/**
12
 *
13
 * Abstract rule for boolean filters.
14
 *
15
 * @package Aura.Filter
16
 *
17
 */
18
abstract class AbstractBoolean
19
{
20
    /**
21
     *
22
     * Pseudo-true representations.
23
     *
24
     * @var array
25
     *
26
     */
27
    protected $true = array('1', 'on', 'true', 't', 'yes', 'y');
28
29
    /**
30
     *
31
     * Pseudo-false representations; `null` and empty-string are *not* included.
32
     *
33
     * @var array
34
     *
35
     */
36
    protected $false = array('0', 'off', 'false', 'f', 'no', 'n');
37
38
    /**
39
     *
40
     * Is a value true-ish?
41
     *
42
     * @param mixed $value The value to check.
43
     *
44
     * @return bool
45
     *
46
     */
47 69
    protected function isTrue($value)
48
    {
49 69
        if (! $this->isBoolIsh($value)) {
50 2
            return false;
51
        }
52 67
        return $value === true
53 67
            || in_array(strtolower(trim($value)), $this->true);
54
    }
55
56
    /**
57
     *
58
     * Is a value false-ish?
59
     *
60
     * @param mixed $value The value to check.
61
     *
62
     * @return bool
63
     *
64
     */
65 37
    protected function isFalse($value)
66
    {
67 37
        if (! $this->isBoolIsh($value)) {
68 2
            return false;
69
        }
70 35
        return $value === false
71 35
            || in_array(strtolower(trim($value)), $this->false);
72
    }
73
74
    /**
75
     *
76
     * Can the value be checked for true/false-ish-ness?
77
     *
78
     * @param mixed $value The value to check.
79
     *
80
     * @return bool
81
     *
82
     */
83 69
    protected function isBoolIsh($value)
84
    {
85 69
        if (is_string($value) || is_int($value) || is_bool($value)) {
86 67
            return true;
87
        }
88 2
        return false;
89
    }
90
}
91