1 | <?php |
||
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 | private function __construct(array $rule) |
||
34 | |||
35 | public static function fromArray(array $rule) |
||
45 | |||
46 | public function ensureRuleNameIsValid() |
||
47 | { |
||
48 | if (!$this->isValid()) { |
||
49 | throw new \RuntimeException( |
||
50 | 'Oops! Invalid configuration!!!' |
||
51 | . 'Type `' . key($this->rule) . '` is not valid. ' |
||
52 | . 'Available types are ' . var_export(static::$avalableRuleTypes, true) |
||
|
|||
53 | ); |
||
54 | } |
||
55 | } |
||
56 | |||
57 | public function asArray() : array |
||
61 | |||
62 | public function isValid() |
||
66 | |||
67 | public function is($type) |
||
71 | |||
72 | public function isNot($type) |
||
76 | |||
77 | public function isCustom() |
||
81 | |||
82 | public function isNotCustom() |
||
86 | |||
87 | public function isNotMail() |
||
91 | |||
92 | public function getRuleType() |
||
96 | |||
97 | public function isObject() |
||
101 | |||
102 | public function getObjectType() |
||
106 | |||
107 | public function getValue() |
||
111 | |||
112 | public function getExpectedType() |
||
113 | { |
||
114 | $expectedType = $this->isObject() |
||
115 | ? $this->getObjectType() |
||
116 | : 'undefined'; |
||
117 | |||
118 | return $this->getRuleType() == self::TYPE_SCALAR |
||
119 | ? $this->getValue() |
||
120 | : $expectedType; |
||
121 | } |
||
122 | |||
123 | public function isValueNotAnObject() |
||
127 | } |
||
128 |
Let’s assume you have a class which uses late-static binding:
The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the
getSomeVariable()
on that sub-class, you will receive a runtime error:In the case above, it makes sense to update
SomeClass
to useself
instead: