| 1 |
<?php |
| 2 |
|
| 3 |
namespace SimpleAnalytics\UI; |
| 4 |
|
| 5 |
use SimpleAnalytics\Support\Str; |
| 6 |
|
| 7 |
class LabelComponent |
| 8 |
{ |
| 9 |
/** |
| 10 |
* @readonly |
| 11 |
* @var string |
| 12 |
*/ |
| 13 |
private $value; |
| 14 |
/** |
| 15 |
* @readonly |
| 16 |
* @var string|null |
| 17 |
*/ |
| 18 |
private $docs; |
| 19 |
/** |
| 20 |
* @readonly |
| 21 |
* @var string|null |
| 22 |
*/ |
| 23 |
private $for; |
| 24 |
/** |
| 25 |
* @readonly |
| 26 |
* @var string |
| 27 |
*/ |
| 28 |
private $as = 'label'; |
| 29 |
public function __construct(string $value, ?string $docs, ?string $for = null, string $as = 'label') |
| 30 |
{ |
| 31 |
$this->value = $value; |
| 32 |
$this->docs = $docs; |
| 33 |
$this->for = $for; |
| 34 |
$this->as = $as; |
| 35 |
} |
| 36 |
public function __invoke(): void |
| 37 |
{ |
| 38 |
$attributes = [ |
| 39 |
'class' => 'block text-sm font-medium leading-6 text-gray-900', |
| 40 |
]; |
| 41 |
|
| 42 |
if ($this->as === 'label') { |
| 43 |
$attributes['for'] = esc_attr($this->for); |
| 44 |
} |
| 45 |
|
| 46 |
echo sprintf( |
| 47 |
'<%1$s %2$s>%3$s%4$s</%1$s>', |
| 48 |
$this->as, |
| 49 |
Str::htmlAttributes($attributes), |
| 50 |
esc_html($this->value), |
| 51 |
$this->docs ? $this->renderDocsLink() : '' |
| 52 |
); |
| 53 |
} |
| 54 |
|
| 55 |
protected function renderDocsLink(): string |
| 56 |
{ |
| 57 |
return sprintf( |
| 58 |
' <a href="%s" target="_blank" class="text-primary">(docs)</a>', |
| 59 |
esc_url($this->docs) |
| 60 |
); |
| 61 |
} |
| 62 |
} |
| 63 |
|