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