Skip to content

Commit

Permalink
Set accessor trait to validate the key before accessing.
Browse files Browse the repository at this point in the history
  • Loading branch information
mohit-rocks committed Feb 1, 2024
1 parent e48748e commit 33a4fd4
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 56 deletions.
75 changes: 23 additions & 52 deletions src/Forecast/Serializer/ForecastPeriodNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@
namespace BomWeather\Forecast\Serializer;

use BomWeather\Forecast\ForecastPeriod;
use BomWeather\Trait\WeatherDataAccessorTrait;
use BomWeather\Util\BaseNormalizer;

/**
* Location period normalizer.
*/
class ForecastPeriodNormalizer extends BaseNormalizer {

use WeatherDataAccessorTrait;

protected string|array $supportedInterfaceOrClass = ForecastPeriod::class;

/**
Expand Down Expand Up @@ -57,24 +60,14 @@ public function denormalize($data, $type, $format = NULL, array $context = []) {
* The period.
*/
protected function setElementValue(array $element, ForecastPeriod $period): void {
switch ($element['@type']) {
case 'forecast_icon_code':
$period->setIconCode($element['#']);
break;

case 'air_temperature_minimum':
$period->setAirTempMinimum((int) $element['#']);
break;

case 'air_temperature_maximum':
$period->setAirTempMaximum((int) $element['#']);
break;

case 'precipitation_range':
$period->setPrecipitationRange($element['#']);
break;

}
$value = $this->accessWeatherData($element, '#');
match ($element['@type']) {
'forecast_icon_code' => $period->setIconCode($value),
'air_temperature_minimum' => $period->setAirTempMinimum((int) $value),
'air_temperature_maximum' => $period->setAirTempMaximum((int) $value),
'precipitation_range' => $period->setPrecipitationRange($value),
default => '',
};
}

/**
Expand All @@ -86,40 +79,18 @@ protected function setElementValue(array $element, ForecastPeriod $period): void
* The period.
*/
public function setTextValue(array $text, ForecastPeriod $period): void {
switch ($text['@type']) {
case 'precis':
$period->setPrecis($text['#']);
break;

case 'probability_of_precipitation':
$period->setProbabilityOfPrecipitation($text['#']);
break;

case 'forecast':
$period->setForecast($text['#']);
break;

case 'uv_alert':
$period->setUvAlert($text['#']);
break;

case 'forecast_seas':
$period->setSeas($text['#']);
break;

case 'forecast_swell1':
$period->setSwell($text['#']);
break;

case 'forecast_weather':
$period->setWeather($text['#']);
break;

case 'forecast_winds':
$period->setWinds($text['#']);
break;

}
$value = $this->accessWeatherData($text, '#');
match ($text['@type']) {
'precis' => $period->setPrecis($value),
'probability_of_precipitation' => $period->setProbabilityOfPrecipitation($value),
'forecast' => $period->setForecast($value),
'uv_alert' => $period->setUvAlert($value),
'forecast_seas' => $period->setSeas($value),
'forecast_swell1' => $period->setSwell($value),
'forecast_weather' => $period->setWeather($value),
'forecast_winds' => $period->setWinds($value),
default => '',
};
}

}
30 changes: 30 additions & 0 deletions src/Trait/WeatherDataAccessorTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

declare(strict_types=1);

namespace BomWeather\Trait;

/**
* Trait to provide general data validation and access methods.
*/
trait WeatherDataAccessorTrait {

/**
* Access the weather data attribute from weather data array.
*
* @param array|null $weatherData
* Array of weather data.
* @param string $key
* Array key to fetch the data.
*/
public function accessWeatherData(?array $weatherData, string $key): mixed {
if (!$weatherData) {
return NULL;
}
if (!\array_key_exists($key, $weatherData)) {
return '';
}
return $weatherData[$key] ?? '';
}

}
12 changes: 8 additions & 4 deletions src/Warning/Serializer/WarningInfoNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace BomWeather\Warning\Serializer;

use BomWeather\Trait\WeatherDataAccessorTrait;
use BomWeather\Util\BaseNormalizer;
use BomWeather\Warning\WarningInfo;
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
Expand All @@ -13,6 +14,8 @@
*/
class WarningInfoNormalizer extends BaseNormalizer {

use WeatherDataAccessorTrait;

/**
* {@inheritdoc}
*/
Expand Down Expand Up @@ -52,13 +55,14 @@ public function denormalize($data, $type, $format = NULL, array $context = []) {
* The warning info.
*/
public function setTextValue(array $text, WarningInfo $warningInfo): void {
$value = $this->accessWeatherData($text, '#');
match ($text['@type']) {
'warning_title' => $warningInfo->setWarningTitle($text['#']),
'preamble' => $warningInfo->setPreamble($text['#']),
'warning_title' => $warningInfo->setWarningTitle($value),
'preamble' => $warningInfo->setPreamble($value),
'warning_advice' => \array_map(function ($advice) use ($warningInfo): void {
$warningInfo->setWarningAdvice($advice);
}, $text['p']),
'warning_next_issue' => $warningInfo->setWarningNextIssue($text['#']),
}, $this->accessWeatherData($text, 'p')),
'warning_next_issue' => $warningInfo->setWarningNextIssue($value),
default => '',
};
}
Expand Down

0 comments on commit 33a4fd4

Please sign in to comment.