From 3c76d17a4f5867df96d55e32ef66951c80914a39 Mon Sep 17 00:00:00 2001 From: Bobby Allen Date: Mon, 14 Apr 2014 13:20:18 +0100 Subject: [PATCH] Updated methods and had a general clean-up of code comments etc. --- examples/nyc_jfk.php | 2 +- examples/stansted.php | 6 ++- lib/Metar.php | 47 +++++++++++++++++------- lib/Providers/MetarProviderInterface.php | 18 ++++++++- lib/Providers/Noaa.php | 25 +++++++++++-- lib/Providers/Vatsim.php | 20 +++++++++- 6 files changed, 95 insertions(+), 23 deletions(-) diff --git a/examples/nyc_jfk.php b/examples/nyc_jfk.php index 3acaf38..cd49dde 100644 --- a/examples/nyc_jfk.php +++ b/examples/nyc_jfk.php @@ -4,6 +4,6 @@ use Ballen\Metar\Metar; -$metar = new Metar('KJFK'); // 'KJFK' is the ICAO code for New York's JFK International Airport. +$metar = new Metar('KJFK'); // 'KJFK' is the ICAO code for New York's JFK International Airport, by default we query the NOAA (real-world) METAR service! echo 'METAR information for New York JFK International is: ' . $metar; diff --git a/examples/stansted.php b/examples/stansted.php index 1b91a4c..aaea510 100644 --- a/examples/stansted.php +++ b/examples/stansted.php @@ -6,4 +6,8 @@ $stansted = new Metar('EGSS'); -echo 'METAR: ' . $stansted . ' published ' . $stansted->getPublishedDate(); +// By default the class will query info from real-world 'NOAA' however if using on VATSIM network you +// may wish to query VATSIM instead to ensure you are using the same as the network. +$stansted->setProvider('VATSIM'); + +echo 'METAR information for London Stansted: ' . $stansted; diff --git a/lib/Metar.php b/lib/Metar.php index 6f19525..8647d53 100644 --- a/lib/Metar.php +++ b/lib/Metar.php @@ -2,6 +2,19 @@ namespace Ballen\Metar; +/** + * Metar + * + * Metar is a PHP 5.4+ library for retrieveing weather reports (METAR infomation), + * the library supports multiple 'METAR prodivers' including NOAA and VATSIM. + * + * @author Bobby Allen + * @version 1.0.0 + * @license http://www.gnu.org/licenses/gpl-3.0.html + * @link https://github.com/bobsta63/metar + * @link http://www.bobbyallen.me + * + */ class Metar { @@ -11,10 +24,10 @@ class Metar const SERVICES_NAMESPACE = 'Ballen\Metar\Providers'; /** - * The METAR service of which to use to provide the METAR report. - * @var string + * Stores the requested airfield/port ICAO code. + * @var string ICAO code. */ - private $metarProvider; + private $icao; /** * The METAR string retrieved from the web service. @@ -23,32 +36,38 @@ class Metar private $metar; /** - * Gather METAR infomation for a given ICAO code. - * @param string $icao Four-digital ICAO airport code. - * @throws \InvalidArgumentException + * The METAR service of which to use to provide the METAR report. + * @var string METAR data provider class/file name. + */ + private $metarProvider; + + /** + * Initiates a new METAR object. + * @param string $icao The airfeild/airport ICAO code. */ public function __construct($icao) { // We force the format of the ICAO code to be upper case! - $icao = strtoupper($icao); + $this->icao = strtoupper($icao); // Validate the ICAO code, just check some standard formatting stuff really! - $this->validateIcao($icao); + $this->validateIcao($this->icao); // Set a default provider, can be overrideen with 'setProvider()' function. $this->setProvider('NOAA'); - - $this->metar = (new $this->metarProvider($icao))->getMetarDataString(); } /** - * Returns the string METAR message. + * Returns the RAW METAR message as a string. * @return string The RAW METAR message. */ public function __toString() { - return $this->metar; + // We'll set the object 'metar' property to the station metar data as well as return the string so + // that in future we can extend further and use the METAR string in other parts of our class after it has + // been retrieved! + return $this->metar = (string) new $this->metarProvider($this->icao); } /** @@ -64,8 +83,8 @@ private function validateIcao($icao) } /** - * - * @param type $provider + * Changes the default 'NOAA' METAR service provider to another one eg. 'VATSIM'. + * @param string $provider METAR Provider Class/File name. */ public function setProvider($provider = 'Noaa') { diff --git a/lib/Providers/MetarProviderInterface.php b/lib/Providers/MetarProviderInterface.php index 39ca51d..27acbfc 100644 --- a/lib/Providers/MetarProviderInterface.php +++ b/lib/Providers/MetarProviderInterface.php @@ -2,10 +2,24 @@ namespace Ballen\Metar\Providers; +/** + * Metar + * + * Metar is a PHP 5.4+ library for retrieveing weather reports (METAR infomation), + * the library supports multiple 'METAR prodivers' including NOAA and VATSIM. + * + * @author Bobby Allen + * @version 1.0.0 + * @license http://www.gnu.org/licenses/gpl-3.0.html + * @link https://github.com/bobsta63/metar + * @link http://www.bobbyallen.me + * + */ + interface MetarProviderInterface { function __construct($icao); - - function getMetarDataString(); + + function __toString(); } diff --git a/lib/Providers/Noaa.php b/lib/Providers/Noaa.php index cc16f6e..ce1ce07 100644 --- a/lib/Providers/Noaa.php +++ b/lib/Providers/Noaa.php @@ -2,6 +2,19 @@ namespace Ballen\Metar\Providers; +/** + * Metar + * + * Metar is a PHP 5.4+ library for retrieveing weather reports (METAR infomation), + * the library supports multiple 'METAR prodivers' including NOAA and VATSIM. + * + * @author Bobby Allen + * @version 1.0.0 + * @license http://www.gnu.org/licenses/gpl-3.0.html + * @link https://github.com/bobsta63/metar + * @link http://www.bobbyallen.me + * + */ use \Ballen\Metar\Providers\MetarProviderInterface; use \Ballen\Metar\Helpers\MetarHTTPClient; @@ -9,7 +22,6 @@ * METAR service provided by the US NOAA (National Oceanic and Atmospheric Administration) * @link http://www.noaa.gov/ */ - class Noaa extends MetarHTTPClient implements MetarProviderInterface { @@ -21,16 +33,21 @@ public function __construct($icao) $this->icao = $icao; } - public function getMetarDataString() + private function getMetarDataString() { - + $data = $this->getMetarAPIResponse(str_replace('{{_ICAO_}}', $this->icao, $this->serviceUrl)); // The NOAA web service provides a human readable timestamp of when the report was last generated but we don't care about that so we'll jump to the next line (the actual METAR string) $lines = explode($this->icao, $data); $data = trim($this->icao . $lines[1]); - + return $data; } + public function __toString() + { + return $this->getMetarDataString(); + } + } diff --git a/lib/Providers/Vatsim.php b/lib/Providers/Vatsim.php index 248512b..bbd7bfa 100644 --- a/lib/Providers/Vatsim.php +++ b/lib/Providers/Vatsim.php @@ -2,6 +2,19 @@ namespace Ballen\Metar\Providers; +/** + * Metar + * + * Metar is a PHP 5.4+ library for retrieveing weather reports (METAR infomation), + * the library supports multiple 'METAR prodivers' including NOAA and VATSIM. + * + * @author Bobby Allen + * @version 1.0.0 + * @license http://www.gnu.org/licenses/gpl-3.0.html + * @link https://github.com/bobsta63/metar + * @link http://www.bobbyallen.me + * + */ use \Ballen\Metar\Providers\MetarProviderInterface; use \Ballen\Metar\Helpers\MetarHTTPClient; @@ -20,7 +33,7 @@ public function __construct($icao) $this->icao = $icao; } - public function getMetarDataString() + private function getMetarDataString() { $data = $this->getMetarAPIResponse(str_replace('{{_ICAO_}}', $this->icao, $this->serviceUrl)); @@ -30,4 +43,9 @@ public function getMetarDataString() return $data; } + public function __toString() + { + return $this->getMetarDataString(); + } + }