Skip to content

Commit

Permalink
Fixed bug where measurements were incorrect if EPS wasn't available. …
Browse files Browse the repository at this point in the history
…Changed measurements default '0.0' values to 'null'. Converted danish named precipitation types and wind directions to english names and abbreviations.
  • Loading branch information
rugaard committed Jul 19, 2019
1 parent bea71e8 commit cfd4c47
Show file tree
Hide file tree
Showing 14 changed files with 262 additions and 150 deletions.
6 changes: 3 additions & 3 deletions src/DTO/Measurements/Humidity.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class Humidity extends AbstractDTO
*
* @var float|null
*/
protected $value = 0.0;
protected $value;

/**
* Unit type.
Expand Down Expand Up @@ -49,7 +49,7 @@ public function __construct(array $data = [])
*/
public function parse(array $data): void
{
$this->setValue((float) $data['humidity']);
$this->setValue($data['humidity'] ?? null);
}

/**
Expand All @@ -60,7 +60,7 @@ public function parse(array $data): void
*/
public function setValue(?float $value) : self
{
$this->value = $value !== null ? (float) $value : 0.0;
$this->value = $value !== null ? (float) $value : null;
return $this;
}

Expand Down
60 changes: 45 additions & 15 deletions src/DTO/Measurements/Precipitation.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,21 @@ class Precipitation extends AbstractDTO
*
* @var float|null
*/
protected $value = 0.0;
protected $value;

/**
* Lowest predicted amount (uncertain).
*
* @var float
* @var float|null
*/
protected $lowestValue = 0.0;
protected $lowestValue;

/**
* Highest predicted amount (uncertain).
*
* @var float
* @var float|null
*/
protected $highestValue = 0.0;
protected $highestValue;

/**
* Unit type.
Expand Down Expand Up @@ -70,22 +70,52 @@ public function __construct(array $data = [])
*/
public function parse(array $data): void
{
$this->setType($data['precipType'])
->setValue((float) ($data['prec50'] ?? $data['precip3'] ?? null));
// Set precipitation type based on danish key.
$this->setTypeByDanishKey($data['precipType'] ?? null);

// If "temp50" is empty,
// then we know EPS is not available.
if (!empty($data['temp50'])) {
$this->setValue($data['prec50'] ?? null)
->setLowestValue($data['prec10'] ?? null)
->setHighestValue($data['prec90'] ?? null);
} else {
$this->setValue($data['precip1'] ?? null);
}
}

