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/bomacon/bomacon.nl/app/Components/ComponentType/Types/AbstractComponentType.php
<?php

namespace App\Components\ComponentType\Types;

use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Support\Collection;
use Illuminate\Support\Str;
use Komma\KMS\Core\Attributes\Attribute;
use Komma\KMS\Core\Attributes\Models\SelectOptionInterface;
use Komma\KMS\Core\Attributes\TextField;

/**
 * Class ComponentType
 */
abstract class AbstractComponentType implements Arrayable
{
    /** @var Collection */
    protected $attributes;

    /** @var string */
    protected $name = '';

    /** @var string */
    protected $icon = '';
    protected $iconPath = 'img/kms/dynamic/';

    /** @var int */
    protected $id = null;

    protected $iconOptions;

    const ICONS = [
        'null'                => 'None',
        'token-save-money'    => 'Save Money',
        'token-speech-bubble' => 'Speech Bubble',
        'token-clipboard'     => 'Clipboard',
        'token-team-meeting'  => 'Team Meeting',
    ];

    /**
     * ComponentType constructor.
     */
    public function __construct()
    {
        $this->attributes = new Collection();
        $this->setIconOptions();
    }

    /**
     * @return Collection
     */
    public function getAttributes(): Collection
    {
        return $this->attributes;
    }

    /**
     * @return string
     */
    public function getName(): string
    {
        return $this->name;
    }

    /**
     * @param string $name
     */
    public function setName(string $name): void
    {
        $this->name = $name;
    }

    /**
     * @return string
     */
    public function getIcon(): string
    {
        if ($this->icon == '') $this->icon = $this->name;

        $fullIconPath = $this->iconPath.$this->icon.'.svg';

        return $fullIconPath;
    }

    /**
     * @param string $icon
     */
    public function setIcon(string $icon): void
    {
        $this->icon = $icon;
    }

    /**
     * @return int
     */
    public function getId(): int
    {
        if (! $this->id) throw new \RuntimeException('The class "'.static::class.'" did not have an id. Set it first');

        return $this->id;
    }

    /**
     * @return array
     */
    public function toArray()
    {
        return [
            'id'   => $this->getId(),
            'name' => $this->getName(),
            'icon' => $this->getIcon(),
        ];
    }

    public function getIconOptions()
    {
        return $this->iconOptions->toArray();
    }

    protected function setIconOptions()
    {
        $iconOptions = collect();
        foreach (self::ICONS as $value => $label) {
            /** @var SelectOptionInterface $selectOption */
            $selectOption = (app(SelectOptionInterface::class))
                ->setContent($label)
                ->setHtmlContent($label)
                ->setValue($value);
            $iconOptions->push($selectOption);
        }

        $this->iconOptions = $iconOptions;
    }

    /**
     * @param string $key
     * @param $tab
     */
    protected function addButton(string $key = 'button', $tab = null)
    {

        $label = (new TextField($key.' Label'))
            ->mapValueFrom(Attribute::ValueFromComponent, Str::slug($key, '_').'_label');

        $link = (new TextField($key.' URL'))
            ->setExplanation(__('kms/attributes/components.url_explanation'))
            ->mapValueFrom(Attribute::ValueFromComponent, Str::slug($key, '_').'_link');

        if (isset($tab)) {
            $link->setDataAttribute('tab', $tab);
            $label->setDataAttribute('tab', $tab);
        }

        $this->attributes->push($label);
        $this->attributes->push($link);
    }

}