Validar MIMEs especificos
Código PHP:
Ver original
<?php
namespace App\Extend\Validators;
use Illuminate\Validation\Validator as IlluminateValidator;
class Mime extends IlluminateValidator
{
'mime' => 'The mime-type of file ":attribute" is invalid',
);
{
parent::__construct($translator, $data, $rules, $messages, $customAttributes);
$this->setCustomMessages($this->custom_messages);
}
public function validateMime($attribute, $value, $parameters) {
$isValid = false;
$fileMime = $value->getClientMimeType();
foreach($parameters as $param) {
$mime = $this->getMimesType($param);
if ($fileMime == $mime) {
$isValid = true;
break;
}
}else {
$isValid = true;
break;
}
}
}
return $isValid;
}
private function getMimesType($type) {
$mime = '';
switch($type) {
case 'bmp':
$mime = ['image/bmp','image/x-windows-bmp'];
break;
case 'gif':
$mime = 'image/gif';
break;
case 'jpg':
case 'jpeg':
$mime = ['image/jpeg','image/pjpeg'];
break;
case 'png':
$mime = 'image/png';
break;
case 'doc':
case 'dot':
$mime = 'application/msword';
break;
case 'docx':
$mime = 'application/vnd.openxmlformats-officedocument.wordprocessingml.document';
break;
case 'dotx':
$mime = 'application/vnd.openxmlformats-officedocument.wordprocessingml.template';
break;
case 'xls':
case 'xlt':
case 'xla':
$mime = 'application/vnd.ms-excel';
break;
case 'xlsx':
$mime = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet';
break;
case 'xltx':
$mime = 'application/vnd.openxmlformats-officedocument.spreadsheetml.template';
case 'ppt':
case 'pot':
case 'pps':
case 'ppa':
$mime = 'application/vnd.ms-powerpoint';
break;
case 'pptx':
$mime = 'application/vnd.openxmlformats-officedocument.presentationml.presentation';
break;
case 'potx':
$mime = 'application/vnd.openxmlformats-officedocument.presentationml.template';
break;
case 'ppsx':
$mime = 'application/vnd.openxmlformats-officedocument.presentationml.slideshow';
break;
case 'pdf':
$mime = 'application/pdf';
break;
}
return $mime;
}
}
El validador anterior lo agrego desde un provider
Código PHP:
Ver original
<?php
namespace App\Providers;
use Validator;
use App\Extend\Validators\Mime;
use Illuminate\Support\ServiceProvider;
class MimeValidateServiceProvider extends ServiceProvider
{
public function boot() {
Validator::resolver(function($translator, $data, $rules, $messages) {
return new Mime($translator, $data, $rules, $messages);
});
}
public function register()
{
}
}
Y el provider lo agrego desde app.php
Código PHP:
Ver original
/*
* Application Service Providers...
*/
'App\Providers\AppServiceProvider',
'App\Providers\BusServiceProvider',
'App\Providers\ConfigServiceProvider',
'App\Providers\EventServiceProvider',
'App\Providers\RouteServiceProvider',
'App\Providers\MimeValidateServiceProvider',
El punto es que si agrego un nuevo validador siguiendo los mismos pasos da conflicto indicando por ejemplo:
Method [validateMime] does not exist.
Agradezco toda ayuda que me puedan proporcionar
 
 



