Completed
Push — master ( 8a81d1...032cb3 )
by Alexey
02:10
created

ValueObject::name()   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 0
Metric Value
cc 1
eloc 1
c 0
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
    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
    public function setAlias($alias){
34 39
        $this->alias = $alias;
35 39
    }
36
37
    protected function setName($name){
38
        if(preg_match("/^(\S*)\s*as\s*(\S*)$/",$name, $matches)){
39
            $this->name = $matches[1];
40
            $this->alias = $matches[2];
41
        } else {
42
            $this->name = $name;
43
            $this->alias = $name;
44
        }
45
46
    }
47
48
49 1
    public function __invoke(){
50 1
        return $this->value;
51
    }
52
53 26
    public function name(){
54 26
        return $this->name;
55
    }
56
57 22
    public function alias(){
58 22
        return $this->alias;
59
    }
60
61 40
    public function value(){
62 40
        return $this->value;
63
    }
64
65 1
    public function type(){
66 1
        return $this->type;
67
    }
68
69 4
    public function isInt(){
70 4
        return $this->type === self::INT;
71
    }
72
73
74 2
    public function isString(){
75 2
        return $this->type === self::STRING;
76
    }
77
78 2
    public function isBool(){
79 2
        return $this->type === self::BOOL;
80
    }
81
82 1
    public function isFloat(){
83 1
        return $this->type === self::FLOAT;
84
    }
85
86 1
    public function isUndefinedType(){
87 1
        return is_null($this->type);
88
    }
89
90 52
    protected function types(){
91 52
        return [null, self::BOOL, self::FLOAT, self::INT, self::STRING];
92
    }
93
94
    public static function get($value, $type = null){
95
        return new static($value, $type);
96
    }
97
98
    /**
99
     * @param Rule $rule
100
     * @return bool
101
     */
102 42
    public function satisfy(Rule $rule) {
103 42
        return $rule->process($this);
104
    }
105
106
107
}