1
|
|
|
<?php |
2
|
|
|
namespace Ballen\Metar; |
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* Metar |
6
|
|
|
* |
7
|
|
|
* Metar is a PHP library for retrieveing weather reports (METAR infomation), |
8
|
|
|
* the library supports multiple 'METAR prodivers' including NOAA, VATSIM and IVAO. |
9
|
|
|
* |
10
|
|
|
* @author Bobby Allen <[email protected]> |
11
|
|
|
* @license http://www.gnu.org/licenses/gpl-3.0.html |
12
|
|
|
* @link https://github.com/allebb/metar |
13
|
|
|
* @link http://www.bobbyallen.me |
14
|
|
|
* |
15
|
|
|
*/ |
16
|
|
|
class Metar |
17
|
|
|
{ |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Stores the default namespace for loading new METAR providers from. |
21
|
|
|
*/ |
22
|
|
|
const DEFAULT_PROVIDER = 'Ballen\Metar\Providers\Noaa'; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Stores the requested airfield/port ICAO code. |
26
|
|
|
* @var string ICAO code. |
27
|
|
|
*/ |
28
|
|
|
private $icao; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* The METAR string retrieved from the web service. |
32
|
|
|
* @var string The raw METAR string retrieved from the web service. |
33
|
|
|
*/ |
34
|
|
|
private $metar; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* The METAR service of which to use to provide the METAR report. |
38
|
|
|
* @var string METAR data provider class/file name. |
39
|
|
|
*/ |
40
|
|
|
private $metarProvider; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Initiates a new METAR object. |
44
|
|
|
* @param string $icao The airfeild/airport ICAO code. |
45
|
|
|
*/ |
46
|
10 |
|
public function __construct($icao) |
47
|
|
|
{ |
48
|
|
|
|
49
|
|
|
// We force the format of the ICAO code to be upper case! |
50
|
10 |
|
$this->icao = strtoupper($icao); |
51
|
|
|
|
52
|
|
|
// Validate the ICAO code, just check some standard formatting stuff really! |
53
|
10 |
|
$this->validateIcao($this->icao); |
54
|
|
|
|
55
|
|
|
// Set a default provider, can be overrideen with 'setProvider()' function. |
56
|
8 |
|
$this->setProvider(); |
57
|
8 |
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Returns the RAW METAR message as a string. |
61
|
|
|
* @return string The RAW METAR message. |
62
|
|
|
*/ |
63
|
6 |
|
public function __toString() |
64
|
|
|
{ |
65
|
|
|
// We'll set the object 'metar' property to the station metar data as well as return the string so |
66
|
|
|
// that in future we can extend further and use the METAR string in other parts of our class after it has |
67
|
|
|
// been retrieved! |
68
|
6 |
|
return $this->metar = (string) new $this->metarProvider($this->icao); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Validates the ICAO code to ensure it's format is valid. |
73
|
|
|
* @param string $icao ICAO code, eg. EGSS |
74
|
|
|
* @throws \InvalidArgumentException |
75
|
|
|
*/ |
76
|
10 |
|
private function validateIcao($icao) |
77
|
|
|
{ |
78
|
10 |
|
if (strlen($icao) != 4) { |
79
|
2 |
|
throw new \InvalidArgumentException('ICAO code does not appear to be a valid format'); |
80
|
|
|
} |
81
|
8 |
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Changes the default 'NOAA' METAR service provider to another one eg. 'VATSIM'. |
85
|
|
|
* @param string $provider METAR Provider Class/File name. |
86
|
|
|
*/ |
87
|
8 |
|
public function setProvider($provider = self::DEFAULT_PROVIDER) |
88
|
|
|
{ |
89
|
8 |
|
if (!class_exists($provider)) { |
90
|
2 |
|
throw new \InvalidArgumentException('The service provider your specified does not exist in the namespace \'' . $provider . '\''); |
91
|
|
|
} |
92
|
8 |
|
$this->metarProvider = $provider; |
93
|
8 |
|
} |
94
|
|
|
} |
95
|
|
|
|