This repository has been archived by the owner on Nov 15, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
af94999
commit 78f0371
Showing
7 changed files
with
485 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?php | ||
|
||
namespace Arrounded\Database\Interfaces; | ||
|
||
interface ValidatableInterface | ||
{ | ||
|
||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
<?php | ||
namespace Arrounded\Database\Models; | ||
|
||
use Arrounded\Collection; | ||
use Arrounded\Interfaces\ValidatableInterface; | ||
use Arrounded\Traits\Reflection\ReflectionModel; | ||
use Illuminate\Database\Eloquent\Model; | ||
|
||
abstract class AbstractModel extends Model implements ValidatableInterface | ||
{ | ||
use ReflectionModel; | ||
|
||
/** | ||
* The attributes to cast on serialization. | ||
* | ||
* @type array | ||
*/ | ||
protected $casts = [ | ||
'integer' => ['id'], | ||
]; | ||
|
||
////////////////////////////////////////////////////////////////////// | ||
/////////////////////////// RELATED CLASSES ////////////////////////// | ||
////////////////////////////////////////////////////////////////////// | ||
|
||
/** | ||
* Create a new Eloquent Collection instance. | ||
* | ||
* @param array $models | ||
* | ||
* @return \Illuminate\Database\Eloquent\Collection | ||
*/ | ||
public function newCollection(array $models = []) | ||
{ | ||
$custom = $this->getNamespace().'\Collection'; | ||
if (class_exists($custom)) { | ||
return new $custom($models); | ||
} | ||
|
||
return new Collection($models); | ||
} | ||
|
||
////////////////////////////////////////////////////////////////////// | ||
/////////////////////////////// SCOPES /////////////////////////////// | ||
////////////////////////////////////////////////////////////////////// | ||
|
||
/** | ||
* Order entries in a specific order. | ||
* | ||
* @param \Illuminate\Database\Eloquent\Builder $query | ||
* @param string $field | ||
* @param array $values | ||
* | ||
* @return \Illuminate\Database\Eloquent\Builder | ||
*/ | ||
public function scopeOrderByField($query, $field, $values) | ||
{ | ||
return $query->orderByRaw($field.' <> "'.implode('", '.$field.' <> "', $values).'"'); | ||
} | ||
|
||
/** | ||
* Get all models belonging to other models. | ||
* | ||
* @param \Illuminate\Database\Eloquent\Builder $query | ||
* @param string $relation | ||
* @param array $ids | ||
* | ||
* @return \Illuminate\Database\Eloquent\Builder | ||
*/ | ||
public function scopeWhereBelongsTo($query, $relation, array $ids = []) | ||
{ | ||
$ids = $ids ?: ['void']; | ||
|
||
return $query->whereIn($relation.'_id', $ids); | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,224 @@ | ||
<?php | ||
namespace Arrounded\Database\Models; | ||
|
||
use Codesleeve\Stapler\Attachment; | ||
use Codesleeve\Stapler\AttachmentConfig; | ||
use Codesleeve\Stapler\ORM\EloquentTrait; | ||
use Codesleeve\Stapler\ORM\StaplerableInterface; | ||
use Illuminate\Database\Eloquent\Builder; | ||
use Illuminate\Support\Facades\Config; | ||
use Illuminate\Support\Facades\HTML; | ||
use Illuminate\Support\Str; | ||
|
||
/** | ||
* @property Attachment file | ||
*/ | ||
abstract class AbstractUploadModel extends AbstractModel implements StaplerableInterface | ||
{ | ||
use EloquentTrait; | ||
|
||
/** | ||
* The attributes that are mass assignable. | ||
* | ||
* @type array | ||
*/ | ||
protected $fillable = [ | ||
'file', | ||
'type', | ||
'illustrable_id', | ||
'illustrable_type', | ||
]; | ||
|
||
/** | ||
* @type array | ||
*/ | ||
protected $appends = ['thumbs']; | ||
|
||
/** | ||
* @param array $attributes | ||
*/ | ||
public function __construct(array $attributes = []) | ||
{ | ||
$this->hasAttachedFile('file', ['styles' => $this->getThumbnailsConfiguration()]); | ||
|
||
parent::__construct($attributes); | ||
} | ||
|
||
/** | ||
* @return \Illuminate\Database\Eloquent\Relations\MorphTo | ||
*/ | ||
public function illustrable() | ||
{ | ||
return $this->morphTo(); | ||
} | ||
|
||
/** | ||
* @return bool | ||
*/ | ||
public function hasIllustrable() | ||
{ | ||
return strpos($this->illustrable_type, 'Temporary') === false && $this->illustrable; | ||
} | ||
|
||
/** | ||
* Call a method on the Attachment object. | ||
* | ||
* @param string $method | ||
* @param array $parameters | ||
* | ||
* @return mixed | ||
*/ | ||
public function __call($method, $parameters) | ||
{ | ||
if (method_exists($this->file, $method)) { | ||
return call_user_func_array([$this->file, $method], $parameters); | ||
} | ||
|
||
return parent::__call($method, $parameters); | ||
} | ||
|
||
////////////////////////////////////////////////////////////////////// | ||
/////////////////////////////// SCOPES /////////////////////////////// | ||
////////////////////////////////////////////////////////////////////// | ||
|
||
/** | ||
* Scope to only the illustrables of an instance. | ||
* | ||
* @param Builder $query | ||
* @param AbstractModel $model | ||
* | ||
* @return Builder | ||
*/ | ||
public function scopeIllustrable($query, AbstractModel $model) | ||
{ | ||
return $query->where([ | ||
'illustrable_type' => $model->getClass(), | ||
'illustrable_id' => $model->id, | ||
]); | ||
} | ||
|
||
/** | ||
* Scope to only images. | ||
* | ||
* @param Builder $query | ||
* | ||
* @return Builder | ||
*/ | ||
public function scopeWhereImages($query) | ||
{ | ||
return $query->where('file_content_type', 'LIKE', 'image/%'); | ||
} | ||
|
||
////////////////////////////////////////////////////////////////////// | ||
/////////////////////////////// THUMBS /////////////////////////////// | ||
////////////////////////////////////////////////////////////////////// | ||
|
||
/** | ||
* Check if the bound file is an image. | ||
* | ||
* @return bool | ||
*/ | ||
public function isImage() | ||
{ | ||
return Str::startsWith($this->file_content_type, 'image/'); | ||
} | ||
|
||
/** | ||
* Get an array of the image's thumbs. | ||
* | ||
* @return array | ||
*/ | ||
public function getThumbsAttribute() | ||
{ | ||
if (!$this->isImage()) { | ||
return []; | ||
} | ||
|
||
$config = $this->getImageConfig(); | ||
$this->file->setConfig($config); | ||
|
||
// Fetch path to thumbnails | ||
$thumbs = []; | ||
foreach ($this->file->styles as $style) { | ||
$thumbs[$style->name] = $this->file->url($style->name); | ||
} | ||
|
||
return $thumbs; | ||
} | ||
|
||
/** | ||
* Reprocess the styles. This will create all the styles for the current image. | ||
*/ | ||
public function reprocessStyles() | ||
{ | ||
// styles only apply to images | ||
if (!$this->isImage()) { | ||
return; | ||
} | ||
|
||
$config = $this->getImageConfig(); | ||
$this->file->setConfig($config); | ||
|
||
// Reprocess thumbnails | ||
$this->file->reprocess(); | ||
} | ||
|
||
/** | ||
* @return AttachmentConfig | ||
*/ | ||
protected function getImageConfig() | ||
{ | ||
// Get base configuration | ||
$config = Config::get('laravel-stapler::stapler'); | ||
$config += Config::get('laravel-stapler::'.$config['storage']); | ||
|
||
// Set styles | ||
$config['styles'] = $this->getThumbnailsConfiguration(); | ||
$config['styles']['original'] = ''; | ||
|
||
return new AttachmentConfig('file', $config); | ||
} | ||
|
||
/** | ||
* Renders the image at a certain size. | ||
* | ||
* @param string|null $size | ||
* @param array $attributes | ||
* | ||
* @return string | ||
*/ | ||
public function render($size = null, $attributes = []) | ||
{ | ||
$url = $this->file->url($size); | ||
$path = $this->file->path(); | ||
if (!file_exists($path)) { | ||
$type = $this->hasIllustrable() ? $this->illustrable->getClassBasename() : null; | ||
$url = static::getPlaceholder($type); | ||
} | ||
|
||
return HTML::image($url, null, $attributes); | ||
} | ||
|
||
////////////////////////////////////////////////////////////////////// | ||
////////////////////////////// HELPERS /////////////////////////////// | ||
////////////////////////////////////////////////////////////////////// | ||
|
||
/** | ||
* Get the placeholder image. | ||
* | ||
* @param string|null $type | ||
* | ||
* @return string|null | ||
*/ | ||
public static function getPlaceholder($type = null) | ||
{ | ||
return; | ||
} | ||
|
||
/** | ||
* Get the available thumbnail sizes. | ||
* | ||
* @return array | ||
*/ | ||
abstract protected function getThumbnailsConfiguration(); | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php | ||
namespace Arrounded\Traits; | ||
|
||
use Hash; | ||
use Illuminate\Auth\Authenticatable as CoreAuthenticatable; | ||
use Illuminate\Auth\Passwords\CanResetPassword; | ||
|
||
/** | ||
* A model with Auth capabilities. | ||
*/ | ||
trait Authenticatable | ||
{ | ||
use CoreAuthenticatable; | ||
use CanResetPassword; | ||
|
||
/** | ||
* Hash password before save. | ||
* | ||
* @param string $password | ||
*/ | ||
public function setPasswordAttribute($password) | ||
{ | ||
$this->attributes['password'] = Hash::make($password); | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<?php | ||
namespace Arrounded\Database\Traits; | ||
|
||
trait HasWebsite | ||
{ | ||
//////////////////////////////////////////////////////////////////// | ||
//////////////////////////// ATTRIBUTES //////////////////////////// | ||
//////////////////////////////////////////////////////////////////// | ||
|
||
/** | ||
* Mutator for website. | ||
*/ | ||
public function getWebsiteAttribute() | ||
{ | ||
return $this->sanitizeWebsite($this->attributes['website']); | ||
} | ||
|
||
/** | ||
* Mutator for website. | ||
* | ||
* @param array $value | ||
*/ | ||
public function setWebsiteAttribute($value) | ||
{ | ||
$this->attributes['website'] = $this->sanitizeWebsite($value); | ||
} | ||
|
||
/** | ||
* Prefix with http if not present. | ||
* | ||
* @param string $url | ||
* | ||
* @return string | ||
*/ | ||
public function sanitizeWebsite($url) | ||
{ | ||
// Don't sanitize what does not exists. | ||
if (!$url) { | ||
return ''; | ||
} | ||
|
||
if (substr($url, 0, 7) !== 'http://' && substr($url, 0, 8) !== 'https://') { | ||
$url = 'http://'.$url; | ||
} | ||
|
||
return $url; | ||
} | ||
} |
Oops, something went wrong.