Test Failed
Push — master ( 032cb3...6fb1e5 )
by Alexey
02:42
created

ValueObject::satisfy()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 1
crap 1
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
    private $alias;
23
24 52
    public function __construct($name, $value, $type = null)
25
    {
26 52
        $this->name = $name;
27 52
        $this->alias = $name;
28 52
        $this->value = $value;
29 52
        if (!in_array($type, $this->types())) throw new ValueObjectInvalidTypeException('Wrong type: ' . $type);
30 51
        $this->type = $type;
31 51
    }
32
33 39
    protected function types()
34 39
    {
35 39
        return [null, self::BOOL, self::FLOAT, self::INT, self::STRING];
36
    }
37
38
    public static function get($value, $type = null)
39
    {
40
        return new static($value, $type);
41
    }
42
43
    public function setAlias($alias)
44
    {
45
        $this->alias = $alias;
46
    }
47
48
    public function __invoke()
49 1
    {
50 1
        return $this->value;
51
    }
52
53 26
    public function name()
54 26
    {
55
        return $this->name;
56
    }
57 22
58 22
    public function alias()
59
    {
60
        return $this->alias;
61 40
    }
62 40
63
    public function value()
64
    {
65 1
        return $this->value;
66 1
    }
67
68
    public function type()
69 4
    {
70 4
        return $this->type;
71
    }
72
73
    public function isInt()
74 2
    {
75 2
        return $this->type === self::INT;
76
    }
77
78 2
    public function isString()
79 2
    {
80
        return $this->type === self::STRING;
81
    }
82 1
83 1
    public function isBool()
84
    {
85
        return $this->type === self::BOOL;
86 1
    }
87 1
88
    public function isFloat()
89
    {
90 52
        return $this->type === self::FLOAT;
91 52
    }
92
93
    public function isUndefinedType()
94
    {
95
        return is_null($this->type);
96
    }
97
98
    /**
99
     * @param Rule $rule
100
     * @return bool
101
     */
102 42
    public function satisfy(Rule $rule)
103 42
    {
104
        return $rule->process($this);
105
    }
106
107
    public function isNull()
108
    {
109
        return false;
110
    }
111
112
    protected function setName($name)
113
    {
114
        if (preg_match("/^(\S*)\s*as\s*(\S*)$/", $name, $matches)) {
115
            $this->name = $matches[1];
116
            $this->alias = $matches[2];
117
        } else {
118
            $this->name = $name;
119
            $this->alias = $name;
120
        }
121
122
    }
123
124
}