File: D:/HostingSpaces/SBogers10/douven.komma.pro/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;
}
}