Skip to content
This repository has been archived by the owner on Nov 15, 2019. It is now read-only.

Commit

Permalink
Add some stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
Anahkiasen committed Jul 24, 2015
1 parent af94999 commit 78f0371
Show file tree
Hide file tree
Showing 7 changed files with 485 additions and 1 deletion.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
],
"require": {
"php": ">=5.5.9",
"illuminate/database": "^5.1"
"illuminate/database": "^5.1",
"illuminate/auth": "^5.1"
},
"require-dev": {
"fabpot/php-cs-fixer": "2.0.*@dev",
Expand Down
8 changes: 8 additions & 0 deletions src/Interfaces/ValidatableInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Arrounded\Database\Interfaces;

interface ValidatableInterface
{

}
76 changes: 76 additions & 0 deletions src/Models/AbstractModel.php
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);
}
}
224 changes: 224 additions & 0 deletions src/Models/AbstractUploadModel.php
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();
}
25 changes: 25 additions & 0 deletions src/Traits/Authenticatable.php
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);
}
}
48 changes: 48 additions & 0 deletions src/Traits/HasWebsite.php
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;
}
}
Loading

0 comments on commit 78f0371

Please sign in to comment.