PluginProbe
WPIDE – File Manager & Code Editor / 3.0
WPIDE – File Manager & Code Editor v3.0
3.5.8 3.5.7 2.0.14 2.0.15 2.0.16 2.0.2 2.0.4 2.0.5 2.0.6 2.0.7 2.0.8 2.0.9 2.1 2.2 2.3 2.3.1 2.3.2 2.4.0 2.5 2.6 3.0 3.1 3.2 3.3 3.4 All 54 releases
wpide / vendor / rakit / validation / src / Rule.php

Rule.php in WPIDE – File Manager & Code Editor 3.0, at vendor/rakit/validation/src/Rule.php

233 lines 4.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Rakit\Validation;
4
5 use Rakit\Validation\MissingRequiredParameterException;
6
7 abstract class Rule
8 {
9 /** @var string */
10 protected $key;
11
12 /** @var \Rakit\Validation\Attribute|null */
13 protected $attribute;
14
15 /** @var \Rakit\Validation\Validation|null */
16 protected $validation;
17
18 /** @var bool */
19 protected $implicit = false;
20
21 /** @var array */
22 protected $params = [];
23
24 /** @var array */
25 protected $paramsTexts = [];
26
27 /** @var array */
28 protected $fillableParams = [];
29
30 /** @var string */
31 protected $message = "The :attribute is invalid";
32
33 abstract public function check($value): bool;
34
35 /**
36 * Set Validation class instance
37 *
38 * @param \Rakit\Validation\Validation $validation
39 * @return void
40 */
41 public function setValidation(Validation $validation)
42 {
43 $this->validation = $validation;
44 }
45
46 /**
47 * Set key
48 *
49 * @param string $key
50 * @return void
51 */
52 public function setKey(string $key)
53 {
54 $this->key = $key;
55 }
56
57 /**
58 * Get key
59 *
60 * @return string
61 */
62 public function getKey()
63 {
64 return $this->key ?: get_class($this);
65 }
66
67 /**
68 * Set attribute
69 *
70 * @param \Rakit\Validation\Attribute $attribute
71 * @return void
72 */
73 public function setAttribute(Attribute $attribute)
74 {
75 $this->attribute = $attribute;
76 }
77
78 /**
79 * Get attribute
80 *
81 * @return \Rakit\Validation\Attribute|null
82 */
83 public function getAttribute()
84 {
85 return $this->attribute;
86 }
87
88 /**
89 * Get parameters
90 *
91 * @return array
92 */
93 public function getParameters(): array
94 {
95 return $this->params;
96 }
97
98 /**
99 * Set params
100 *
101 * @param array $params
102 * @return \Rakit\Validation\Rule
103 */
104 public function setParameters(array $params): Rule
105 {
106 $this->params = array_merge($this->params, $params);
107 return $this;
108 }
109
110 /**
111 * Set parameters
112 *
113 * @param string $key
114 * @param mixed $value
115 * @return \Rakit\Validation\Rule
116 */
117 public function setParameter(string $key, $value): Rule
118 {
119 $this->params[$key] = $value;
120 return $this;
121 }
122
123 /**
124 * Fill $params to $this->params
125 *
126 * @param array $params
127 * @return \Rakit\Validation\Rule
128 */
129 public function fillParameters(array $params): Rule
130 {
131 foreach ($this->fillableParams as $key) {
132 if (empty($params)) {
133 break;
134 }
135 $this->params[$key] = array_shift($params);
136 }
137 return $this;
138 }
139
140 /**
141 * Get parameter from given $key, return null if it not exists
142 *
143 * @param string $key
144 * @return mixed
145 */
146 public function parameter(string $key)
147 {
148 return isset($this->params[$key])? $this->params[$key] : null;
149 }
150
151 /**
152 * Set parameter text that can be displayed in error message using ':param_key'
153 *
154 * @param string $key
155 * @param string $text
156 * @return void
157 */
158 public function setParameterText(string $key, string $text)
159 {
160 $this->paramsTexts[$key] = $text;
161 }
162
163 /**
164 * Get $paramsTexts
165 *
166 * @return array
167 */
168 public function getParametersTexts(): array
169 {
170 return $this->paramsTexts;
171 }
172
173 /**
174 * Check whether this rule is implicit
175 *
176 * @return boolean
177 */
178 public function isImplicit(): bool
179 {
180 return $this->implicit;
181 }
182
183 /**
184 * Just alias of setMessage
185 *
186 * @param string $message
187 * @return \Rakit\Validation\Rule
188 */
189 public function message(string $message): Rule
190 {
191 return $this->setMessage($message);
192 }
193
194 /**
195 * Set message
196 *
197 * @param string $message
198 * @return \Rakit\Validation\Rule
199 */
200 public function setMessage(string $message): Rule
201 {
202 $this->message = $message;
203 return $this;
204 }
205
206 /**
207 * Get message
208 *
209 * @return string
210 */
211 public function getMessage(): string
212 {
213 return $this->message;
214 }
215
216 /**
217 * Check given $params must be exists
218 *
219 * @param array $params
220 * @return void
221 * @throws \Rakit\Validation\MissingRequiredParameterException
222 */
223 protected function requireParameters(array $params)
224 {
225 foreach ($params as $param) {
226 if (!isset($this->params[$param])) {
227 $rule = $this->getKey();
228 throw new MissingRequiredParameterException("Missing required parameter '{$param}' on rule '{$rule}'");
229 }
230 }
231 }
232 }
233