File: D:/HostingSpaces/stafa/stafa.nl/app/Komma/Components/ComponentType/Types/AbstractComponentType.php
<?php
namespace App\Komma\Components\ComponentType\Types;
use App\Komma\Kms\Core\Attributes\Attribute;
use App\Komma\Kms\Core\Attributes\Models\SelectOptionInterface;
use App\Komma\Kms\Core\Attributes\TextField;
use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Support\Collection;
/**
* Class ComponentType
*
* @package App\Komma\Components\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('Note: http or https will open a new tab')
->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);
}
}