mirror of
https://gitlab.com/CIEFWorldwideSdnBhd/exchange-2.0.git
synced 2026-08-27 08:24:06 +00:00
large commit
This commit is contained in:
@@ -2,58 +2,85 @@
|
||||
|
||||
namespace App\Classes\Modules\Documents\DataTransferObjects;
|
||||
|
||||
use App\Classes\Interfaces\DataTransferObject;
|
||||
use App\Classes\Exceptions\MalformedRequestException;
|
||||
use App\Classes\General\Interfaces\DataTransferObject;
|
||||
use App\Classes\ValueObjects\Constants\FileType;
|
||||
use Illuminate\Support\Str;
|
||||
use Intervention\Image\ImageManager;
|
||||
|
||||
class FileObject implements DataTransferObject
|
||||
{
|
||||
/** @var int|null */
|
||||
private $document_id;
|
||||
|
||||
/** @var text|null */
|
||||
private $file;
|
||||
|
||||
/** @var string|null */
|
||||
private $file_type;
|
||||
/** @var string */
|
||||
private $data;
|
||||
|
||||
/**
|
||||
* CompanyObject constructor.
|
||||
* @param int|null $document_id
|
||||
* @param null|string $file
|
||||
* @param string|null $file_type
|
||||
* FileInfoObject constructor.
|
||||
* @param string $data
|
||||
*/
|
||||
public function __construct(
|
||||
?int $document_id,
|
||||
?string $file,
|
||||
?string $file_type
|
||||
)
|
||||
public function __construct(string $data)
|
||||
{
|
||||
$this->document_id = $document_id;
|
||||
$this->file = $file;
|
||||
$this->file_type = $file_type;
|
||||
$this->data = $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int|null
|
||||
* @return \Intervention\Image\Image|string
|
||||
* @throws MalformedRequestException
|
||||
*/
|
||||
public function getDocumentId(): ?int
|
||||
public function getData()
|
||||
{
|
||||
return $this->document_id;
|
||||
return $this->getExtension() === 'pdf' ? $this->data : (new imageManager())->make($this->data);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string|null
|
||||
* @return string
|
||||
*/
|
||||
public function getFile(): ?string
|
||||
public function getFileName(): string
|
||||
{
|
||||
return $this->file;
|
||||
return (string) Str::uuid();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string|null
|
||||
* @return string
|
||||
*/
|
||||
public function getFileType(): ?string
|
||||
public function getMimeType(): string
|
||||
{
|
||||
return $this->file_type;
|
||||
return finfo_file(finfo_open(), $this->data, FILEINFO_MIME_TYPE);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
* @throws MalformedRequestException
|
||||
*/
|
||||
public function getExtension(): string
|
||||
{
|
||||
if(array_key_exists($this->getMimeType(), FileType::EXTENSION)){
|
||||
return FileType::EXTENSION[$this->getMimeType()];
|
||||
}
|
||||
|
||||
throw new MalformedRequestException('Failed to save file due to Unknown file extension');
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
* @throws MalformedRequestException
|
||||
*/
|
||||
public function getDecodedData(): string
|
||||
{
|
||||
return $this->getExtension() === 'pdf' ?
|
||||
base64_decode((explode('base64,', $this->getData()))[1]):
|
||||
$this->getData()->encode('data-url')->encoded;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $data
|
||||
*/
|
||||
public function setData(string $data): void
|
||||
{
|
||||
$this->data = $data;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user