Skip to content

Commit

Permalink
Merge pull request #23 from dkozickis/phenom-fix
Browse files Browse the repository at this point in the history
Ability to detect more than 1 phenomena.
  • Loading branch information
jpjoux committed Feb 22, 2017
2 parents 870a864 + 4b81805 commit 7ea3c6f
Show file tree
Hide file tree
Showing 6 changed files with 211 additions and 93 deletions.
69 changes: 38 additions & 31 deletions src/ChunkDecoder/EvolutionChunkDecoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,16 +51,16 @@ public function getRemaining()

public function getRegexp()
{
$type = '(BECMG\s+|TEMPO\s+|FM|PROB[034]{2}\s+){1}';
$type = '(BECMG\s+|TEMPO\s+|FM|PROB[034]{2}\s+){1}';
$period = '([0-9]{4}/[0-9]{4}\s+|[0-9]{6}\s+){1}';
$rest = '(.*)';
$rest = '(.*)';

return "#$type$period$rest#";
}

/**
* @param string $remaining_taf
* @param DecodedTaf $decoded_taf
* @param string $remaining_taf
* @param DecodedTaf $decoded_taf
* @return string
*/
public function parse($remaining_taf, $decoded_taf)
Expand All @@ -74,6 +74,7 @@ public function parse($remaining_taf, $decoded_taf)
if ($found == null) {
// the first chunk didn't match anything, so we remove it to avoid an infinite loop
$this->remaining = preg_replace('#(\S+\s+)(.*)#', '', $remaining_taf);

return;
}

Expand All @@ -88,12 +89,12 @@ public function parse($remaining_taf, $decoded_taf)
if ($evo_type == 'BECMG' || $evo_type == 'TEMPO') {
$periodArr = explode('/', $evo_period);
$evolution->setFromDay(intval(mb_substr($periodArr[0], 0, 2)));
$evolution->setFromTime(mb_substr($periodArr[0], 2, 2) . ':00 UTC');
$evolution->setFromTime(mb_substr($periodArr[0], 2, 2).':00 UTC');
$evolution->setToDay(intval(mb_substr($periodArr[1], 0, 2)));
$evolution->setToTime(mb_substr($periodArr[1], 2, 2) . ':00 UTC');
$evolution->setToTime(mb_substr($periodArr[1], 2, 2).':00 UTC');
} else {
$evolution->setFromDay(intval(mb_substr($evo_period, 0, 2)));
$evolution->setFromTime(mb_substr($evo_period, 2, 2) . ':' . mb_substr($evo_period, 4, 2) . ' UTC');
$evolution->setFromTime(mb_substr($evo_period, 2, 2).':'.mb_substr($evo_period, 4, 2).' UTC');
}