if (!empty($data['prec50'])) {
$this->setLowestValue((float) $data['prec10'])
->setHighestValue((float) $data['prec90']);
/**
* Set precipitation type by danish key.
*
* @param string|null $key
* @return $this
*/
public function setTypeByDanishKey(?string $key) : self
{
switch ($key) {
case 'regn':
$this->setType('rain');
break;
case 'hagl':
$this->setType('hail');
break;
case 'slud':
$this->setType('sleet');
break;
case 'sne':
$this->setType('snow');
break;
}
return $this;
}

/**
* Set precipitation type.
*
* @param string $type
* @param string|null $type
* @return $this
*/
public function setType(string $type) : self
public function setType(?string $type) : self
{
$this->type = $type;
return $this;
Expand All @@ -109,7 +139,7 @@ public function getType() :? string
*/
public function setValue(?float $value) : self
{
$this->value = (float) ($value < 0.1 ? 0.0 : $value);
$this->value = $value !== null ? (float) ($value < 0.1 ? 0.0 : $value) : null;
return $this;
}

Expand All @@ -131,7 +161,7 @@ public function getValue() :? float
*/
public function setLowestValue(?float $value) : self
{
$this->lowestValue = (float) ($value < 0.1 ? 0.0 : $value);
$this->lowestValue = $value !== null ? (float) ($value < 0.1 ? 0.0 : $value) : null;
return $this;
}

Expand All @@ -153,7 +183,7 @@ public function getLowestValue() :? float
*/
public function setHighestValue(?float $value) : self
{
$this->highestValue = (float) ($value < 0.1 ? 0 : $value);
$this->highestValue = $value !== null ? (float) ($value < 0.1 ? 0 : $value) : null;
return $this;
}

Expand Down
8 changes: 4 additions & 4 deletions src/DTO/Measurements/Pressure.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ class Pressure extends AbstractDTO
/**
* Pressure value.
*
* @var float
* @var float|null
*/
protected $value = 0.0;
protected $value;

/**
* Unit type.
Expand Down Expand Up @@ -49,7 +49,7 @@ public function __construct(array $data = [])
*/
public function parse(array $data): void
{
$this->setValue((float) $data['pressure']);
$this->setValue($data['pressure'] ?? null);
}

/**
Expand All @@ -60,7 +60,7 @@ public function parse(array $data): void
*/
public function setValue(?float $value) : self
{
$this->value = $value !== null ? (float) $value : 0.0;
$this->value = $value !== null ? (float) $value : null;
return $this;
}

Expand Down
29 changes: 16 additions & 13 deletions src/DTO/Measurements/Temperature.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,23 @@ class Temperature extends AbstractDTO
/**
* Predicted/expected temperature.
*
* @var float
* @var float|null
*/
protected $value = 0.0;
protected $value;

/**
* Lowest predicted temperature (uncertain).
*
* @var float
* @var float|null
*/
protected $lowestValue = 0.0;
protected $lowestValue;

/**
* Highest predicted temperature (uncertain).
*
* @var float
* @var float|null
*/
protected $highestValue = 0.0;
protected $highestValue;

/**
* Unit type.
Expand Down Expand Up @@ -63,11 +63,14 @@ public function __construct(array $data = [])
*/
public function parse(array $data): void
{
$this->setValue((float) ($data['temp50'] ?? $data['temp'] ?? null));

// If "temp50" is empty,
// then we know EPS is not available.
if (!empty($data['temp50'])) {
$this->setLowestValue((float) $data['temp10'])
->setHighestValue((float) $data['temp90']);
$this->setValue($data['temp50'] ?? null)
->setLowestValue($data['temp10'] ?? null)
->setHighestValue($data['temp90'] ?? null);
} else {
$this->setValue($data['temp'] ?? null);
}
}

Expand All @@ -79,7 +82,7 @@ public function parse(array $data): void
*/
public function setValue(?float $value) : self
{
$this->value = $value !== null ? (float) $value : 0.0;
$this->value = $value !== null ? (float) $value : null;
return $this;
}

Expand All @@ -101,7 +104,7 @@ public function getValue() :? float
*/
public function setLowestValue(?float $value) : self
{
$this->lowestValue = $value !== null ? (float) $value : 0.0;
$this->lowestValue = $value !== null ? (float) $value : null;
return $this;
}

Expand All @@ -123,7 +126,7 @@ public function getLowestValue() :? float
*/
public function setHighestValue(?float $value) : self
{
$this->highestValue = $value !== null ? (float) $value : 0.0;
$this->highestValue = $value !== null ? (float) $value : null;
return $this;
}

Expand Down
8 changes: 4 additions & 4 deletions src/DTO/Measurements/Visibility.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ class Visibility extends AbstractDTO
/**
* Visibility value.
*
* @var float
* @var float|null
*/
protected $value = 0.0;
protected $value;

/**
* Unit type.
Expand Down Expand Up @@ -49,7 +49,7 @@ public function __construct(array $data = [])
*/
public function parse(array $data): void
{
$this->setValue((float) $data['visibility']);
$this->setValue($data['visibility'] ?? null);
}

/**
Expand All @@ -60,7 +60,7 @@ public function parse(array $data): void
*/
public function setValue(?float $value) : self
{
$this->value = $value !== null ? (float) $value : 0.0;
$this->value = $value !== null ? (float) $value : null;
return $this;
}

Expand Down
Loading

0 comments on commit cfd4c47

Please sign in to comment.