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

Identifier::__construct()   B

Complexity

Conditions 4
Paths 3

Size

Total Lines 23
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 23
ccs 10
cts 10
cp 1
rs 8.7972
cc 4
eloc 13
nc 3
nop 2
crap 4
1
<?php
2
3
namespace Equip\ValueObject;
4
5
use InvalidArgumentException;
6
7
class Identifier
8
{
9
    /**
10
     * @var int|null
11
     */
12
    private $id;
13
14 13
    public function __construct($id, $is_required = true)
15
    {
16 13
        static $empty_values = [null, false, 0, '0', ''];
17
18 13
        if (in_array($id, $empty_values, true) && !$is_required) {
19 3
            $id = null;
20
        } else {
21
            $options = [
22 10
                'flags' => \FILTER_REQUIRE_SCALAR,
23
                'options' => [
24
                    'min_range' => 1,
25
                ],
26
            ];
27
28 10
            if (filter_var($id, \FILTER_VALIDATE_INT, $options) === false) {
29 7
                throw new InvalidArgumentException('Value must be a valid identifier');
30
            }
31
32 3
            $id = (int) $id;
33
        }
34
35 6
        $this->id = $id;
36 6
    }
37
38 6
    public function value()
39
    {
40 6
        return $this->id;
41
    }
42
}
43