tethys/app/Rules/RdrFiletypes.php

57 lines
1.4 KiB
PHP
Raw Normal View History

2018-08-29 15:18:15 +00:00
<?php
namespace App\Rules;
use Illuminate\Contracts\Validation\Rule;
2019-04-02 16:03:29 +00:00
//use Illuminate\Support\Facades\Config;
use App\Models\MimeType;
2018-08-29 15:18:15 +00:00
class RdrFiletypes implements Rule
{
protected $filetypes;
// protected $maxFileSize;
/**
* Create a new rule instance.
*
* @return void
*/
public function __construct()
{
2019-04-02 16:03:29 +00:00
//$this->mimetypes = Config::get('enums.mimetypes_allowed', ['application/pdf']);
$this->mimetypes = MimeType::where('enabled', 1)
->pluck('name')
->toArray();
2018-08-29 15:18:15 +00:00
// $this->maxFileSize = Config::get('enums.max_filesize');
}
/**
* Determine if the validation rule passes.
*
* @param string $attribute
* @param mixed $value
* @return bool
*/
public function passes($attribute, $value)
{
//return Rule::in($this->filetypes);
2019-04-02 16:03:29 +00:00
$mimeType = $value->getMimeType();//"application/pdf"
2018-08-29 15:18:15 +00:00
return $value->getPath() != '' &&
2019-04-02 16:03:29 +00:00
in_array($mimeType, $this->mimetypes);
2019-02-22 17:07:00 +00:00
// in_array($value->guessExtension(), $this->filetypes); //file extension
// &&
2018-08-29 15:18:15 +00:00
// $this->getSize($attribute, $value) <= $this->maxFileSize;
}
/**
* Get the validation error message.
*
* @return string
*/
public function message()
{
return 'attribute :attribute has not a valid mime type.';
}
}