HEX
Server: Microsoft-IIS/8.5
System: Windows NT YDAWBH120 6.3 build 9600 (Windows Server 2012 R2 Standard Edition) AMD64
User: tentjecom_web (0)
PHP: 7.4.14
Disabled: NONE
Upload Files
File: D:/HostingSpaces/SBogers95/rentman.io/app/Komma/Dynamic/Component/Component.php
<?php

namespace App\Komma\Dynamic\Component;

use App\Komma\Documents\DocumentsTrait;
use App\Komma\Documents\Kms\DocumentableInterface;
use App\Komma\Dynamic\ComponentArea\ComponentArea;
use App\Komma\Pages\Models\Page;
use App\Komma\Products\Models\Product;
use App\Komma\Users\Models\User;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\MorphToMany;
use Illuminate\Support\Collection;

/**
 * App\Komma\Dynamic\Component\Component
 *
 * @property int $id
 * @property int $component_type_id
 * @property int $component_area_id
 * @property int $sort_order
 * @property string $version
 * @property string $data
 * @property \Illuminate\Support\Carbon|null $created_at
 * @property \Illuminate\Support\Carbon|null $updated_at
 * @property-read \App\Komma\Dynamic\ComponentArea\ComponentArea $componentArea
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Komma\Dynamic\Component\Component whereComponentAreaId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Komma\Dynamic\Component\Component whereComponentTypeId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Komma\Dynamic\Component\Component whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Komma\Dynamic\Component\Component whereData($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Komma\Dynamic\Component\Component whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Komma\Dynamic\Component\Component whereSortOrder($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Komma\Dynamic\Component\Component whereUpdatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Komma\Dynamic\Component\Component whereVersion($value)
 * @mixin \Eloquent
 * @property int|null $linkable_id
 * @property string|null $linkable_type
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Komma\Documents\Models\Document[] $documents
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Komma\Documents\Models\Document[] $images
 * @property-read \Illuminate\Database\Eloquent\Model|\Eloquent $linkable
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Komma\Dynamic\Component\Component whereLinkableId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Komma\Dynamic\Component\Component whereLinkableType($value)
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Komma\Pages\Models\Page[] $pages
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Komma\Users\Models\User[] $users
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Komma\Dynamic\Component\Component newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Komma\Dynamic\Component\Component newQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Komma\Dynamic\Component\Component query()
 */
class Component extends Model implements DocumentableInterface
{
    use DocumentsTrait;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = ['component_area_id', 'component_type_id', 'sort_order', 'version', 'data'];

    /**
     * @return BelongsTo
     */
    public function componentArea(): BelongsTo
    {
        return $this->belongsTo(ComponentArea::class);
    }

    //Polymorphic relations below this line. Safety goggles on please.
    public function users(): MorphToMany
    {
        return $this->morphedByMany(User::class, 'componentable');
    }

    public function pages(): MorphToMany
    {
        return $this->morphedByMany(Page::class, 'componentable');
    }

    public function products(): MorphToMany
    {
        return $this->morphedByMany(Product::class, 'componentable');
    }

    /**
     * Helper function which we call on site views
     * Attribute reference is the name of the attribute within the component
     * The method is usually getValue() but f.e. Documents uses a getValueForCollection()
     *
     * @param string $attributeKeyReference
     * @param string $attributeMethod
     * @return string|Collection
     */
    public function getValueFromAttribute(string $attributeKeyReference, string $attributeMethod = 'getValue')
    {
        // We're expecting a collection with Attributes
        if (! is_a($this->data, Collection::class)) {
            return '';
        }

        /** @var Collection $collection */
        $collection = $this->data;

        /** @var Attribute $attribute */
        $attribute = $collection->get($attributeKeyReference);

        // Make sure the attribute method exists
        if (! method_exists($attribute, $attributeMethod)) {
            return '';
        }

        if ($value = $this->getValueExceptions($attribute)) {
            return $value;
        }

        // Return value by calling the method
        return $attribute->{$attributeMethod}();
    }

    /**
     * Some attribute require some more work, this should be fixed when creating the ViewComponent
     *
     * @param $attribute
     * @return string
     */
    private function getValueExceptions($attribute)
    {
        switch (get_class($attribute)) {
            case Documents::class:
                return json_decode($attribute->getValue());
        }

        return '';
    }
}