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/bekkers/bekkersengineering.nl/app/KommaApp/Kms/Core/Sections/AttributeKey.php
<?php

namespace App\KommaApp\Kms\Core\Sections;

/**
 * Class AttributeKey
 * @package App\KommaApp\Kms\Core\Sections
 *
 * Represents an attribute key that is used for tracking an attribute.
 * You can for example have two "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. The key could correspond to a form field name if you would like that
 */
class AttributeKey extends AbstractAttributeKey
{
    /**
     * Creates an instance from a string
     *
     * @param string $string
     * @return AbstractAttributeKey
     */
    public static function createInstanceFromString(string $string): AbstractAttributeKey
    {
        $parts = explode(static::$delimiter, $string);
        if(count($parts) != 2 && count($parts) != 3) throw new \InvalidArgumentException("The string you pas must contain 2 or 3 '".static::$delimiter."' characters to respectively delimit the shortname, valuepart OR shortname, valuepart, languageIso2. Now contains ".count($parts)." '".static::$delimiter."' character(s)");

        $shortName = $parts[0];
        $valuePart = $parts[1];
        if(count($parts) == 3) $translationIso2 = $parts[2];

        $instance = new self();
        $instance->setAttributeShortClassName($shortName)->setValuePart($valuePart);
        if(isset($translationIso2)) $instance->translationIso2 = $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 AttributeKey string and create an instance of it.
     * @return bool
     */
    public static function couldBeAKeyString(string $string): bool
    {
        $parts = explode(static::$delimiter, $string);
        if(count($parts) == 2 || count($parts) == 3) 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
        ];
        if($this->translationIso2) $keyParts[] = $this->translationIso2;

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