<commit_before><?php

class Weatherco
{
    public function updateFullCurrent(){
        
        $xml_resp = simplexml_load_file('http://xml.weather.co.ua/1.2/fullcurrent/');

        if (!$xml_resp){
            throw new Exception("Error loading fullcurrent weather");
        }

        foreach ($xml_resp->current as $current){

            $item = array();

            $item['city_id'] = intval($current->attributes()->city);

            foreach ($current as $field => $val){
                $item[$field] = strval($val);

                if ($field == 'date'){
                    $item['hour'] = date("G", strtotime(strval($val)));
                }
            }

            $item = $this->parse($item);

            $this->setCurrentCache($item);
        }
    }

    public function updateFullForecast(){

        $xml_resp = simplexml_load_file('http://xml.weather.co.ua/1.2/fullforecast/');

        if (!$xml_resp){
            throw new Exception("Error loading fullforecast weather");
        }

        foreach ($xml_resp->forecast as $forecast){

            $weather = array();

            $weather['city_id'] = intval($forecast->attributes()->city);

            $weather['forecast'] = array();

            foreach ($xml_resp->forecast->day as $day){

                $item = array();

                $item['date'] = strval($day->attributes()->date);
                $item['hour'] = intval($day->attributes()->hour);
                $item['timestamp'] = strtotime($item['date'].' '.$item['hour'].':00:00');

                foreach ($day as $field => $val){

                    if ($val->count() > 0){ // work if php>=5.3.0
                        $child = array();
                        foreach ($val as $child_name => $child_val){
                            $child[$child_name] = strval($child_val);
                        }
                        $item[$field] = $child;
                    }else{
                        $item[$field] = strval($val);
                    }
                }

                $weather['forecast'][] = $this->parse($item);

                $this->setForecastCache($weather);
            }
        }
    }

    private function setCurrentCache($current){

        return $this->setCache($current);
    }

    private function setForecastCache($forecast){

        return $this->setCache($forecast, false);
    }

    private function setCache($weather, $current = true){

        if (empty($weather['city_id'])){
            return false;
        }

        $city_id = intval($weather['city_id']);

        $weather['updated'] = 'NOW()';

        $cache = Mysql::getInstance()->from('weatherco_cache')->where(array('city_id' => $city_id))->get()->first();

        $field = empty($current) ? 'forecast' : 'current';

        if (empty($cache)){
            return Mysql::getInstance()->insert('weatherco_cache', array('city_id' => $city_id, $field => System::base64_encode(serialize($weather))));
        }else{
            return Mysql::getInstance()->update('weatherco_cache', array($field => System::base64_encode(serialize($weather))), array('city_id' => $city_id));
        }
    }

    public function getCurrent(){

        $city_id = Stb::getInstance()->city_id;

        $cache = Mysql::getInstance()->from('weatherco_cache')->where(array('city_id' => $city_id))->get()->first();

        $current = unserialize(System::base64_decode($cache['current']));

        if (!empty($current) && is_array($current)){

            $current['city'] = Mysql::getInstance()->from('cities')->where(array('id' => $current['city_id']))->get()->first(_('city_name_field'));<commit_msg>Fix fullforecast bug<commit_after><?php

class Weatherco
{
    public function updateFullCurrent(){
        
        $xml_resp = simplexml_load_file('http://xml.weather.co.ua/1.2/fullcurrent/');

        if (!$xml_resp){
            throw new Exception("Error loading fullcurrent weather");
        }

        foreach ($xml_resp->current as $current){

            $item = array();

            $item['city_id'] = intval($current->attributes()->city);

            foreach ($current as $field => $val){
                $item[$field] = strval($val);

                if ($field == 'date'){
                    $item['hour'] = date("G", strtotime(strval($val)));
                }
            }

            $item = $this->parse($item);

            $this->setCurrentCache($item);
        }
    }

    public function updateFullForecast(){

        $xml_resp = simplexml_load_file('http://xml.weather.co.ua/1.2/fullforecast/');

        if (!$xml_resp){
            throw new Exception("Error loading fullforecast weather");
        }

        foreach ($xml_resp->forecast as $forecast){

            $weather = array();

            $weather['city_id'] = intval($forecast->attributes()->city);

            $weather['forecast'] = array();

            foreach ($forecast->day as $day){

                $item = array();

                $item['date'] = strval($day->attributes()->date);
                $item['hour'] = intval($day->attributes()->hour);
                $item['timestamp'] = strtotime($item['date'].' '.$item['hour'].':00:00');

                foreach ($day as $field => $val){

                    if ($val->count() > 0){ // work if php>=5.3.0
                        $child = array();
                        foreach ($val as $child_name => $child_val){
                            $child[$child_name] = strval($child_val);
                        }
                        $item[$field] = $child;
                    }else{
                        $item[$field] = strval($val);
                    }
                }

                $weather['forecast'][] = $this->parse($item);

                $this->setForecastCache($weather);
            }
        }
    }

    private function setCurrentCache($current){

        return $this->setCache($current);
    }

    private function setForecastCache($forecast){

        return $this->setCache($forecast, false);
    }

    private function setCache($weather, $current = true){

        if (empty($weather['city_id'])){
            return false;
        }

        $city_id = intval($weather['city_id']);

        $weather['updated'] = 'NOW()';

        $cache = Mysql::getInstance()->from('weatherco_cache')->where(array('city_id' => $city_id))->get()->first();

        $field = empty($current) ? 'forecast' : 'current';

        if (empty($cache)){
            return Mysql::getInstance()->insert('weatherco_cache', array('city_id' => $city_id, 'updated' => 'NOW()', $field => System::base64_encode(serialize($weather))));
        }else{
            return Mysql::getInstance()->update('weatherco_cache', array($field => System::base64_encode(serialize($weather)), 'updated' => 'NOW()'), array('city_id' => $city_id));
        }
    }

    public function getCurrent(){

        $city_id = Stb::getInstance()->city_id;

        $cache = Mysql::getInstance()->from('weatherco_cache')->where(array('city_id' => $city_id))->get()->first();

        $current = unserialize(System::base64_decode($cache['current']));

        if (!empty($current) && is_array($current)){

            $current['city'] = Mysql::getInstance()->from('cities')->where(array('id' => $current['city_id']))->get()->first(_('city_name_field'));
{'fraction_non_alphanumeric': 0.1809250920998772, 'fraction_numerical': 0.004911993450675399, 'mean_word_length': 2.077246011754828, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 96, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 21}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '100431', 'n_tokens_mistral': 2154, 'n_tokens_neox': 1975, 'n_words': 337}