File size: 2,262 Bytes
89a1081 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 | <?php
namespace App\Rules;
use Illuminate\Contracts\Validation\Rule;
class FileExtentionCheckRule implements Rule
{
/**
* Create a new rule instance.
*
* @return void
*/
public $extention;
public $type;
public $message;
public $counter;
public function __construct(mixed $extention,string $type = 'image' ,int $counter = 0)
{
$this->extention = $extention;
$this->type = $type;
$this->counter = $counter;
}
/**
* Determine if the validation rule passes.
*
* @param string $attribute
* @param mixed $value
* @return bool
*/
public function passes($attribute, $value) {
$flag = 1;
if(is_array($value)){
if(count($value) + $this->counter > (int) site_settings('max_file_upload')) {
$this->message = " ".translate("You Can Not Upload More Than ").site_settings('max_file_upload').translate(' File At a Time');
$flag = 0;
} else {
foreach($value as $file) {
$flag = $this->checkRule($file);
if($flag == 0){
break;
}
}
}
}
else{
$flag = $this->checkRule($value);
}
if( $flag == 1){
return true;
}
return false;
}
/**
* Get the validation error message.
*
* @return string
*/
public function message()
{
return $this->message;
}
public function checkRule(mixed $file) : int{
$fileSizeInBytes = $file->getSize();
$indicator = 1;
if( round($fileSizeInBytes / 1024) > (int) site_settings('max_file_size')){
$this->message = translate($this->type.' Size Must be Under '). site_settings('max_file_size'). translate('KB');
$indicator = 0;
} elseif (!in_array($file->getClientOriginalExtension(), $this->extention)) {
$this->message = translate($this->type.' Must be '.implode(", ", $this->extention).' Format');
$indicator = 0;
}
return $indicator;
}
}
|