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

BooleanCatser   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 90%

Importance

Changes 0
Metric Value
dl 0
loc 31
ccs 9
cts 10
cp 0.9
rs 10
c 0
b 0
f 0
wmc 5
lcom 1
cbo 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 2
A cast() 0 10 3
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