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/SBogers10/ste.komma.pro/app/Attributes/ComponentArea.php
<?php


namespace App\Attributes;


use App\Components\Component\ComponentAttributeKey;
use App\Components\ComponentArea\ComponentAreaInterface;
use App\Components\ComponentType\ComponentTypeFactory;
use App\Components\ComponentType\ComponentTypes;
use App\Components\ComponentType\Types\AbstractComponentType;
use Komma\KMS\Core\Attributes\Models\Traits\SubFolderTrait;
use FontLib\TrueType\Collection;
use Illuminate\Support\MessageBag;
use Illuminate\View\View;

/**
 * Class DynamicGroupTypes
 *
 *
 * @package App\Kms\Core\Attributes
 */
class ComponentArea extends Attribute implements ComponentAreaInterface
{
    use SubFolderTrait;
    /** @var string[] */
    private $componentTypes = [];

    /** @var Collection $components */
    protected $components;

    /** @var bool  */
    protected $showBackgroundOption = true;

    /** @var array $selfClassInstances */
    private static $selfClassInstances = [];

    /**
     * DynamicGroupTypes constructor.
     */
    public function __construct()
    {
        $this->subFolder = '';
        $this->components = collect();
        $this->componentTypes = [];
        parent::__construct();
        self::$selfClassInstances[] = $this;
    }

    /**
     * Returns a view that visually represents this attribute
     */
    public function render(): View
    {
        $data = [
            'attribute' => $this,
            'components' => $this->components ?: collect(),

            'componentAttributeFieldsRegex' => ComponentAttributeKey::getRegexForAttributeKeysInsideComponentWithKey((string) $this->getKey(), $this->getAssociatedLanguage()->iso_2),
            '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 == '' ? '[]' : $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): ComponentArea
    {
        $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(): ComponentArea
    {
        $this->showBackgroundOption = false;
        return $this;
    }

    public function __clone()
    {
        parent::__clone();
        self::$selfClassInstances[] = $this;
    }

    /**
     * @return array
     */
    public static function getSelfInstances(): array
    {
        return self::$selfClassInstances;
    }

    /**
     * Return an array of component areas with the same value part but different language as the current component area.
     */
    public function getCopyableInstances()
    {
        $copyableInstances = [];
        foreach (self::getSelfInstances() as $selfInstance) {
            /** @var ComponentArea $selfInstance */
            if($selfInstance->getKey() === $this->getKey()) continue;
            if($selfInstance->getKey()->getValuePart() != $this->getKey()->getValuePart()) continue;
            if($selfInstance->getKey()->getTranslationIso2() == '') continue; // Necessary to prevent selecting the original component area without language suffix
            $copyableInstances[] = $selfInstance;
        }

        return $copyableInstances;
    }
}