Skip to content

Commit

Permalink
Add an L53 Eloquent specific trait since they broke backwards compati…
Browse files Browse the repository at this point in the history
…bility with laravel/framework#15722.
  • Loading branch information
tabennett committed Oct 7, 2016
1 parent 7da8cf0 commit fd487e7
Showing 1 changed file with 166 additions and 0 deletions.
166 changes: 166 additions & 0 deletions src/ORM/Eloquent53Trait.php
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;
}
}

0 comments on commit fd487e7

Please sign in to comment.