Files
michaelschiemer/src/Framework/Design/ValueObjects/CustomProperty.php
Michael Schiemer 55a330b223 Enable Discovery debug logging for production troubleshooting
- Add DISCOVERY_LOG_LEVEL=debug
- Add DISCOVERY_SHOW_PROGRESS=true
- Temporary changes for debugging InitializerProcessor fixes on production
2025-08-11 20:13:26 +02:00

90 lines
2.4 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Framework\Design\ValueObjects;
/**
* Repräsentiert eine CSS Custom Property (CSS Variable)
*/
final readonly class CustomProperty
{
public function __construct(
public string $name,
public string $value
) {
}
public static function fromDeclaration(string $declaration): self
{
// Parse "--property-name: value" format
if (preg_match('/--([^:]+):\s*([^;]+)/', $declaration, $matches)) {
return new self(trim($matches[1]), trim($matches[2]));
}
throw new \InvalidArgumentException('Invalid CSS custom property declaration');
}
public function hasValueType(string $type): bool
{
return match($type) {
'color' => $this->isColorValue(),
'size' => $this->isSizeValue(),
'number' => $this->isNumberValue(),
default => false
};
}
public function getValueAs(string $type): mixed
{
return match($type) {
'color' => $this->getColorValue(),
'size' => $this->getSizeValue(),
'number' => $this->getNumberValue(),
default => $this->value
};
}
private function isColorValue(): bool
{
return preg_match('/^(#[0-9a-fA-F]{3,8}|rgb|hsl|oklch|color)/', $this->value) === 1;
}
private function isSizeValue(): bool
{
return preg_match('/^\d+(\.\d+)?(px|em|rem|%|vh|vw)$/', $this->value) === 1;
}
private function isNumberValue(): bool
{
return is_numeric($this->value);
}
private function getColorValue(): CssColor
{
if ($this->isColorValue()) {
$format = match(true) {
str_starts_with($this->value, '#') => ColorFormat::HEX,
str_starts_with($this->value, 'rgb') => ColorFormat::RGB,
str_starts_with($this->value, 'hsl') => ColorFormat::HSL,
str_starts_with($this->value, 'oklch') => ColorFormat::OKLCH,
default => ColorFormat::HEX
};
return new CssColor($this->value, $format);
}
throw new \InvalidArgumentException('Value is not a color');
}
private function getSizeValue(): string
{
return $this->value;
}
private function getNumberValue(): float
{
return (float) $this->value;
}
}