<?php

	class Html {

		public static function select($data, $options) {

            $select = '<select';

            if(isset($options['extras']) && !empty($options['extras'])) {
                foreach ($options['extras'] as $name => $value) {
                    $select .= ' ' . $name . '="' . $value . '"';
                }
            }

            $select .= '>';

            if(isset($options['default_text'])) {
                $select .= '<option value="">' . $options['default_text'] . '</option>';
            } else {
                $select .= '<option value="">Selecione</option>';
            }

            if(isset($options['before']) && !empty($options['before'])) {
                foreach ($options['before'] as $item) {
                    $select .= '<option value="' . $item[0] . '"';
                    if(isset($options['selected'])) {
                        if(is_array($options['selected'])) {
                            if(in_array($item[0], $options['selected'])) {
                                $select .= ' selected';
                            }
                        } else {
                            if($item[0] == $options['selected']) {
                                $select .= ' selected';
                            }
                        }
                    }
                    $select .= '>' . $item[1] . '</option>';
                }
            }

            foreach($data as $line) {
                $option  = '<option value="' . $line[$options['value']] . '"';

                if(isset($options['selected'])) {
                    if(is_array($options['selected'])) {
                        if(in_array($line[$options['value']], $options['selected'])) {
                            $option .= ' selected';
                        }
                    } else {
                        if($line[$options['value']] == $options['selected']) {
                            $option .= ' selected';
                        }
                    }
                }

                $option .= '>';

                if(strpos($options['content'], '{') !== false) {

                    preg_match_all('/{(.*?)}/', $options['content'], $matches);
                    $replace = $matches[0];
                    $fields  = $matches[1];

                    $string = $options['content'];

                    for($i = 0; $i < count($replace); $i++) {
                        if(isset($line[$fields[$i]])) {
                            $string = str_replace($replace[$i], $line[$fields[$i]], $string);
                        }
                    } 

                    $option .= $string;

                } else {
                    $option .= $line[$options['content']];
                }

                $option .= '</option>';
                $select .= $option;
            }

            if(isset($options['after']) && !empty($options['after'])) {
                foreach ($options['after'] as $item) {
                    $select .= '<option value="' . $item[0] . '"';
                    if(isset($options['selected'])) {
                        if(is_array($options['selected'])) {
                            if(in_array($item[0], $options['selected'])) {
                                $select .= ' selected';
                            }
                        } else {
                            if($item[0] == $options['selected']) {
                                $select .= ' selected';
                            }
                        }
                    }
                    $select .= '>' . $item[1] . '</option>';
                }
            }

            $select .= '</select>';

            echo $select;
        }

	}