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/mountadvies.komma.pro/app/Components/Component/ComponentAttributeKey.php
<?php


namespace App\Components\Component;


use App\Helpers\KommaHelpers;
use App\Attributes\ComponentArea;
use Komma\KMS\Core\Sections\AbstractAttributeKey;
use Komma\KMS\Core\Sections\AttributeKey;
use Illuminate\Support\Facades\Log;

/**
 * Class AbstractAttributeKey
 * @package Komma\KMS\Core\Sections
 *
 * Represents an attribute key that is used for tracking an attribute inside of a component.
 * You can for example have to "title" attributes that look exactly the same
 * but internally they have a different Attribute key which enables you to select the one that
 * you want to manipulate.
 */
class ComponentAttributeKey extends AbstractAttributeKey
{
    protected const SHORTNAME_INDEX = 0;
    protected const VALUE_PART_INDEX = 1;
    protected const COMPONENT_ID_INDEX = 2;
    protected const ATTRIBUTE_REFERENCE_INDEX = 3;
    protected const TRANSLATION_ISO2_INDEX = 4;

    protected static $delimiter = '|';

    /** @var int $componentId */
    private $componentId;

    /** @var int $attributeReference */
    private $attributeReference;

    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Creates an instance from a string
     *
     * @param string $string
     * @return ComponentAttributeKey
     */
    public static function createInstanceFromString(string $string): AbstractAttributeKey
    {
        $parts = self::getParts($string);
        $shortName = $parts[self::SHORTNAME_INDEX];
        $valuePart = $parts[self::VALUE_PART_INDEX];
        $componentId = preg_replace('/[cC]/','', $parts[self::COMPONENT_ID_INDEX]); //Remove C prefix if needed
        $attributeReference = $parts[self::ATTRIBUTE_REFERENCE_INDEX];

        if (count($parts) == 5) $translationIso2 = $parts[self::TRANSLATION_ISO2_INDEX];

//        dd('shortName: '. $shortName. ' valuePart: '. $valuePart.' componentId: '.$componentId.' attributeReference: '.$attributeReference);

        $instance = new self();
        $instance
            ->setComponentId($componentId)
            ->setAttributeReference($attributeReference)
            ->setAttributeShortClassName($shortName)
            ->setValuePart($valuePart);
        if(isset($translationIso2)) $instance->setTranslationIso2($translationIso2);

        return $instance;
    }


    /**
     * @param string $string
     * @return string
     */
    public static function getComponentIdFromString(string $string):int{
        $parts = self::getParts($string);
        return (int) substr($parts[self::COMPONENT_ID_INDEX], 1);
    }

    /**
     * @param string $string
     * @return string
     */
    public static function getAttributeReferenceFromString(string $string):string{
        $parts = self::getParts($string);
        return $parts[self::ATTRIBUTE_REFERENCE_INDEX];
    }

    /**
     * Get the parts of the key and validate them
     *
     * @param string $string
     * @return array
     */
    protected static function getParts(string $string): array
    {
        /** @var string[] $parts */
        $parts = explode(static::$delimiter, $string);
        if(count($parts) < 4 || count($parts) > 5) throw new \InvalidArgumentException("The string you pas must contain 3 or 5 '".static::$delimiter."' characters to respectively delimit the shortname, valuepart, componentId (optionally prefixed with -), attributeReference, OR shortname, valuepart, (optionally prefixed with -), attributeReference, languageIso2. Now contains ".count($parts)." '".static::$delimiter."' character(s)");
        return $parts;
    }

    /**
     * @param string $shortClassName
     * @param string $valuePart
     * @param string $translationIso2
     * @param int $componentId
     * @param string $componentAttributeReference
     * @return ComponentAttributeKey
     */
    public static function createInstance(int $componentId, string $componentAttributeReference, string $shortClassName, string $valuePart, string $translationIso2 = '')
    {
        $instance = new self();
        $instance->setComponentId($componentId)
            ->setAttributeReference($componentAttributeReference)
            ->setAttributeShortClassName($shortClassName)
            ->setValuePart($valuePart);

        if($translationIso2 !== '') $instance->setTranslationIso2($translationIso2);

        return $instance;
    }

    /**
     * @param string $string Checks if the given string could be a key string.
     * If it returns true then you can use it as a ComponentAttributeKey string and create an instance of it.
     * @return bool
     */
    public static function couldBeAKeyString(string $string): bool
    {
        $parts = explode(static::$delimiter, $string);
        if(count($parts) >= 4 && count($parts) <=  5) return true;
        return false;
    }

    /**
     * Returns the Attribute key as you must use it in a form.
     */
    public function __toString(): string
    {
        $keyParts = [
            $this->attributeShortClassName,
            $this->valuePart,
            'C'.$this->componentId,
            $this->attributeReference,
        ];
        if($this->translationIso2) $keyParts[] = $this->translationIso2;

        $key = implode(static::$delimiter, $keyParts);
        return $key;
    }

    /**
     * Returns a regex to help select inputs using an dynamic groups value reference
     *
     * @param string $componentKey
     * @param string|null $languageIso2
     * @return string
     */
    public static function getRegexForAttributeKeysInsideComponentWithKey(string $componentKey, string $languageIso2 = null)
    {
        if(!AttributeKey::couldBeAKeyString($componentKey)) abort(400, 'The specified groups key "'.$componentKey.'" does not represent an "'.AttributeKey::class.'"');
        $keyInstance = AttributeKey::createInstanceFromString($componentKey);
        $regex = '^'.KommaHelpers::getShortNameFromClass(ComponentArea::class).self::$delimiter.$keyInstance->getValuePart().self::$delimiter.'C(?:-)?\d+'.self::$delimiter.'.+';
        if($languageIso2) $regex .= self::$delimiter.$languageIso2;

        return $regex;
    }

    /**
     * @return mixed
     */
    public function getComponentId()
    {
        return $this->componentId;
    }

    /**
     * @param mixed $componentId
     * @return ComponentAttributeKey
     */
    public function setComponentId($componentId)
    {
        $componentId = str_ireplace('c', '', $componentId);
        if(!is_numeric($componentId)) throw new \InvalidArgumentException('The component id without C prefix must be an integer. Actual: '.$componentId);
        $this->componentId = (int) $componentId;
        return $this;
    }

    /**
     * @return int
     */
    public function getAttributeReference(): string
    {
        return $this->attributeReference;
    }

    /**
     * @param $attributeReference
     * @return ComponentAttributeKey
     */
    public function setAttributeReference(string $attributeReference): ComponentAttributeKey
    {
        $this->attributeReference = $attributeReference;
        return $this;
    }
}