mirror of
https://code.castopod.org/adaures/castopod.git
synced 2024-09-04 01:47:19 +02:00
55 lines
1.2 KiB
PHP
55 lines
1.2 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace ViewComponents;
|
|
|
|
class Component implements ComponentInterface
|
|
{
|
|
protected string $slot = '';
|
|
|
|
protected string $class = '';
|
|
|
|
/**
|
|
* @var array<string, string>
|
|
*/
|
|
protected array $attributes = [
|
|
'class' => '',
|
|
];
|
|
|
|
/**
|
|
* @param array<string, string> $attributes
|
|
*/
|
|
public function __construct(array $attributes)
|
|
{
|
|
helper('viewcomponents');
|
|
|
|
// overwrite default attributes if set
|
|
$this->attributes = array_merge($this->attributes, $attributes);
|
|
|
|
if ($attributes !== []) {
|
|
$this->hydrate($attributes);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @param array<string, string> $attributes
|
|
*/
|
|
public function hydrate(array $attributes): void
|
|
{
|
|
foreach ($attributes as $name => $value) {
|
|
$method = 'set' . str_replace(' ', '', ucwords(str_replace('_', ' ', $name)));
|
|
if (is_callable([$this, $method])) {
|
|
$this->{$method}($value);
|
|
} else {
|
|
$this->{$name} = $value;
|
|
}
|
|
}
|
|
}
|
|
|
|
public function render(): string
|
|
{
|
|
return static::class . ': RENDER METHOD NOT IMPLEMENTED';
|
|
}
|
|
}
|