Skip to content

Commit

Permalink
Updated methods and had a general clean-up of code comments etc.
Browse files Browse the repository at this point in the history
  • Loading branch information
allebb committed Apr 14, 2014
1 parent 2f14989 commit 3c76d17
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 23 deletions.
2 changes: 1 addition & 1 deletion examples/nyc_jfk.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
6 changes: 5 additions & 1 deletion examples/stansted.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
47 changes: 33 additions & 14 deletions lib/Metar.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 <[email protected]>
* @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
{

Expand All @@ -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.
Expand All @@ -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);
}

/**
Expand All @@ -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')
{
Expand Down
18 changes: 16 additions & 2 deletions lib/Providers/MetarProviderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 <[email protected]>
* @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();
}
25 changes: 21 additions & 4 deletions lib/Providers/Noaa.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,26 @@

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 <[email protected]>
* @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;

/**
* METAR service provided by the US NOAA (National Oceanic and Atmospheric Administration)
* @link http://www.noaa.gov/
*/

class Noaa extends MetarHTTPClient implements MetarProviderInterface
{

Expand All @@ -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();
}

}
20 changes: 19 additions & 1 deletion lib/Providers/Vatsim.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 <[email protected]>
* @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;

Expand All @@ -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));

Expand All @@ -30,4 +43,9 @@ public function getMetarDataString()
return $data;
}

public function __toString()
{
return $this->getMetarDataString();
}

}

0 comments on commit 3c76d17

Please sign in to comment.