<?php
class Select {
  /**
   * Gera o código HTML do select com todos os atributos e opções fornecidos.
   * 
   * @param array $options Opções para formação do HTML.
   * @param array $attributes Atributos adicionais para o campo.
   * @return string O código HTML renderizado.
   */
  public static function render($options, $attributes) {
    $attribute_value = $attributes['value'] ?? null;
    $html = '<select';

    /* Attributes */
    foreach ($attributes as $key => $value)
      $html .= sprintf(' %s="%s"', Helper::sanitize_value($key), Helper::sanitize_value($value));
    $html .= '>';

    /* Options */
    foreach ($options as $key => $value) {
      if (Helper::is_associative_array($options)) {
        $option_value = strval($key);
        $option_label = $value;
      } else {
        $option_value = $option_label = $value;
      }

      $option_html = sprintf(
        '<option value="%s"%s>%s</option>',
        Helper::sanitize_value($option_value),
        selected($option_value === $attribute_value, true, false),
        Helper::sanitize_value($option_label)
      );

      $html .= $option_html;
    }

    $html .= '</select>';
    return $html;
  }
}
