Completed
Pull Request — master (#140)
by Christian
01:49 queued 11s
created

City   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 7
lcom 0
cbo 1
dl 0
loc 67
ccs 0
cts 9
cp 0
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 5
A timezoneOffsetInSecondsToHours() 0 11 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\Util;
20
21
/**
22
 * The city class representing a city object.
23
 */
24
class City extends Location
25
{
26
    /**
27
     * @var int The city id.
28
     */
29
    public $id;
30
31
    /**
32
     * @var string The name of the city.
33
     */
34
    public $name;
35
36
    /**
37
     * @var string The abbreviation of the country the city is located in.
38
     */
39
    public $country;
40
41
    /**
42
     * @var int The city's population
43
     */
44
    public $population;
45
46
    /**
47
     * @var \DateTimeZone|null The shift in seconds from UTC
48
     */
49
    public $timezone;
50
51
    /**
52
     * Create a new city object.
53
     *
54
     * @param int    $id             The city id.
55
     * @param string $name           The name of the city.
56
     * @param float  $lat            The latitude of the city.
57
     * @param float  $lon            The longitude of the city.
58
     * @param string $country        The abbreviation of the country the city is located in
59
     * @param int    $population     The city's population.
60
     * @param int    $timezoneOffset The shift in seconds from UTC.
61
     *
62
     * @internal
63
     */
64
    public function __construct($id, $name = null, $lat = null, $lon = null, $country = null, $population = null, $timezoneOffset = null)
65
    {
66
        $this->id = (int)$id;
67
        $this->name = isset($name) ? (string)$name : null;
68
        $this->country = isset($country) ? (string)$country : null;
69
        $this->population = isset($population) ? (int)$population : null;
70
        $this->timezone = isset($timezoneOffset) ? new \DateTimeZone(self::timezoneOffsetInSecondsToHours($timezoneOffset)) : null;
71
72
        parent::__construct($lat, $lon);
73
    }
74
75
    /**
76
     * @param int $offset The timezone offset in seconds from UTC.
77
     * @return int        The timezone offset in +/-HH:MM form.
78
     */
79
    private static function timezoneOffsetInSecondsToHours($offset)
80
    {
81
        $minutes = floor(abs($offset) / 60) % 60;
82
        $hours = floor(abs($offset) / 3600);
83
84
        $result = $offset < 0 ? "-" : "+";
85
        $result .= str_pad($hours, 2, "0", STR_PAD_LEFT);
86
        $result .= str_pad($minutes, 2, "0", STR_PAD_LEFT);
87
88
        return $result;
89
    }
90
}
91