Completed
Pull Request — master (#2)
by Woody
02:04
created

Identifier::value()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
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 12
    public function __construct($id, $is_required = true)
15
    {
16
        static $empty_values = [
17
            null,
18
            '0',
19
            '',
20
            0,
21 12
        ];
22
23 12
        if (in_array($id, $empty_values, true) && !$is_required) {
24 3
            $id = null;
25 3
        } else {
26
            $options = [
27 9
                'flags' => \FILTER_REQUIRE_SCALAR,
28
                'options' => [
29 9
                    'min_range' => 1,
30 9
                ],
31 9
            ];
32
33 9
            if (filter_var($id, \FILTER_VALIDATE_INT, $options) === false) {
34 6
                throw new InvalidArgumentException('Value must be a valid identifier');
35
            }
36
37 3
            $id = (int) $id;
38
        }
39
40 6
        $this->id = $id;
41 6
    }
42
43 6
    public function value()
44
    {
45 6
        return $this->id;
46
    }
47
}
48