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/slenders/slenders.nl/app/Komma/Kms/Core/Sections/AbstractAttributeKey.php
<?php
namespace App\Komma\Kms\Core\Sections;

/**
 * Class AbstractAttributeKey
 * @package App\Komma\Kms\Core\Sections
 *
 * Represents an attribute key that is used for tracking an attribute.
 * 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.
 */
abstract class AbstractAttributeKey
{
    protected const SHORTNAME_INDEX = 0;
    protected const VALUE_PART_INDEX = 1;
    protected const TRANSLATION_ISO2_INDEX = 2;


    /** @var string $valuePart A string that either refers to the value of an attribute via it's attribute with the same name OR the value itself if it is not linked to any model value*/
    protected $valuePart;

    /** @var string the class name without FQCN from the attribute this key is meant for*/
    protected $attributeShortClassName;

    /** @var string the iso 2 code of the translation of the attribute this key belongs to*/
    protected $translationIso2;

    /**
     * @var string The delimiter that seperates values
     */
    protected static $delimiter = '-';

    /**
     * AbstractAttributeKey constructor.
     */
    public function __construct()
    {
        $this->valuePart = '';
        $this->attributeShortClassName = '';
        $this->translationIso2 = '';
    }

    /**
     * Returns the Attribute key as you must use it in a form.
     */
    abstract public function __toString(): string;

    /**
     * Creates an instance from a string
     *
     * @param string $string
     * @return AbstractAttributeKey
     */
    abstract static public function createInstanceFromString(string $string): self;

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

    /**
     * @param string $valuePart
     * @return AbstractAttributeKey
     */
    public function setValuePart(string $valuePart): AbstractAttributeKey
    {
        if(strpos($valuePart, static::$delimiter) !== false) throw new \InvalidArgumentException("The valuePart must not contain an '".static::$delimiter."': ".$valuePart);
        $this->valuePart = $valuePart;
        return $this;
    }

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

    /**
     * @param string $attributeShortClassName
     * @return AbstractAttributeKey
     */
    public function setAttributeShortClassName(string $attributeShortClassName): AbstractAttributeKey
    {
        if(strpos($attributeShortClassName, static::$delimiter) !== false) throw new \InvalidArgumentException("The valuePart must not contain an '".static::$delimiter."': ".$attributeShortClassName);
        $this->attributeShortClassName = $attributeShortClassName;
        return $this;
    }

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

    /**
     * @param string $translationIso2
     * @return AbstractAttributeKey
     */
    public function setTranslationIso2(string $translationIso2): AbstractAttributeKey
    {
        if(strpos($translationIso2, static::$delimiter) !== false) throw new \InvalidArgumentException("The valuePart must not contain an '".static::$delimiter."': ".$translationIso2);
        $this->translationIso2 = $translationIso2;
        return $this;
    }

    /**
     * @param string $string
     * @return array
     */
    protected static function getParts(string $string): array {
        throw new \RuntimeException('AbstractAttributeKey::please override and implement the getParts method of AbstractAttributeKey in class "'.static::class.'"');
    }

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

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

    /**
     * @param string $string
     * @return string
     */
    public static function getTranslationISO2FromString(string $string):string{
        $parts = static::getParts($string);
        return (count($parts) < static::TRANSLATION_ISO2_INDEX) ? '' : $parts[static::TRANSLATION_ISO2_INDEX];
    }
}