Completed
Push — master ( 2a0a89...468a99 )
by Christian
02:41
created

WeatherHistory::__construct()   C

Complexity

Conditions 8
Paths 6

Size

Total Lines 23
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 72

Importance

Changes 4
Bugs 2 Features 1
Metric Value
c 4
b 2
f 1
dl 0
loc 23
ccs 0
cts 20
cp 0
rs 6.1404
cc 8
eloc 15
nc 6
nop 2
crap 72
1
<?php
2
/**
3
 * OpenWeatherMap-PHP-API — A php api to parse weather data from http://www.OpenWeatherMap.org .
4
 *
5
 * @license MIT
6
 *
7
 * Please see the LICENSE file distributed with this source code for further
8
 * information regarding copyright and licensing.
9
 *
10
 * Please visit the following links to read about the usage policies and the license of
11
 * OpenWeatherMap before using this class:
12
 *
13
 * @see http://www.OpenWeatherMap.org
14
 * @see http://www.OpenWeatherMap.org/terms
15
 * @see http://openweathermap.org/appid
16
 */
17
18
namespace Cmfcmf\OpenWeatherMap;
19
20
use Cmfcmf\OpenWeatherMap;
21
22
/**
23
 * Class WeatherHistory.
24
 */
25
class WeatherHistory implements \Iterator
26
{
27
    /**
28
     * The city object. IMPORTANT: Not all values will be set
29
     *
30
     * @var Util\City
31
     */
32
    public $city;
33
34
    /**
35
     * The time needed to calculate the request data.
36
     *
37
     * @var float
38
     */
39
    public $calctime;
40
41
    /**
42
     * An array of {@link WeatherHistory} objects.
43
     *
44
     * @var array
45
     *
46
     * @see WeatherForecast The WeatherForecast class.
47
     */
48
    private $histories;
49
50
    /**
51
     * @internal
52
     */
53
    private $position = 0;
54
55
    public function __construct($weatherHistory, $query)
56
    {
57
        if (isset($weatherHistory['list'][0]['city'])) {
58
            $country = $weatherHistory['list'][0]['city']['country'];
59
            $population = $weatherHistory['list'][0]['city']['population'];
60
        } else {
61
            $country = null;
62
            $population = null;
63
        }
64
65
        $this->city = new OpenWeatherMap\Util\City($weatherHistory['city_id'], (is_string($query)) ? $query : null, (isset($query['lon'])) ? $query['lon'] : null, (isset($query['lat'])) ? $query['lat'] : null, $country, $population);
66
        $this->calctime = $weatherHistory['calctime'];
67
68
        foreach ($weatherHistory['list'] as $history) {
69
            if (isset($history['rain'])) {
70
                $units = array_keys($history['rain']);
71
            } else {
72
                $units = array(0 => null);
73
            }
74
75
            $this->histories[] = new History($this->city, $history['weather'][0], array('now' => $history['main']['temp'], 'min' => $history['main']['temp_min'], 'max' => $history['main']['temp_max']), $history['main']['pressure'], $history['main']['humidity'], $history['clouds']['all'], isset($history['rain']) ? array('val' => $history['rain'][($units[0])], 'unit' => $units[0]) : null, $history['wind'], \DateTime::createFromFormat('U', $history['dt']));
76
        }
77
    }
78
79
    /**
80
     * @internal
81
     */
82
    public function rewind()
83
    {
84
        $this->position = 0;
85
    }
86
87
    /**
88
     * @internal
89
     */
90
    public function current()
91
    {
92
        return $this->histories[$this->position];
93
    }
94
95
    /**
96
     * @internal
97
     */
98
    public function key()
99
    {
100
        return $this->position;
101
    }
102
103
    /**
104
     * @internal
105
     */
106
    public function next()
107
    {
108
        ++$this->position;
109
    }
110
111
    /**
112
     * @internal
113
     */
114
    public function valid()
115
    {
116
        return isset($this->histories[$this->position]);
117
    }
118
}
119