File: D:/HostingSpaces/SBogers10/rentman2019.komma.pro/app/Komma/Kms/Core/Attributes/ComponentArea.php
<?php
namespace App\Komma\Kms\Core\Attributes;
use App\Komma\Dynamic\Component\ComponentAttributeKey;
use App\Komma\Dynamic\ComponentArea\ComponentAreaInterface;
use App\Komma\Dynamic\ComponentType\ComponentTypeFactory;
use App\Komma\Dynamic\ComponentType\ComponentTypes;
use App\Komma\Dynamic\ComponentType\Types\AbstractComponentType;
use App\Komma\Kms\Core\Attributes\Models\Traits\SubFolderTrait;
use FontLib\TrueType\Collection;
use Illuminate\Support\MessageBag;
use Illuminate\View\View;
/**
* Class DynamicGroupTypes
*/
class ComponentArea extends Attribute implements ComponentAreaInterface
{
use SubFolderTrait;
/** @var string[] */
private $componentTypes = [];
/** @var Collection */
protected $components;
public $showBackgroundOption = true;
/**
* DynamicGroupTypes constructor.
*/
public function __construct()
{
$this->subFolder = '';
$this->components = collect();
$this->componentTypes = [];
parent::__construct();
}
/**
* Returns a view that visually represents this attribute
*/
public function render(): View
{
if ($this->hasAssociatedLanguage()) {
$componentAttributeFieldsRegex = ComponentAttributeKey::getRegexForAttributeKeysInsideComponentWithKey((string) $this->getKey(), $this->getAssociatedLanguage()->iso_2);
} else {
$componentAttributeFieldsRegex = ComponentAttributeKey::getRegexForAttributeKeysInsideComponentWithKey((string) $this->getKey());
}
$data = [
'attribute' => $this,
'components' => $this->components ?: collect(),
'componentAttributeFieldsRegex' => $componentAttributeFieldsRegex,
'errors' => new MessageBag(), //Needed for unit testing
];
return view('kms.attributes.componentArea', $data);
}
/**
* @return string The value associated with the attribute
*/
public function getValue(): string
{
//Convert internal value to json for storing in the database only.
return $this->value;
}
/**
* @param string $value The value associated with the attribute
* @return $this
*/
public function setValue(string $value)
{
//VALUE CAN BE JSON DATA FROM DATABASE. OR FROM INPUT. IF IT IS FROM INPUT, THEN IT COMES FROM A DYNAMICALLY UPDATED HIDDEN INPUT
$this->value = $value;
return $this;
}
/**
* Returns an array with class names for component types or if you
* specify the argument as true, instances of those componentTypes
*
* @param bool $asInstances
* @return string[]|AbstractComponentType[]
*/
public function getComponentTypes($asInstances = false): array
{
if (! $asInstances) {
return $this->componentTypes;
}
return array_map(function (int $abstractComponentTypeId) {
return ComponentTypeFactory::make($abstractComponentTypeId);
}, $this->componentTypes);
}
/**
* Set the allowed component types for the component area.
*
* @see ComponentTypes
* @param string[] $componentTypes
* @return ComponentArea
*/
public function setComponentTypes(array $componentTypes): self
{
$this->componentTypes = array_map(function ($item) { //Only allow int values
if (! is_int($item) || ! in_array($item, ComponentTypes::getAsArray(), true)) {
throw new \InvalidArgumentException('The componentTypes array must only contain ComponentTypes enum int\'s of the available componentTypes.');
}
return $item;
}, $componentTypes);
return $this;
}
/**
* @return bool
*/
public function showBackgroundOption(): bool
{
return $this->showBackgroundOption;
}
/**
* @return ComponentArea
*/
public function disableBackgroundOption(): self
{
$this->showBackgroundOption = false;
return $this;
}
}