-
-
Notifications
You must be signed in to change notification settings - Fork 120
Expand file tree
/
Copy pathCity.php
More file actions
113 lines (99 loc) · 3.21 KB
/
City.php
File metadata and controls
113 lines (99 loc) · 3.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
<?php
/*
* OpenWeatherMap-PHP-API — A PHP API to parse weather data from https://OpenWeatherMap.org.
*
* @license MIT
*
* Please see the LICENSE file distributed with this source code for further
* information regarding copyright and licensing.
*
* Please visit the following links to read about the usage policies and the license of
* OpenWeatherMap data before using this library:
*
* @see https://OpenWeatherMap.org/price
* @see https://OpenWeatherMap.org/terms
* @see https://OpenWeatherMap.org/appid
*/
namespace Cmfcmf\OpenWeatherMap\Util;
/**
* The city class representing a city object.
*/
class City extends Location
{
/**
* @var int The city id.
*/
public $id;
/**
* @var string The name of the city.
*/
public $name;
/**
* @var string The abbreviation of the country the city is located in.
*/
public $country;
/**
* @var string|null The state in which the city is located.
*/
public $state;
/**
* @var array<string,string> An array of internationalised versions of the city's name.
*/
public $localNames = [];
/**
* @var int The city's population
*/
public $population;
/**
* @var \DateTimeZone|null The shift in seconds from UTC
*/
public $timezone;
/**
* Create a new city object.
*
* @param int $id The city id.
* @param string $name The name of the city.
* @param float $lat The latitude of the city.
* @param float $lon The longitude of the city.
* @param string $country The abbreviation of the country the city is located in
* @param int $population The city's population.
* @param int $timezoneOffset The shift in seconds from UTC.
* @param string $state The state in which the city is located.
* @param array $localNames An array of internationalised versions of the city's name.
*
* @internal
*/
public function __construct(
$id,
$name = null,
$lat = null,
$lon = null,
$country = null,
$population = null,
$timezoneOffset = null,
$state = null,
$localNames = []
) {
$this->id = (int)$id;
$this->name = isset($name) ? (string)$name : null;
$this->country = isset($country) ? (string)$country : null;
$this->population = isset($population) ? (int)$population : null;
$this->timezone = isset($timezoneOffset) ? new \DateTimeZone(self::timezoneOffsetInSecondsToHours((int)$timezoneOffset)) : null;
$this->state = $state ?? null;
$this->localNames = $localNames ?? [];
parent::__construct($lat, $lon);
}
/**
* @param int $offset The timezone offset in seconds from UTC.
* @return int The timezone offset in +/-HH:MM form.
*/
private static function timezoneOffsetInSecondsToHours($offset)
{
$minutes = floor(abs($offset) / 60) % 60;
$hours = floor(abs($offset) / 3600);
$result = $offset < 0 ? "-" : "+";
$result .= str_pad($hours, 2, "0", STR_PAD_LEFT);
$result .= str_pad($minutes, 2, "0", STR_PAD_LEFT);
return $result;
}
}