File: D:/HostingSpaces/brameda/brameda.nl/app/Komma/Documents/Models/Document.php
<?php
namespace App\Komma\Documents\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\UploadedFile;
/**
* Class Document
*
* Represents a document in the database
*
* WARNING: The path attribute holds a FILE SYSTEM PATH. NOT a URL. Also see the pathAsUrl method.
*
* Transient properties (Not saved to the database)
*
* @property UploadedFile file
* @property string $state;
* @package App\Komma\Files\Model
* @property-read \Illuminate\Database\Eloquent\Model|\Eloquent $documentable
* @mixin \Eloquent
* @property int $id
* @property string $name
* @property string $file_system_path
* @property int $sort_order
* @property int $documentable_id
* @property string $documentable_type
* @property string $thumb_image_url
* @property string $small_image_url
* @property string $medium_image_url
* @property string $large_image_url
* @property int $uploadTrackerId
* @property \Carbon\Carbon|null $created_at
* @property \Carbon\Carbon|null $updated_at
* @property-read string file_url
* @method static \Illuminate\Database\Eloquent\Builder|\App\Komma\Documents\Models\Document whereCreatedAt($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Komma\Documents\Models\Document whereDocumentableId($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Komma\Documents\Models\Document whereDocumentableType($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Komma\Documents\Models\Document whereId($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Komma\Documents\Models\Document whereLargeImageUrl($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Komma\Documents\Models\Document whereMediumImageUrl($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Komma\Documents\Models\Document whereName($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Komma\Documents\Models\Document wherePath($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Komma\Documents\Models\Document whereSmallImageUrl($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Komma\Documents\Models\Document whereSortOrder($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Komma\Documents\Models\Document whereThumbImageUrl($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Komma\Documents\Models\Document whereUpdatedAt($value)
* @property string $mime_type
* @method static \Illuminate\Database\Eloquent\Builder|\App\Komma\Documents\Models\Document whereMimeType($value)
* @property string $key
* @method static \Illuminate\Database\Eloquent\Builder|\App\Komma\Documents\Models\Document whereKey($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Komma\Documents\Models\Document whereOriginalImageUrl($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Komma\Documents\Models\Document newModelQuery()
* @method static \Illuminate\Database\Eloquent\Builder|\App\Komma\Documents\Models\Document newQuery()
* @method static \Illuminate\Database\Eloquent\Builder|\App\Komma\Documents\Models\Document query()
*/
final class Document extends Model
{
const STATE_NEW = 'new';
const STATE_PRISTINE = 'pristine';
const STATE_MODIFIED = 'modified';
const STATE_DELETED = 'deleted';
/**
* The attributes that are mass assignable.
* WARNING: The path attribute holds a FILE SYSTEM PATH. NOT a URL. See the getFileUrlAttribute method.
*
* @var array
*/
protected $fillable = ['file_system_path', 'name', 'sort_order', 'documentable_id', 'documentable_type', 'thumb_image_url', 'small_image_url', 'medium_image_url', 'large_image_url'];
/**
* The attributes that should be hidden for serialization.
*
* @var array
*/
protected $hidden = ['file_system_path'];
/** @var UploadedFile $file associated with the document */
public $file;
/** @var string $state The state of the document */
public $state = self::STATE_PRISTINE;
public function documentable()
{
return $this->morphTo();
}
/**
* Convert the object into something JSON serializable.
* And makes sure that all the properties are ALWAYS included. Even when empty. So
* that for example javascript can count on them being present.
*
* @return array
*/
public function toArray()
{
$array = parent::toArray();
$array['state'] = $this->state;
$array['file_url'] = $this->file_url ? $this->file_url : '';
$array['name'] = $this->name ? $this->name : '';
$array['sort_order'] = $this->sort_order ? $this->sort_order : 0;
$array['documentable_id'] = $this->documentable_id ? $this->documentable_id : 0;
$array['documentable_type'] = $this->documentable_type ? $this->documentable_type : "";
$array['thumb_image_url'] = $this->thumb_image_url ? $this->thumb_image_url : "";
$array['small_image_url'] = $this->small_image_url ? $this->small_image_url : "";
$array['medium_image_url'] = $this->medium_image_url ? $this->medium_image_url : "";
$array['large_image_url'] = $this->large_image_url ? $this->large_image_url : "";
return $array;
}
/**
* Returns the file_system_path as an url.
*
* Notice. This is a accessor. @see https://laravel.com/docs/5.8/eloquent-mutators#defining-an-accessor
*
* The file_system_path may contain either / or \ directory seperators, depending on the operating system.
* Valid URLs ONLY use / as the "directory separator". So when you try to use the file_system_path attribute as an url
* path, it sometimes works and sometimes does not. This method ensures you will get the correct URL based on the file_system_path.
* And remember NEVER, in ANY SYSTEM, use a raw file system path as a URL, just because of this reason.
*
* Please make sure you know the difference between a URL and a PATH
*/
public function getFileUrlAttribute()
{
return str_replace('\\', '/', $this->file_system_path);
}
/**
* Checks if the document is a certain mime type
*
* @param string $method
* @param array $parameters
* @return mixed
*/
public function __call($method, $parameters)
{
//Check if we are going to check if the document is of a certain mime type type. If this document is of mime type "image/png". The type is "image" and we can magically check it with a call to the magic isImage method.
if($this->mime_type != '' && substr($method, 0, 2) == 'is')
{
$possibleMimeType = strtolower(substr($method, 2)); //The type part of the mime type. Not the tree part.
$mimeParts = explode('/', $this->mime_type);
$explodedMimeTypeType = array_shift($mimeParts);
return $explodedMimeTypeType == $possibleMimeType;
}
return parent::__call($method, $parameters); //Pass the call further into laravel and return its result
}
}