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

Commit 78f0371

Browse files
committed
Add some stuff
1 parent af94999 commit 78f0371

File tree

7 files changed

+485
-1
lines changed

7 files changed

+485
-1
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
],
1414
"require": {
1515
"php": ">=5.5.9",
16-
"illuminate/database": "^5.1"
16+
"illuminate/database": "^5.1",
17+
"illuminate/auth": "^5.1"
1718
},
1819
"require-dev": {
1920
"fabpot/php-cs-fixer": "2.0.*@dev",
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace Arrounded\Database\Interfaces;
4+
5+
interface ValidatableInterface
6+
{
7+
8+
}

src/Models/AbstractModel.php

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
<?php
2+
namespace Arrounded\Database\Models;
3+
4+
use Arrounded\Collection;
5+
use Arrounded\Interfaces\ValidatableInterface;
6+
use Arrounded\Traits\Reflection\ReflectionModel;
7+
use Illuminate\Database\Eloquent\Model;
8+
9+
abstract class AbstractModel extends Model implements ValidatableInterface
10+
{
11+
use ReflectionModel;
12+
13+
/**
14+
* The attributes to cast on serialization.
15+
*
16+
* @type array
17+
*/
18+
protected $casts = [
19+
'integer' => ['id'],
20+
];
21+
22+
//////////////////////////////////////////////////////////////////////
23+
/////////////////////////// RELATED CLASSES //////////////////////////
24+
//////////////////////////////////////////////////////////////////////
25+
26+
/**
27+
* Create a new Eloquent Collection instance.
28+
*
29+
* @param array $models
30+
*
31+
* @return \Illuminate\Database\Eloquent\Collection
32+
*/
33+
public function newCollection(array $models = [])
34+
{
35+
$custom = $this->getNamespace().'\Collection';
36+
if (class_exists($custom)) {
37+
return new $custom($models);
38+
}
39+
40+
return new Collection($models);
41+
}
42+
43+
//////////////////////////////////////////////////////////////////////
44+
/////////////////////////////// SCOPES ///////////////////////////////
45+
//////////////////////////////////////////////////////////////////////
46+
47+
/**
48+
* Order entries in a specific order.
49+
*
50+
* @param \Illuminate\Database\Eloquent\Builder $query
51+
* @param string $field
52+
* @param array $values
53+
*
54+
* @return \Illuminate\Database\Eloquent\Builder
55+
*/
56+
public function scopeOrderByField($query, $field, $values)
57+
{
58+
return $query->orderByRaw($field.' <> "'.implode('", '.$field.' <> "', $values).'"');
59+
}
60+
61+
/**
62+
* Get all models belonging to other models.
63+
*
64+
* @param \Illuminate\Database\Eloquent\Builder $query
65+
* @param string $relation
66+
* @param array $ids
67+
*
68+
* @return \Illuminate\Database\Eloquent\Builder
69+
*/
70+
public function scopeWhereBelongsTo($query, $relation, array $ids = [])
71+
{
72+
$ids = $ids ?: ['void'];
73+
74+
return $query->whereIn($relation.'_id', $ids);
75+
}
76+
}

src/Models/AbstractUploadModel.php

Lines changed: 224 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,224 @@
1+
<?php
2+
namespace Arrounded\Database\Models;
3+
4+
use Codesleeve\Stapler\Attachment;
5+
use Codesleeve\Stapler\AttachmentConfig;
6+
use Codesleeve\Stapler\ORM\EloquentTrait;
7+
use Codesleeve\Stapler\ORM\StaplerableInterface;
8+
use Illuminate\Database\Eloquent\Builder;
9+
use Illuminate\Support\Facades\Config;
10+
use Illuminate\Support\Facades\HTML;
11+
use Illuminate\Support\Str;
12+
13+
/**
14+
* @property Attachment file
15+
*/
16+
abstract class AbstractUploadModel extends AbstractModel implements StaplerableInterface
17+
{
18+
use EloquentTrait;
19+
20+
/**
21+
* The attributes that are mass assignable.
22+
*
23+
* @type array
24+
*/
25+
protected $fillable = [
26+
'file',
27+
'type',
28+
'illustrable_id',
29+
'illustrable_type',
30+
];
31+
32+
/**
33+
* @type array
34+
*/
35+
protected $appends = ['thumbs'];
36+
37+
/**
38+
* @param array $attributes
39+
*/
40+
public function __construct(array $attributes = [])
41+
{
42+
$this->hasAttachedFile('file', ['styles' => $this->getThumbnailsConfiguration()]);
43+
44+
parent::__construct($attributes);
45+
}
46+
47+
/**
48+
* @return \Illuminate\Database\Eloquent\Relations\MorphTo
49+
*/
50+
public function illustrable()
51+
{
52+
return $this->morphTo();
53+
}
54+
55+
/**
56+
* @return bool
57+
*/
58+
public function hasIllustrable()
59+
{
60+
return strpos($this->illustrable_type, 'Temporary') === false && $this->illustrable;
61+
}
62+
63+
/**
64+
* Call a method on the Attachment object.
65+
*
66+
* @param string $method
67+
* @param array $parameters
68+
*
69+
* @return mixed
70+
*/
71+
public function __call($method, $parameters)
72+
{
73+
if (method_exists($this->file, $method)) {
74+
return call_user_func_array([$this->file, $method], $parameters);
75+
}
76+
77+
return parent::__call($method, $parameters);
78+
}
79+
80+
//////////////////////////////////////////////////////////////////////
81+
/////////////////////////////// SCOPES ///////////////////////////////
82+
//////////////////////////////////////////////////////////////////////
83+
84+
/**
85+
* Scope to only the illustrables of an instance.
86+
*
87+
* @param Builder $query
88+
* @param AbstractModel $model
89+
*
90+
* @return Builder
91+
*/
92+
public function scopeIllustrable($query, AbstractModel $model)
93+
{
94+
return $query->where([
95+
'illustrable_type' => $model->getClass(),
96+
'illustrable_id' => $model->id,
97+
]);
98+
}
99+
100+
/**
101+
* Scope to only images.
102+
*
103+
* @param Builder $query
104+
*
105+
* @return Builder
106+
*/
107+
public function scopeWhereImages($query)
108+
{
109+
return $query->where('file_content_type', 'LIKE', 'image/%');
110+
}
111+
112+
//////////////////////////////////////////////////////////////////////
113+
/////////////////////////////// THUMBS ///////////////////////////////
114+
//////////////////////////////////////////////////////////////////////
115+
116+
/**
117+
* Check if the bound file is an image.
118+
*
119+
* @return bool
120+
*/
121+
public function isImage()
122+
{
123+
return Str::startsWith($this->file_content_type, 'image/');
124+
}
125+
126+
/**
127+
* Get an array of the image's thumbs.
128+
*
129+
* @return array
130+
*/
131+
public function getThumbsAttribute()
132+
{
133+
if (!$this->isImage()) {
134+
return [];
135+
}
136+
137+
$config = $this->getImageConfig();
138+
$this->file->setConfig($config);
139+
140+
// Fetch path to thumbnails
141+
$thumbs = [];
142+
foreach ($this->file->styles as $style) {
143+
$thumbs[$style->name] = $this->file->url($style->name);
144+
}
145+
146+
return $thumbs;
147+
}
148+
149+
/**
150+
* Reprocess the styles. This will create all the styles for the current image.
151+
*/
152+
public function reprocessStyles()
153+
{
154+
// styles only apply to images
155+
if (!$this->isImage()) {
156+
return;
157+
}
158+
159+
$config = $this->getImageConfig();
160+
$this->file->setConfig($config);
161+
162+
// Reprocess thumbnails
163+
$this->file->reprocess();
164+
}
165+
166+
/**
167+
* @return AttachmentConfig
168+
*/
169+
protected function getImageConfig()
170+
{
171+
// Get base configuration
172+
$config = Config::get('laravel-stapler::stapler');
173+
$config += Config::get('laravel-stapler::'.$config['storage']);
174+
175+
// Set styles
176+
$config['styles'] = $this->getThumbnailsConfiguration();
177+
$config['styles']['original'] = '';
178+
179+
return new AttachmentConfig('file', $config);
180+
}
181+
182+
/**
183+
* Renders the image at a certain size.
184+
*
185+
* @param string|null $size
186+
* @param array $attributes
187+
*
188+
* @return string
189+
*/
190+
public function render($size = null, $attributes = [])
191+
{
192+
$url = $this->file->url($size);
193+
$path = $this->file->path();
194+
if (!file_exists($path)) {
195+
$type = $this->hasIllustrable() ? $this->illustrable->getClassBasename() : null;
196+
$url = static::getPlaceholder($type);
197+
}
198+
199+
return HTML::image($url, null, $attributes);
200+
}
201+
202+
//////////////////////////////////////////////////////////////////////
203+
////////////////////////////// HELPERS ///////////////////////////////
204+
//////////////////////////////////////////////////////////////////////
205+
206+
/**
207+
* Get the placeholder image.
208+
*
209+
* @param string|null $type
210+
*
211+
* @return string|null
212+
*/
213+
public static function getPlaceholder($type = null)
214+
{
215+
return;
216+
}
217+
218+
/**
219+
* Get the available thumbnail sizes.
220+
*
221+
* @return array
222+
*/
223+
abstract protected function getThumbnailsConfiguration();
224+
}

src/Traits/Authenticatable.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
namespace Arrounded\Traits;
3+
4+
use Hash;
5+
use Illuminate\Auth\Authenticatable as CoreAuthenticatable;
6+
use Illuminate\Auth\Passwords\CanResetPassword;
7+
8+
/**
9+
* A model with Auth capabilities.
10+
*/
11+
trait Authenticatable
12+
{
13+
use CoreAuthenticatable;
14+
use CanResetPassword;
15+
16+
/**
17+
* Hash password before save.
18+
*
19+
* @param string $password
20+
*/
21+
public function setPasswordAttribute($password)
22+
{
23+
$this->attributes['password'] = Hash::make($password);
24+
}
25+
}

src/Traits/HasWebsite.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
namespace Arrounded\Database\Traits;
3+
4+
trait HasWebsite
5+
{
6+
////////////////////////////////////////////////////////////////////
7+
//////////////////////////// ATTRIBUTES ////////////////////////////
8+
////////////////////////////////////////////////////////////////////
9+
10+
/**
11+
* Mutator for website.
12+
*/
13+
public function getWebsiteAttribute()
14+
{
15+
return $this->sanitizeWebsite($this->attributes['website']);
16+
}
17+
18+
/**
19+
* Mutator for website.
20+
*
21+
* @param array $value
22+
*/
23+
public function setWebsiteAttribute($value)
24+
{
25+
$this->attributes['website'] = $this->sanitizeWebsite($value);
26+
}
27+
28+
/**
29+
* Prefix with http if not present.
30+
*
31+
* @param string $url
32+
*
33+
* @return string
34+
*/
35+
public function sanitizeWebsite($url)
36+
{
37+
// Don't sanitize what does not exists.
38+
if (!$url) {
39+
return '';
40+
}
41+
42+
if (substr($url, 0, 7) !== 'http://' && substr($url, 0, 8) !== 'https://') {
43+
$url = 'http://'.$url;
44+
}
45+
46+
return $url;
47+
}
48+
}

0 commit comments

Comments
 (0)