diff --git a/src/ORM/Eloquent53Trait.php b/src/ORM/Eloquent53Trait.php new file mode 100644 index 0000000..5982a79 --- /dev/null +++ b/src/ORM/Eloquent53Trait.php @@ -0,0 +1,166 @@ +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; + } +}