['html'], 'needs_context' => true ]) ]; } /** * Génère le code HTML d'un champs * @param array $context Contexte de la vue Twig * @param string $key Clef du champs * @param mixed $value Valeur du champs * @param string|null $label Label à utiliser * @param array $options * @param array $attributes * @return string */ public function field( array $context, string $key, $value, ?string $label = null, array $options = [], array $attributes = [] ): string { $type = $options['type'] ?? 'text'; $error = $this->getErrorHtml($context, $key); $class = 'form-group'; $value = $this->convertValue($value); $attributes = array_merge([ 'class' => trim('form-control ' . ($options['class'] ?? '')), 'name' => $key, 'id' => $key ], $attributes); if ($error) { $class .= ' has-danger'; $attributes['class'] .= ' form-control-danger'; } if ($type === 'textarea') { $input = $this->textarea($value, $attributes); } elseif ($type === 'file') { $input = $this->file($attributes); } elseif ($type === 'checkbox') { $input = $this->checkbox($value, $attributes); } elseif (array_key_exists('options', $options)) { $input = $this->select($value, $options['options'], $attributes); } else { $attributes['type'] = $options['type'] ?? 'text'; $input = $this->input($value, $attributes); } return "
{$input} {$error}
"; } private function convertValue($value): string { if ($value instanceof \DateTime) { return $value->format('Y-m-d H:i:s'); } return (string)$value; } /** * Génère l'HTML en fonction des erreurs du contexte * @param $context * @param $key * @return string */ private function getErrorHtml($context, $key) { $error = $context['errors'][$key] ?? false; if ($error) { return "{$error}"; } return ""; } /** * Génère un * @param null|string $value * @param array $attributes * @return string */ private function input(?string $value, array $attributes): string { return "getHtmlFromArray($attributes) . " value=\"{$value}\">"; } /** * Génère un * @param null|string $value * @param array $attributes * @return string */ private function checkbox(?string $value, array $attributes): string { $html = ''; if ($value) { $attributes['checked'] = true; } return $html . "getHtmlFromArray($attributes) . " value=\"1\">"; } private function file($attributes) { return "getHtmlFromArray($attributes) . ">"; } /** * Génère un "; } /** * Génère un getHtmlFromArray($attributes) . ">$htmlOptions"; } /** * Transforme un tableau $clef => $valeur en attribut HTML * @param array $attributes * @return string */ private function getHtmlFromArray(array $attributes) { $htmlParts = []; foreach ($attributes as $key => $value) { if ($value === true) { $htmlParts[] = (string)$key; } elseif ($value !== false) { $htmlParts[] = "$key=\"$value\""; } } return implode(' ', $htmlParts); } }