Completed
Push — docs ( 298ede )
by Christian
01:38
created

Forecast::__construct()   B

Complexity

Conditions 5
Paths 8

Size

Total Lines 46

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 46
rs 8.867
c 0
b 0
f 0
cc 5
nc 8
nop 2
1
<?php
2
3
/*
4
 * OpenWeatherMap-PHP-API — A PHP API to parse weather data from https://OpenWeatherMap.org.
5
 *
6
 * @license MIT
7
 *
8
 * Please see the LICENSE file distributed with this source code for further
9
 * information regarding copyright and licensing.
10
 *
11
 * Please visit the following links to read about the usage policies and the license of
12
 * OpenWeatherMap data before using this library:
13
 *
14
 * @see https://OpenWeatherMap.org/price
15
 * @see https://OpenWeatherMap.org/terms
16
 * @see https://OpenWeatherMap.org/appid
17
 */
18
19
namespace Cmfcmf\OpenWeatherMap;
20
21
use Cmfcmf\OpenWeatherMap\Util\Temperature;
22
use Cmfcmf\OpenWeatherMap\Util\Time;
23
use Cmfcmf\OpenWeatherMap\Util\Unit;
24
use Cmfcmf\OpenWeatherMap\Util\Weather;
25
use Cmfcmf\OpenWeatherMap\Util\Wind;
26
27
/**
28
 * Class Forecast.
29
 */
30
class Forecast extends CurrentWeather
31
{
32
    /**
33
     * @var Time The time of the forecast.
34
     */
35
    public $time;
36
37
    /**
38
     * Create a new weather object for forecasts.
39
     *
40
     * @param \SimpleXMLElement $xml   The forecasts xml.
41
     * @param string            $units Ths units used.
42
     *
43
     * @internal
44
     */
45
    public function __construct(\SimpleXMLElement $xml, $units)
46
    {
47
        $utctz = new \DateTimeZone('UTC');
48
49
        if ($units == 'metric') {
50
            $temperatureUnit = "°C";
51
        } else {
52
            $temperatureUnit = '°F';
53
        }
54
55
        $xml->temperature['value'] = round((floatval($xml->temperature['max']) + floatval($xml->temperature['min'])) / 2, 2);
56
57
        $this->temperature = new Temperature(
58
            new Unit($xml->temperature['value'], $temperatureUnit),
59
            new Unit($xml->temperature['min'], $temperatureUnit),
60
            new Unit($xml->temperature['max'], $temperatureUnit),
61
            new Unit($xml->temperature['day'], $temperatureUnit),
62
            new Unit($xml->temperature['morn'], $temperatureUnit),
63
            new Unit($xml->temperature['eve'], $temperatureUnit),
64
            new Unit($xml->temperature['night'], $temperatureUnit)
65
        );
66
        $this->humidity = new Unit($xml->humidity['value'], $xml->humidity['unit']);
67
        $this->pressure = new Unit($xml->pressure['value'], $xml->pressure['unit']);
68
69
        // This is kind of a hack, because the units are missing in the xml document.
70
        if ($units == 'metric') {
71
            $windSpeedUnit = 'm/s';
72
        } else {
73
            $windSpeedUnit = 'mph';
74
        }
75
76
        $this->wind = new Wind(
77
            new Unit($xml->windSpeed['mps'], $windSpeedUnit, $xml->windSpeed['name']),
78
            property_exists($xml, 'windDirection') ? new Unit($xml->windDirection['deg'], $xml->windDirection['code'], $xml->windDirection['name']) : null
79
        );
80
        $this->clouds = new Unit($xml->clouds['all'], $xml->clouds['unit'], $xml->clouds['value']);
81
        $this->precipitation = new Unit($xml->precipitation['value'], null, $xml->precipitation['type']);
82
        $this->weather = new Weather($xml->symbol['number'], $xml->symbol['name'], $xml->symbol['var']);
83
        $this->lastUpdate = new \DateTime($xml->lastupdate['value'], $utctz);
84
85
        if (isset($xml['from'])) {
86
            $this->time = new Time($xml['from'], $xml['to']);
87
        } else {
88
            $this->time = new Time($xml['day']);
89
        }
90
    }
91
}
92