Completed
Push — master ( 699dc6...d8f60e )
by Changwan
05:50
created

BooleanCatser::cast()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 3
nop 1
dl 0
loc 10
ccs 6
cts 6
cp 1
crap 3
rs 9.4285
c 0
b 0
f 0
1
<?php
2
namespace Wandu\Caster\Caster;
3
4
use Wandu\Caster\CasterInterface;
5
6
class BooleanCatser implements CasterInterface
7
{
8
    /** @var array */
9
    protected $allowedBooleanString = [
10
        '0', 'false', 'False', 'FALSE', 'n', 'N', 'no', 'No', 'NO', 'off', 'Off', 'OFF'
11
    ];
12
13
    /**
14
     * @param array $allowedBooleanString
15
     */
16 80
    public function __construct(array $allowedBooleanString = null)
17
    {
18 80
        if ($allowedBooleanString) {
19
            $this->allowedBooleanString = $allowedBooleanString;
20
        }
21 80
    }
22
23
    /**
24
     * {@inheritdoc}
25
     */
26 15
    public function cast($value)
27
    {
28 15
        if ($value === null) {
29 2
            return false;
30
        }
31 13
        if (in_array($value, $this->allowedBooleanString)) {
32 8
            return false;
33
        }
34 5
        return (bool) $value;
35
    }
36
}
37