| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788 |
- <?php
- /**
- * Classe html
- *
- * Cette classe fournit des méthodes pour générer et afficher des éléments HTML,
- * tels que les éléments <select> et <input>.
- */
- class html {
- /**
- * Construit le HTML d'un élément <select>.
- *
- * @param string $_config Attributs HTML du <select> sous forme de chaîne.
- * @param array $_options Tableau associatif des options du <select> (clé = valeur de l'option, valeur = label affiché).
- * @param mixed $_value Valeur sélectionnée par défaut (facultatif).
- * @return string Le HTML généré pour l'élément <select>.
- */
- private static function constructSelect(string $_config, array $_options, $_value = null): string {
- $selectHtml = '<select ' . $_config . '>';
- foreach ($_options as $optionValue => $label) {
- $selected = ($_value !== null && $_value == $optionValue) ? ' selected' : '';
- $selectHtml .= sprintf('<option value="%s"%s>%s</option>', htmlspecialchars($optionValue), $selected, htmlspecialchars($label));
- }
- $selectHtml .= '</select>';
- return $selectHtml;
- }
- /**
- * Renvoie le HTML d'un élément <select> sous forme de chaîne.
- *
- * @param string $_config Attributs HTML du <select> sous forme de chaîne.
- * @param array $_options Tableau associatif des options du <select> (clé = valeur de l'option, valeur = label affiché).
- * @param mixed $_value Valeur sélectionnée par défaut (facultatif).
- * @return string Le HTML généré pour l'élément <select>.
- */
- public static function getSelect(string $_config, array $_options, $_value = null): string {
- return self::constructSelect($_config, $_options, $_value);
- }
- /**
- * Affiche directement le HTML d'un élément <select>.
- *
- * @param string $_config Attributs HTML du <select> sous forme de chaîne.
- * @param array $_options Tableau associatif des options du <select> (clé = valeur de l'option, valeur = label affiché).
- * @param mixed $_value Valeur sélectionnée par défaut (facultatif).
- * @return void
- */
- public static function printSelect(string $_config, array $_options, $_value = null): void {
- echo self::constructSelect($_config, $_options, $_value);
- }
- /**
- * Construit le HTML d'un élément <input> de type texte.
- *
- * @param string $_config Attributs HTML de l'élément <input> sous forme de chaîne.
- * @param mixed $_value Valeur par défaut de l'élément <input> (facultatif).
- * @return string Le HTML généré pour l'élément <input>.
- */
- private static function constructInput(string $_config, $_value = null): string {
- $value = $_value !== null ? htmlspecialchars((string) $_value) : '';
- $inputHtml = sprintf('<input value="%s" %s />', htmlspecialchars($value), $_config);
- return $inputHtml;
- }
- /**
- * Renvoie le HTML d'un élément <input> de type texte sous forme de chaîne.
- *
- * @param string $_config Attributs HTML de l'élément <input> sous forme de chaîne.
- * @param mixed $_value Valeur par défaut de l'élément <input> (facultatif).
- * @return string Le HTML généré pour l'élément <input>.
- */
- public static function getInput(string $_config, $_value = null): string {
- return self::constructInput($_config, $_value);
- }
- /**
- * Affiche directement le HTML d'un élément <input> de type texte.
- *
- * @param string $_config Attributs HTML de l'élément <input> sous forme de chaîne.
- * @param mixed $_value Valeur par défaut de l'élément <input> (facultatif).
- * @return void
- */
- public static function printInput(string $_config, $_value = null): void {
- echo self::constructInput($_config, $_value);
- }
- }
|