<?php
final class Generator {
  private static $pattern = '/\$\{(\w+)\}/';
  private static $row = array();
  public static function getOptionsByResource($rows, $valueName, $textName, $selected = NULL) {
    $options = '';
    while ($row = mysql_fetch_assoc($rows)) {
      $options .= self::getOption($row[$valueName], $row[$textName], $selected);
    }
    return $options;
  }
  public static function getOptionsByArray($rows, $selected = NULL) {
    $options = '';
    foreach ($rows as $key => $val) {
      $options .= self::getOption($key, $val, $selected);
    }
    return $options;
  }
  public static function replaceTpl(array $entry, $tplName) {
    self::$row = $entry;
    return preg_replace_callback(self::$pattern, array('self', 'createCallback'), file_get_contents(TPL . $tplName));

  }
  private static function getOption($value, $text, $selected = NULL) {
    return '<option value="' . $value . '" ' . ($value == $selected ? 'selected="selected"' : '') . '>' . $text . '</option>';
  }
  private static function createCallback($key) {
    return isset(self::$row[$key[1]]) ? self::$row[$key[1]] : '';
  }
}
// EOF.
