60 lines
1.7 KiB
PHP
60 lines
1.7 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace ElixForms\Common;
|
|
|
|
/**
|
|
* Wrapper per layout label + input in un layout a due colonne Bootstrap.
|
|
* Equivalente PHP di ElixFormsElement.tsx nel progetto React.
|
|
*
|
|
* Genera markup compatibile con il design system Bootstrap Italia:
|
|
* - Colonna sinistra (col-12 col-md-4): label
|
|
* - Colonna destra (col-12 col-md-8): input
|
|
*/
|
|
class ElixFormsElement
|
|
{
|
|
private string $labelHtml;
|
|
private string $inputHtml;
|
|
private string $separator;
|
|
|
|
/**
|
|
* @param string $labelHtml HTML della label
|
|
* @param string $inputHtml HTML dell'input
|
|
* @param string $separator Separatore opzionale tra label e input
|
|
*/
|
|
public function __construct(string $labelHtml, string $inputHtml, string $separator = '')
|
|
{
|
|
if (empty($labelHtml) || empty($inputHtml)) {
|
|
throw new \InvalidArgumentException('labelHtml and inputHtml are required.');
|
|
}
|
|
|
|
$this->labelHtml = $labelHtml;
|
|
$this->inputHtml = $inputHtml;
|
|
$this->separator = $separator;
|
|
}
|
|
|
|
/**
|
|
* Genera l'HTML del campo form con layout a due colonne.
|
|
*
|
|
* @return string HTML renderizzato
|
|
*/
|
|
public function render(): string
|
|
{
|
|
$sep = $this->separator !== '' ? htmlspecialchars($this->separator) : '';
|
|
|
|
return <<<HTML
|
|
<div class="row mb-3 align-items-start">
|
|
<div class="col-12 col-md-4">
|
|
<div class="text-md-end pe-md-3 mt-2">
|
|
{$this->labelHtml}{$sep}
|
|
</div>
|
|
</div>
|
|
<div class="col-12 col-md-8">
|
|
{$this->inputHtml}
|
|
</div>
|
|
</div>
|
|
HTML;
|
|
}
|
|
}
|