Completed
Push — master ( b523b7...cbe5c5 )
by Woody
10s
created

Boolean::__construct()   B

Complexity

Conditions 5
Paths 6

Size

Total Lines 24
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 5

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 24
ccs 14
cts 14
cp 1
rs 8.5125
cc 5
eloc 12
nc 6
nop 2
crap 5
1
<?php
2
3
namespace Equip\ValueObject;
4
5
use InvalidArgumentException;
6
7
class Boolean
8
{
9
    private $value;
10
11 17
    public function __construct($value, $default = null)
12
    {
13 17
        static $empty_values = [null, ''];
14
15 17
        if (is_bool($default) && in_array($value, $empty_values, true)) {
16 1
            $value = $default;
17 1
        }
18
19 17
        if (!is_scalar($value)) {
20 2
            throw new InvalidArgumentException('Value must a possible boolean');
21
        }
22
23
        $options = [
24 15
            'flags' => \FILTER_NULL_ON_FAILURE,
25 15
        ];
26
27 15
        $value = filter_var($value, \FILTER_VALIDATE_BOOLEAN, $options);
28
29 15
        if ($value === null) {
30 3
            throw new InvalidArgumentException('Value must be boolean');
31
        }
32
33 12
        $this->value = $value;
34 12
    }
35
36 12
    public function value()
37
    {
38 12
        return $this->value;
39
    }
40
}
41