'123456', 'password_confirmation' => '123456'] * new Validator($params)->addRule('password', 'mustMatch|password_confirmation'); * */ public function mustMatch($key, $ruleString, $value, $params) { $keys = explode('|', $ruleString); if (count($keys) < 2) { throw new Exception("validator rules not valid for $$ruleString"); } $compare1 = $this->_getParamValue($keys[1], $params); $compare2 = $this->_getParamValue($key, $params); if (is_null($compare1) || is_null($compare2)) { return "VALIDATOR.MUST_MATCH.MUST_NOT_EMPTY"; } if ($compare1 != $compare2) { return "VALIDATOR.MUST_MATCH.NOT_MATCH"; } return null; } /** * @param string $key pattern : file|image,document|2048 * * pattern for | : index 0 must be file, index 1 is file type, for the rest current support only maxSize * * @example * * $request = ['my_file' => $file, 'others' => '1234'] * new Validator($params)->addRule('my_file', 'file|image,document|2048'); * */ public function file($key, $ruleString, $value, $params) { $log = new Log(0); if (!($value instanceof UploadedFile)) { $log->error("ValidatorRuleCollection [file]: file is not file"); return 'VALIDATOR.FILE.IS_NOT_FILE'; } $keys = explode('|', $ruleString); if (count($keys) == 0) { throw new Exception("$key validator rules not valid for $$ruleString"); } $validatedMime = []; if (strpos($keys[1], ',') == false) { if (!in_array($keys[1], ['image', 'document'])) { $log->error("ValidatorRuleCollection [file]: mime not supported {$keys[1]}"); return 'VALIDATOR.FILE.MIME'; } $validatedMime = [$keys[1]]; } else { $exp = explode(',', $keys[1]); for ($i = 0; $i < count($exp); $i++) { if (!in_array($exp[$i], ['image', 'document'])) { $log->error("ValidatorRuleCollection [file]: mime not supported {$exp[$i]}"); return 'VALIDATOR.FILE.MIME'; } } $validatedMime = $exp; } $maxSize = 4096; if (isset($keys[2]) && is_numeric($keys[2])) { $maxSize = (int) $maxSize; } $sizeInKb = $value->getSize() / 1024; if ($sizeInKb > $maxSize) { $log->error("ValidatorRuleCollection [file]: max size {$sizeInKb} higher than allowed $maxSize"); return "VALIDATOR.FILE.MAX_SIZE"; } $mime = $value->getMimeType(); if (in_array('image', $validatedMime)) { if (!in_array($mime, $this->allowedMimeType)) { $log->error("ValidatorRuleCollection [file]: image mime type not allowed $mime"); return 'VALIDATOR.FILE.IMAGE_INVALID'; } } if (in_array('document', $validatedMime)) { if (!in_array($mime, $this->allowedMimeType)) { $log->error("ValidatorRuleCollection [file]: document mime type not allowed $mime"); return 'VALIDATOR.FILE.DOCUMENT_INVALID'; } } return null; } public function required($key, $ruleString, $value, $params) { if (!isset($value) || strlen($value) == 0) return 'VALIDATOR.REQUIRED'; return null; } public function _getParamValue($key, $params) { if (array_key_exists($key, $params)) { return $params[$key]; } return null; } }