-
Notifications
You must be signed in to change notification settings - Fork 144
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add an L53 Eloquent specific trait since they broke backwards compati…
…bility with laravel/framework#15722.
- Loading branch information
Showing
1 changed file
with
166 additions
and
0 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,166 @@ | ||
<?php | ||
|
||
namespace Codesleeve\Stapler\ORM; | ||
|
||
use Codesleeve\Stapler\Factories\Attachment as AttachmentFactory; | ||
|
||
trait EloquentTrait53 | ||
{ | ||
/** | ||
* All of the model's current file attachments. | ||
* | ||
* @var array | ||
*/ | ||
protected $attachedFiles = []; | ||
|
||
/** | ||
* Accessor method for the $attachedFiles property. | ||
* | ||
* @return array | ||
*/ | ||
public function getAttachedFiles() | ||
{ | ||
return $this->attachedFiles; | ||
} | ||
|
||
/** | ||
* Add a new file attachment type to the list of available attachments. | ||
* This function acts as a quasi constructor for this trait. | ||
* | ||
* @param string $name | ||
* @param array $options | ||
*/ | ||
public function hasAttachedFile($name, array $options = []) | ||
{ | ||
$attachment = AttachmentFactory::create($name, $options); | ||
$attachment->setInstance($this); | ||
$this->attachedFiles[$name] = $attachment; | ||
} | ||
|
||
/** | ||
* The "booting" method of the model. | ||
*/ | ||
public static function boot() | ||
{ | ||
parent::boot(); | ||
|
||
static::bootStapler(); | ||
} | ||
|
||
/** | ||
* Register eloquent event handlers. | ||
* We'll spin through each of the attached files defined on this class | ||
* and register callbacks for the events we need to observe in order to | ||
* handle file uploads. | ||
*/ | ||
public static function bootStapler() | ||
{ | ||
static::saved(function ($instance) { | ||
foreach ($instance->attachedFiles as $attachedFile) { | ||
$attachedFile->afterSave($instance); | ||
} | ||
}); | ||
|
||
static::deleting(function ($instance) { | ||
foreach ($instance->attachedFiles as $attachedFile) { | ||
$attachedFile->beforeDelete($instance); | ||
} | ||
}); | ||
|
||
static::deleted(function ($instance) { | ||
foreach ($instance->attachedFiles as $attachedFile) { | ||
$attachedFile->afterDelete($instance); | ||
} | ||
}); | ||
} | ||
|
||
/** | ||
* Get an attribute from the model. | ||
* | ||
* @param string $key | ||
* @return mixed | ||
*/ | ||
public function getAttribute($key) | ||
{ | ||
if (array_key_exists($key, $this->attachedFiles)) { | ||
return $this->attachedFiles[$key]; | ||
} | ||
|
||
return parent::getAttribute($key); | ||
} | ||
|
||
/** | ||
* Set a given attribute on the model. | ||
* | ||
* @param string $key | ||
* @param mixed $value | ||
* @return $this | ||
*/ | ||
public function setAttribute($key, $value) | ||
{ | ||
if (array_key_exists($key, $this->attachedFiles)) { | ||
if ($value) { | ||
$attachedFile = $this->attachedFiles[$key]; | ||
$attachedFile->setUploadedFile($value); | ||
} | ||
|
||
return; | ||
} | ||
|
||
parent::setAttribute($key, $value); | ||
} | ||
|
||
/** | ||
* Get a selection of the current attributes on the model. | ||
* | ||
* @param array $keys | ||
* | ||
* @return array | ||
*/ | ||
public function getAttributes($keys = []) | ||
{ | ||
return array_merge($this->attachedFiles, parent::getAttributes()); | ||
} | ||
|
||
/** | ||
* Return the image paths (across all styles) for a given attachment. | ||
* | ||
* @param string $attachmentName | ||
* @return array | ||
*/ | ||
public function pathsForAttachment($attachmentName) | ||
{ | ||
$paths = []; | ||
|
||
if (isset($this->attachedFiles[$attachmentName])) { | ||
$attachment = $this->attachedFiles[$attachmentName]; | ||
|
||
foreach ($attachment->styles as $style) { | ||
$paths[$style->name] = $attachment->path($style->name); | ||
} | ||
} | ||
|
||
return $paths; | ||
} | ||
|
||
/** | ||
* Return the image urls (across all styles) for a given attachment. | ||
* | ||
* @param string $attachmentName | ||
* @return array | ||
*/ | ||
public function urlsForAttachment($attachmentName) | ||
{ | ||
$urls = []; | ||
|
||
if (isset($this->attachedFiles[$attachmentName])) { | ||
$attachment = $this->attachedFiles[$attachmentName]; | ||
|
||
foreach ($attachment->styles as $style) { | ||
$urls[$style->name] = $attachment->url($style->name); | ||
} | ||
} | ||
|
||
return $urls; | ||
} | ||
} |