Last active
April 14, 2016 10:50
-
-
Save sunshain/8d4f486d24dc6b281b6160f08bb607c7 to your computer and use it in GitHub Desktop.
Laravel custom Validation (mimetypes from url)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Providers; | |
use Validator; | |
use Illuminate\Support\ServiceProvider; | |
class AppServiceProvider extends ServiceProvider | |
{ | |
/** | |
* Bootstrap any application services. | |
* | |
* @return void | |
*/ | |
public function boot() | |
{ | |
Validator::extend('mimeurl', | |
function($attribute, $value, $parameters, $validator) { | |
$finfo = new \finfo(FILEINFO_MIME_TYPE); | |
$mimetype = $finfo->buffer(file_get_contents($value)); | |
return in_array($mimetype, $parameters); | |
} | |
); | |
Validator::replacer('mimeurl', | |
function($message, $attribute, $rule, $parameters) { | |
return str_replace(':values', implode(', ', $parameters), $message); | |
} | |
); | |
} | |
/** | |
* Register any application services. | |
* | |
* @return void | |
*/ | |
public function register() | |
{ | |
// | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment