Passed
Push — master ( 5d30f6...461084 )
by Alexey
02:28
created

ValueObject   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 128
Duplicated Lines 0 %

Test Coverage

Coverage 84.31%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 22
eloc 40
c 2
b 0
f 0
dl 0
loc 128
ccs 43
cts 51
cp 0.8431
rs 10

20 Methods

Rating   Name   Duplication   Size   Complexity  
A satisfy() 0 3 1
A __invoke() 0 3 1
A alias() 0 3 1
A get() 0 3 1
A setType() 0 4 2
A isNull() 0 3 1
A isUndefinedType() 0 3 1
A setName() 0 8 2
A isFloat() 0 3 1
A isString() 0 3 1
A types() 0 3 1
A isInt() 0 3 1
A setAlias() 0 3 1
A isArray() 0 3 1
A isBool() 0 3 1
A type() 0 3 1
A name() 0 3 1
A isDate() 0 3 1
A __construct() 0 6 1
A value() 0 3 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
    const ARRAY = 'array';
16
    const DATE = 'date';
17
18
    private $value;
19
    /**
20
     * @var null
21
     */
22
    private $type;
23
    private $name;
24
    private $alias;
25
26 157
    public function __construct($name, $value, $type = null)
27
    {
28 157
        $this->name = $name;
29 157
        $this->alias = $name;
30 157
        $this->value = $value;
31 157
        $this->setType($type);
32 156
    }
33
34 157
    public function setType($type)
35
    {
36 157
        if (!in_array($type, $this->types())) throw new ValueObjectInvalidTypeException('Wrong type: ' . $type);
37 156
        $this->type = $type;
38 156
    }
39
40 157
    protected function types()
41
    {
42 157
        return [null, self::BOOL, self::FLOAT, self::INT, self::STRING, self::ARRAY, self::DATE];
43
    }
44
45
    public static function get($value, $type = null)
46
    {
47
        return new static($value, $type);
48
    }
49
50 138
    public function setAlias($alias)
51
    {
52 138
        $this->alias = $alias;
53 138
    }
54
55 1
    public function __invoke()
56
    {
57 1
        return $this->value;
58
    }
59
60 88
    public function name()
61
    {
62 88
        return $this->name;
63
    }
64
65 61
    public function alias()
66
    {
67 61
        return $this->alias;
68
    }
69
70 145
    public function value()
71
    {
72 145
        return $this->value;
73
    }
74
75 1
    public function type()
76
    {
77 1
        return $this->type;
78
    }
79
80 5
    public function isInt()
81
    {
82 5
        return $this->type === self::INT;
83
    }
84
85 3
    public function isString()
86
    {
87 3
        return $this->type === self::STRING;
88
    }
89
90 3
    public function isBool()
91
    {
92 3
        return $this->type === self::BOOL;
93
    }
94
95 2
    public function isFloat()
96
    {
97 2
        return $this->type === self::FLOAT;
98
    }
99
100 1
    public function isArray()
101
    {
102 1
        return $this->type === self::ARRAY;
103
    }
104
105 1
    public function isDate()
106
    {
107 1
        return $this->type === self::DATE;
108
    }
109
110 1
    public function isUndefinedType()
111
    {
112 1
        return is_null($this->type);
113
    }
114
115
    /**
116
     * @param Rule $rule
117
     * @return bool
118
     */
119 141
    public function satisfy(Rule $rule)
120
    {
121 141
        return $rule->process($this);
122
    }
123
124 138
    public function isNull()
125
    {
126 138
        return false;
127
    }
128
129
    protected function setName($name)
130
    {
131
        if (preg_match("/^(\S*)\s*as\s*(\S*)$/", $name, $matches)) {
132
            $this->name = $matches[1];
133
            $this->alias = $matches[2];
134
        } else {
135
            $this->name = $name;
136
            $this->alias = $name;
137
        }
138
139
    }
140
141
}