Completed
Push — master ( 0365a3...8a81d1 )
by Alexey
02:12
created

ValueObject::isUndefinedType()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 2
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
3
4
namespace Lexuss1979\Validol;
5
6
7
use Lexuss1979\Validol\Exceptions\ValueObjectInvalidTypeException;
8
9
class ValueObject
10
{
11
    const STRING = 'string';
12
    const INT = 'int';
13
    const FLOAT = 'float';
14
    const BOOL = 'bool';
15
16
    private $value;
17
    /**
18
     * @var null
19
     */
20
    private $type;
21
    private $name;
22
23 51
    public function __construct($name, $value, $type = null)
24
    {
25 51
        $this->name = $name;
26 51
        $this->value = $value;
27 51
        if(!in_array($type, $this->types())) throw new ValueObjectInvalidTypeException('Wrong type: '. $type);
28 50
        $this->type = $type;
29 50
    }
30
31 1
    public function __invoke(){
32 1
        return $this->value;
33
    }
34
    
35 41
    public function name(){
36 41
        return $this->name;
37
    }
38
39 39
    public function value(){
40 39
        return $this->value;
41
    }
42
43 1
    public function type(){
44 1
        return $this->type;
45
    }
46
47 4
    public function isInt(){
48 4
        return $this->type === self::INT;
49
    }
50
51
52 2
    public function isString(){
53 2
        return $this->type === self::STRING;
54
    }
55
56 2
    public function isBool(){
57 2
        return $this->type === self::BOOL;
58
    }
59
60 1
    public function isFloat(){
61 1
        return $this->type === self::FLOAT;
62
    }
63
64 1
    public function isUndefinedType(){
65 1
        return is_null($this->type);
66
    }
67
68 51
    protected function types(){
69 51
        return [null, self::BOOL, self::FLOAT, self::INT, self::STRING];
70
    }
71
72
    public static function get($value, $type = null){
73
        return new static($value, $type);
74
    }
75
76
77
}