// rest
Expand All @@ -105,9 +106,9 @@ public function parse($remaining_taf, $decoded_taf)
/**
* Extract the weather elements (surface winds, visibility, etc) between 2 evolution tags (BECMG, TEMPO or FM)
*
* @param Evolution $evolution
* @param string $chunk
* @param DecodedTaf $decoded_taf
* @param Evolution $evolution
* @param string $chunk
* @param DecodedTaf $decoded_taf
* @throws ChunkDecoderException
* @return string
*/
Expand Down Expand Up @@ -143,10 +144,12 @@ private function parseEntitiesChunk($evolution, $chunk, $decoded_taf)
$entity = $result[$entity_name];
if ($entity == null && $entity_name != 'visibility') {
// visibility will be null if cavok is true but we still want to add the evolution
throw new ChunkDecoderException($chunk,
throw new ChunkDecoderException(
$chunk,
$remaining_evo,
'Bad format for weather evolution',
$this);
$this
);
}
if ($entity_name == 'maxTemperature') {
$this->addEvolution($decoded_taf, $evolution, $result, 'maxTemperature');
Expand All @@ -161,10 +164,12 @@ private function parseEntitiesChunk($evolution, $chunk, $decoded_taf)
} catch (ChunkDecoderException $e) {
if (++$tries == count($this->decoder_chain)) {
if ($this->strict) {
throw new ChunkDecoderException($chunk,
throw new ChunkDecoderException(
$chunk,
$remaining_evo,
'Bad format for evolution information',
$this);
$this
);
} else {
// we tried all the chunk decoders on the first chunk and none of them got a match,
// so we drop it
Expand All @@ -180,9 +185,9 @@ private function parseEntitiesChunk($evolution, $chunk, $decoded_taf)
/**
* Look recursively for probability (PROBnn) attributes and embed a new evolution object one level deeper for each
*
* @param Evolution $evolution
* @param string $chunk
* @param DecodedTaf $decoded_taf
* @param Evolution $evolution
* @param string $chunk
* @param DecodedTaf $decoded_taf
* @return string
*/
private function probabilityChunkDecoder($evolution, $chunk, $decoded_taf)
Expand All @@ -195,10 +200,10 @@ private function probabilityChunkDecoder($evolution, $chunk, $decoded_taf)
return $chunk;
}

$prob = trim($found[1]);
$type = trim($found[2]);
$period = trim($found[3]);
$remaining = trim($found[4]);
$prob = trim($found[1]);
$type = trim($found[2]);
$period = trim($found[3]);
$remaining = trim($found[4]);

if (strpos($prob, 'PROB') !== false) {
$evolution->setProbability($prob);
Expand All @@ -210,9 +215,9 @@ private function probabilityChunkDecoder($evolution, $chunk, $decoded_taf)
}
$periodArr = explode('/', $period);
$embeddedEvolution->setFromDay(intval(mb_substr($periodArr[0], 0, 2)));
$embeddedEvolution->setFromTime(mb_substr($periodArr[0], 2, 2) . ':00 UTC');
$embeddedEvolution->setFromTime(mb_substr($periodArr[0], 2, 2).':00 UTC');
$embeddedEvolution->setToDay(intval(mb_substr($periodArr[1], 0, 2)));
$embeddedEvolution->setToTime(mb_substr($periodArr[1], 2, 2) . ':00 UTC');
$embeddedEvolution->setToTime(mb_substr($periodArr[1], 2, 2).':00 UTC');

$evolution->addEvolution($embeddedEvolution);
// recurse on the remaining chunk to extract the weather elements it contains
Expand All @@ -225,10 +230,10 @@ private function probabilityChunkDecoder($evolution, $chunk, $decoded_taf)
/**
* Add the evolution to the decodedTaf's entity
*
* @param DecodedTaf $decoded_taf
* @param Evolution $evolution
* @param array $result
* @param string $entity_name
* @param DecodedTaf $decoded_taf
* @param Evolution $evolution
* @param array $result
* @param string $entity_name
*/
private function addEvolution($decoded_taf, $evolution, $result, $entity_name)
{
Expand All @@ -245,10 +250,10 @@ private function addEvolution($decoded_taf, $evolution, $result, $entity_name)
}

// get the original entity from the decoded taf or a new one decoded taf doesn't contain it yet
$getter_name = 'get' . ucfirst($entity_name);
$setter_name = 'set' . ucfirst($entity_name);
$getter_name = 'get'.ucfirst($entity_name);
$setter_name = 'set'.ucfirst($entity_name);
$decoded_entity = $decoded_taf->$getter_name();
if ($decoded_entity == null || $entity_name == 'clouds') {
if ($decoded_entity == null || $entity_name == 'clouds' || $entity_name == 'weatherPhenomenons') {
// that entity is not in the decoded_taf yet, or it's a cloud layer which is a special case
$decoded_entity = $this->instantiateEntity($entity_name);
}
Expand All @@ -259,6 +264,8 @@ private function addEvolution($decoded_taf, $evolution, $result, $entity_name)
// update the decoded taf's entity or add the new one to it
if ($entity_name == 'clouds') {
$decoded_taf->addCloud($decoded_entity);
} elseif ($entity_name == 'weatherPhenomenons') {
$decoded_taf->addWeatherPhenomenon($decoded_entity);
} else {
$decoded_taf->$setter_name($decoded_entity);
}
Expand All @@ -274,7 +281,7 @@ private function instantiateEntity($entity_name)
{
$entity = null;

if ($entity_name == 'weatherPhenomenon') {
if ($entity_name == 'weatherPhenomenons') {
$entity = new WeatherPhenomenon();
} else if ($entity_name == 'maxTemperature') {
$entity = new Temperature();
Expand Down
29 changes: 16 additions & 13 deletions src/ChunkDecoder/WeatherChunkDecoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ public function getRegexp()
{
$desc_regexp = implode(self::$desc_dic, '|');
$phenom_regexp = implode(self::$phenom_dic, '|');
$pw_regexp = "([-+]|VC)?($desc_regexp)?($phenom_regexp)?($desc_regexp)?($phenom_regexp)?";
$pw_regexp = "([-+]|VC)?($desc_regexp)?($phenom_regexp)?($phenom_regexp)?($phenom_regexp)?";

return "#^($pw_regexp )?()?#";
return "#^($pw_regexp )?($pw_regexp )?($pw_regexp )?()?#";
}

public function parse($remaining_taf, $cavok = false)
Expand All @@ -37,20 +37,23 @@ public function parse($remaining_taf, $cavok = false)
$found = $result['found'];
$new_remaining_taf = $result['remaining'];

$weatherPhenom = null;
if (trim($found[1]) != null && $found[4] != '//') {
$weatherPhenom = new WeatherPhenomenon();
$weatherPhenom->setIntensityProximity($found[2]);
$weatherPhenom->setDescriptor($found[3]);
for ($k = 3; $k <= 5; $k++) {
if ($found[1+$k] != null) {
$weatherPhenom->addPhenomenon($found[1+$k]);
$result = array(
'weatherPhenomenons' => array(),
);

for ($i = 1; $i <= 13; $i += 6) {
if ($found[$i] != null && $found[$i + 3] != '//') {
$weather = new WeatherPhenomenon();
$weather->setIntensityProximity($found[$i + 1]);
$weather->setDescriptor($found[$i + 2]);
for ($k = 3; $k <= 5; ++$k) {
if ($found[$i + $k] != null) {
$weather->addPhenomenon($found[$i + $k]);
}
}
$result['weatherPhenomenons'][] = $weather;
}
}
$result = array(
'weatherPhenomenon' => $weatherPhenom,
);

// return result + remaining taf
return array(
Expand Down
18 changes: 13 additions & 5 deletions src/Entity/DecodedTaf.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class DecodedTaf
private $cavok;

// weather phenomenon
private $weather_phenomenon;
private $weather_phenomenons;

// cloud layers information
private $clouds;
Expand All @@ -49,6 +49,7 @@ public function __construct($raw_taf)
$this->cavok = false;
$this->decoding_exceptions = array();
$this->clouds = array();
$this->weather_phenomenons = array();
}

/**
Expand Down Expand Up @@ -185,16 +186,23 @@ public function getCavok()
return $this->cavok;
}

public function setWeatherPhenomenon($weather_phenomenon)
public function setWeatherPhenomenons(array $weather_phenomenons)
{
$this->weather_phenomenon = $weather_phenomenon;
$this->weather_phenomenons = $weather_phenomenons;

return $this;
}

public function getWeatherPhenomenon()
public function addWeatherPhenomenon($weather_phenomenon)
{
return $this->weather_phenomenon;
$this->weather_phenomenons[] = $weather_phenomenon;

return $this;
}

public function getWeatherPhenomenons()
{
return $this->weather_phenomenons;
}

public function setClouds(array $clouds)
Expand Down
18 changes: 11 additions & 7 deletions tests/ChunkDecoder/EvolutionChunkDecoderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,17 @@ public function testParse($strict, $base, $evoChunk, $type, $probability, $fromD

if ($elements['weather_phenomena']) {
/** @var array $weatherPhenomena */
$weatherPhenomena = $decoded_taf->getWeatherPhenomenon()->getEvolutions();
$this->assertEquals(
$elements['weather_intensity'],
$weatherPhenomena[0]->getEntity()->getIntensityProximity()
);
$this->assertEquals($elements['weather_desc'], $weatherPhenomena[0]->getEntity()->getDescriptor());
$this->assertEquals($elements['weather_phenomena'], $weatherPhenomena[0]->getEntity()->getPhenomena());
if(!is_null($decoded_taf->getWeatherPhenomenons())){
$proxyWeatherPhenomena = $decoded_taf->getWeatherPhenomenons();
$weatherPhenomena = $proxyWeatherPhenomena[0]->getEvolutions();
$entity = $weatherPhenomena[0]->getEntity();
$this->assertEquals(
$elements['weather_intensity'],
$entity[0]->getIntensityProximity()
);
$this->assertEquals($elements['weather_desc'], $entity[0]->getDescriptor());
$this->assertEquals($elements['weather_phenomena'], $entity[0]->getPhenomena());
}
}

/** @var array $clouds */
Expand Down

0 comments on commit 7ea3c6f

Please sign in to comment.