1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of sensorario/resources repository |
5
|
|
|
* |
6
|
|
|
* (c) Simone Gentili <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Sensorario\Resources\Rulers; |
13
|
|
|
|
14
|
|
|
class Rule |
15
|
|
|
{ |
16
|
|
|
const TYPE_OBJECT = 'object'; |
17
|
|
|
|
18
|
|
|
const TYPE_SCALAR = 'scalar'; |
19
|
|
|
|
20
|
|
|
const TYPE_CUSTOM = 'custom-validator'; |
21
|
|
|
|
22
|
|
|
private static $avalableRuleTypes = [ |
23
|
|
|
self::TYPE_OBJECT, |
24
|
|
|
self::TYPE_SCALAR, |
25
|
|
|
self::TYPE_CUSTOM, |
26
|
|
|
]; |
27
|
|
|
|
28
|
|
|
private $rule; |
29
|
|
|
|
30
|
16 |
|
private function __construct(array $rule) |
31
|
|
|
{ |
32
|
16 |
|
$this->rule = $rule; |
33
|
16 |
|
} |
34
|
|
|
|
35
|
17 |
|
public static function fromArray(array $rule) |
36
|
|
|
{ |
37
|
17 |
|
if ([] === $rule) { |
38
|
1 |
|
throw new \LogicException( |
39
|
1 |
|
'rule type is not defined' |
40
|
|
|
); |
41
|
|
|
} |
42
|
|
|
|
43
|
16 |
|
return new self($rule); |
44
|
|
|
} |
45
|
|
|
|
46
|
13 |
|
public function ensureRuleNameIsValid() |
47
|
|
|
{ |
48
|
13 |
|
if (!$this->isValid()) { |
49
|
1 |
|
throw new \Sensorario\Resources\Exceptions\InvalidTypeException( |
50
|
|
|
'Oops! Invalid configuration!!!' |
51
|
1 |
|
. 'Type `' . key($this->rule) . '` is not valid. ' |
52
|
1 |
|
. 'Available types are ' . var_export(self::$avalableRuleTypes, true) |
53
|
|
|
); |
54
|
|
|
} |
55
|
12 |
|
} |
56
|
|
|
|
57
|
2 |
|
public function asArray() : array |
58
|
|
|
{ |
59
|
2 |
|
return $this->rule; |
60
|
|
|
} |
61
|
|
|
|
62
|
14 |
|
public function isValid() |
63
|
|
|
{ |
64
|
14 |
|
return in_array(key($this->rule), self::$avalableRuleTypes); |
65
|
|
|
} |
66
|
|
|
|
67
|
13 |
|
public function is($type) |
68
|
|
|
{ |
69
|
13 |
|
return key($this->rule) === $type; |
70
|
|
|
} |
71
|
|
|
|
72
|
1 |
|
public function isNot($type) |
73
|
|
|
{ |
74
|
1 |
|
return !$this->is($type); |
75
|
|
|
} |
76
|
|
|
|
77
|
12 |
|
public function isCustom() |
78
|
|
|
{ |
79
|
12 |
|
return $this->is(Rule::TYPE_CUSTOM); |
80
|
|
|
} |
81
|
|
|
|
82
|
10 |
|
public function isNotCustom() |
83
|
|
|
{ |
84
|
10 |
|
return !$this->isCustom(); |
85
|
|
|
} |
86
|
|
|
|
87
|
6 |
|
public function isNotMail() |
88
|
|
|
{ |
89
|
6 |
|
return 'email' != $this->getValue(); |
90
|
|
|
} |
91
|
|
|
|
92
|
8 |
|
public function getRuleType() |
93
|
|
|
{ |
94
|
8 |
|
return key($this->rule); |
95
|
|
|
} |
96
|
|
|
|
97
|
2 |
|
public function isObject() |
98
|
|
|
{ |
99
|
2 |
|
return isset($this->rule[self::TYPE_OBJECT]); |
100
|
|
|
} |
101
|
|
|
|
102
|
1 |
|
public function getObjectType() |
103
|
|
|
{ |
104
|
1 |
|
return $this->rule[self::TYPE_OBJECT]; |
105
|
|
|
} |
106
|
|
|
|
107
|
10 |
|
public function getValue() |
108
|
|
|
{ |
109
|
10 |
|
return current($this->rule); |
110
|
|
|
} |
111
|
|
|
|
112
|
2 |
|
public function getExpectedType() |
113
|
|
|
{ |
114
|
2 |
|
$expectedType = $this->isObject() |
115
|
1 |
|
? $this->getObjectType() |
116
|
2 |
|
: 'undefined'; |
117
|
|
|
|
118
|
2 |
|
return $this->getRuleType() == self::TYPE_SCALAR |
119
|
1 |
|
? $this->getValue() |
120
|
2 |
|
: $expectedType; |
121
|
|
|
} |
122
|
|
|
|
123
|
1 |
|
public function isValueNotAnObject() |
124
|
|
|
{ |
125
|
1 |
|
return 'array' !== $this->getValue(); |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|