| 1 |
<?php |
| 2 |
|
| 3 |
namespace Rakit\Validation; |
| 4 |
|
| 5 |
class Validator |
| 6 |
{ |
| 7 |
use Traits\TranslationsTrait, Traits\MessagesTrait; |
| 8 |
|
| 9 |
/** @var array */ |
| 10 |
protected $translations = []; |
| 11 |
|
| 12 |
/** @var array */ |
| 13 |
protected $validators = []; |
| 14 |
|
| 15 |
/** @var bool */ |
| 16 |
protected $allowRuleOverride = false; |
| 17 |
|
| 18 |
/** @var bool */ |
| 19 |
protected $useHumanizedKeys = true; |
| 20 |
|
| 21 |
/** |
| 22 |
* Constructor |
| 23 |
* |
| 24 |
* @param array $messages |
| 25 |
* @return void |
| 26 |
*/ |
| 27 |
public function __construct(array $messages = []) |
| 28 |
{ |
| 29 |
$this->messages = $messages; |
| 30 |
$this->registerBaseValidators(); |
| 31 |
} |
| 32 |
|
| 33 |
/** |
| 34 |
* Register or override existing validator |
| 35 |
* |
| 36 |
* @param mixed $key |
| 37 |
* @param \Rakit\Validation\Rule $rule |
| 38 |
* @return void |
| 39 |
*/ |
| 40 |
public function setValidator(string $key, Rule $rule) |
| 41 |
{ |
| 42 |
$this->validators[$key] = $rule; |
| 43 |
$rule->setKey($key); |
| 44 |
} |
| 45 |
|
| 46 |
/** |
| 47 |
* Get validator object from given $key |
| 48 |
* |
| 49 |
* @param mixed $key |
| 50 |
* @return mixed |
| 51 |
*/ |
| 52 |
public function getValidator($key) |
| 53 |
{ |
| 54 |
return isset($this->validators[$key]) ? $this->validators[$key] : null; |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* Validate $inputs |
| 59 |
* |
| 60 |
* @param array $inputs |
| 61 |
* @param array $rules |
| 62 |
* @param array $messages |
| 63 |
* @return Validation |
| 64 |
*/ |
| 65 |
public function validate(array $inputs, array $rules, array $messages = []): Validation |
| 66 |
{ |
| 67 |
$validation = $this->make($inputs, $rules, $messages); |
| 68 |
$validation->validate(); |
| 69 |
return $validation; |
| 70 |
} |
| 71 |
|
| 72 |
/** |
| 73 |
* Given $inputs, $rules and $messages to make the Validation class instance |
| 74 |
* |
| 75 |
* @param array $inputs |
| 76 |
* @param array $rules |
| 77 |
* @param array $messages |
| 78 |
* @return Validation |
| 79 |
*/ |
| 80 |
public function make(array $inputs, array $rules, array $messages = []): Validation |
| 81 |
{ |
| 82 |
$messages = array_merge($this->messages, $messages); |
| 83 |
$validation = new Validation($this, $inputs, $rules, $messages); |
| 84 |
$validation->setTranslations($this->getTranslations()); |
| 85 |
|
| 86 |
return $validation; |
| 87 |
} |
| 88 |
|
| 89 |
/** |
| 90 |
* Magic invoke method to make Rule instance |
| 91 |
* |
| 92 |
* @param string $rule |
| 93 |
* @return Rule |
| 94 |
* @throws RuleNotFoundException |
| 95 |
*/ |
| 96 |
public function __invoke(string $rule): Rule |
| 97 |
{ |
| 98 |
$args = func_get_args(); |
| 99 |
$rule = array_shift($args); |
| 100 |
$params = $args; |
| 101 |
$validator = $this->getValidator($rule); |
| 102 |
if (!$validator) { |
| 103 |
throw new RuleNotFoundException("Validator '{$rule}' is not registered", 1); |
| 104 |
} |
| 105 |
|
| 106 |
$clonedValidator = clone $validator; |
| 107 |
$clonedValidator->fillParameters($params); |
| 108 |
|
| 109 |
return $clonedValidator; |
| 110 |
} |
| 111 |
|
| 112 |
/** |
| 113 |
* Initialize base validators array |
| 114 |
* |
| 115 |
* @return void |
| 116 |
*/ |
| 117 |
protected function registerBaseValidators() |
| 118 |
{ |
| 119 |
$baseValidator = [ |
| 120 |
'required' => new Rules\Required, |
| 121 |
'required_if' => new Rules\RequiredIf, |
| 122 |
'required_unless' => new Rules\RequiredUnless, |
| 123 |
'required_with' => new Rules\RequiredWith, |
| 124 |
'required_without' => new Rules\RequiredWithout, |
| 125 |
'required_with_all' => new Rules\RequiredWithAll, |
| 126 |
'required_without_all' => new Rules\RequiredWithoutAll, |
| 127 |
'email' => new Rules\Email, |
| 128 |
'alpha' => new Rules\Alpha, |
| 129 |
'numeric' => new Rules\Numeric, |
| 130 |
'alpha_num' => new Rules\AlphaNum, |
| 131 |
'alpha_dash' => new Rules\AlphaDash, |
| 132 |
'alpha_spaces' => new Rules\AlphaSpaces, |
| 133 |
'in' => new Rules\In, |
| 134 |
'not_in' => new Rules\NotIn, |
| 135 |
'min' => new Rules\Min, |
| 136 |
'max' => new Rules\Max, |
| 137 |
'between' => new Rules\Between, |
| 138 |
'url' => new Rules\Url, |
| 139 |
'integer' => new Rules\Integer, |
| 140 |
'boolean' => new Rules\Boolean, |
| 141 |
'ip' => new Rules\Ip, |
| 142 |
'ipv4' => new Rules\Ipv4, |
| 143 |
'ipv6' => new Rules\Ipv6, |
| 144 |
'extension' => new Rules\Extension, |
| 145 |
'array' => new Rules\TypeArray, |
| 146 |
'same' => new Rules\Same, |
| 147 |
'regex' => new Rules\Regex, |
| 148 |
'date' => new Rules\Date, |
| 149 |
'accepted' => new Rules\Accepted, |
| 150 |
'present' => new Rules\Present, |
| 151 |
'different' => new Rules\Different, |
| 152 |
'uploaded_file' => new Rules\UploadedFile, |
| 153 |
'mimes' => new Rules\Mimes, |
| 154 |
'callback' => new Rules\Callback, |
| 155 |
'before' => new Rules\Before, |
| 156 |
'after' => new Rules\After, |
| 157 |
'lowercase' => new Rules\Lowercase, |
| 158 |
'uppercase' => new Rules\Uppercase, |
| 159 |
'json' => new Rules\Json, |
| 160 |
'digits' => new Rules\Digits, |
| 161 |
'digits_between' => new Rules\DigitsBetween, |
| 162 |
'defaults' => new Rules\Defaults, |
| 163 |
'default' => new Rules\Defaults, // alias of defaults |
| 164 |
'nullable' => new Rules\Nullable, |
| 165 |
]; |
| 166 |
|
| 167 |
foreach ($baseValidator as $key => $validator) { |
| 168 |
$this->setValidator($key, $validator); |
| 169 |
} |
| 170 |
} |
| 171 |
|
| 172 |
/** |
| 173 |
* Given $ruleName and $rule to add new validator |
| 174 |
* |
| 175 |
* @param string $ruleName |
| 176 |
* @param \Rakit\Validation\Rule $rule |
| 177 |
* @return void |
| 178 |
*/ |
| 179 |
public function addValidator(string $ruleName, Rule $rule) |
| 180 |
{ |
| 181 |
if (!$this->allowRuleOverride && array_key_exists($ruleName, $this->validators)) { |
| 182 |
throw new RuleQuashException( |
| 183 |
"You cannot override a built in rule. You have to rename your rule" |
| 184 |
); |
| 185 |
} |
| 186 |
|
| 187 |
$this->setValidator($ruleName, $rule); |
| 188 |
} |
| 189 |
|
| 190 |
/** |
| 191 |
* Set rule can allow to be overrided |
| 192 |
* |
| 193 |
* @param boolean $status |
| 194 |
* @return void |
| 195 |
*/ |
| 196 |
public function allowRuleOverride(bool $status = false) |
| 197 |
{ |
| 198 |
$this->allowRuleOverride = $status; |
| 199 |
} |
| 200 |
|
| 201 |
/** |
| 202 |
* Set this can use humanize keys |
| 203 |
* |
| 204 |
* @param boolean $useHumanizedKeys |
| 205 |
* @return void |
| 206 |
*/ |
| 207 |
public function setUseHumanizedKeys(bool $useHumanizedKeys = true) |
| 208 |
{ |
| 209 |
$this->useHumanizedKeys = $useHumanizedKeys; |
| 210 |
} |
| 211 |
|
| 212 |
/** |
| 213 |
* Get $this->useHumanizedKeys value |
| 214 |
* |
| 215 |
* @return void |
| 216 |
*/ |
| 217 |
public function isUsingHumanizedKey(): bool |
| 218 |
{ |
| 219 |
return $this->useHumanizedKeys; |
| 220 |
} |
| 221 |
} |
| 222 |
|