137 lines
4.9 KiB
PHP
137 lines
4.9 KiB
PHP
<?php
|
|
|
|
namespace Aiko;
|
|
|
|
use Exception;
|
|
use Symfony\Component\HttpFoundation\File\UploadedFile;
|
|
|
|
class ValidatorRuleCollection
|
|
{
|
|
|
|
private $allowedMimeType = [
|
|
'image/jpeg',
|
|
'image/png',
|
|
'image/jpg',
|
|
'video/mp4',
|
|
'application/pdf',
|
|
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
|
|
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheetapplication/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
|
|
'application/msword',
|
|
'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
|
|
'application/vnd.openxmlformats-officedocument.wordprocessingml.template',
|
|
'application/vnd.ms-word.document.macroEnabled.12',
|
|
'application/vnd.ms-word.template.macroEnabled.12',
|
|
'application/vnd.ms-excel',
|
|
'application/vnd.openxmlformats-officedocument.spreadsheetml.template',
|
|
'application/vnd.ms-excel.sheet.macroEnabled.12',
|
|
'application/vnd.ms-excel.template.macroEnabled.12',
|
|
'application/vnd.ms-excel.addin.macroEnabled.12',
|
|
'application/vnd.ms-excel.sheet.binary.macroEnabled.12'
|
|
];
|
|
|
|
/**
|
|
* @param string $key pattern : mustMatch|param_key_match
|
|
*
|
|
* @example
|
|
*
|
|
* $request = ['password' => '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;
|
|
}
|
|
}
|