<?php

namespace Dao;
use Conn\MySqlConnect;

/**
 * Description of Ufs
 *
 * @author Délio
 */
class Ufs {
    
    public $id;
    public $name;
    public $uf;
    public $url;

    function __construct($res = null) {
        $this->id = (int) 0;
        $this->name = (string) '';
        $this->uf = (string) '';
        $this->url = (string) '';
        $this->fill($res);
    }
    
    private function fill($res) {
        if ($res != null && $res instanceof \stdClass) {
            $this->id = (int) (isset($res->id) ? $res->id : 0);
            $this->name = (string) (isset($res->name) ? $res->name : '');
            $this->uf = (string) (isset($res->uf) ? $res->uf : '');
            $this->url = (string) (isset($res->url) ? $res->url : null);
        }
    }
    
    function getLista(MySqlConnect $conecta, $where = '') {
        $lst_ufs = [];
        $sql = $conecta->query("SELECT * FROM ufs $where") or die($conecta->error);
        while ($res = $sql->fetch_object())
            $lst_ufs[] = new Ufs($res);
        return $lst_ufs;
    }
    
    function getUfPorId(MySqlConnect $conecta, $id) {
        $sql = $conecta->query("SELECT * FROM ufs WHERE id = '$id'") or die($conecta->error);
        if ($sql->num_rows > 0) {
            $res = $sql->fetch_object();
            $this->fill($res);
        }
    }
    
    function getUfPorUrl(MySqlConnect $conecta, $uf) {
        $sql = $conecta->query("SELECT id FROM ufs WHERE uf = '$uf'") or die($conecta->error);
        if ($sql->num_rows > 0) {
            $res = $sql->fetch_object();
            $this->getUfPorId($conecta, $res->id);
        }
    }
}