Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
mohit-rocks committed Jan 25, 2024
1 parent d2f8b4d commit 215a5b5
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 9 deletions.
34 changes: 31 additions & 3 deletions src/Warning/Serializer/WarningInfoNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@

namespace BomWeather\Warning\Serializer;

use BomWeather\Forecast\Area;
use BomWeather\Forecast\Forecast;
use BomWeather\Util\BaseNormalizer;
use BomWeather\Warning\Warning;
use BomWeather\Warning\WarningInfo;
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;

Expand All @@ -31,8 +28,39 @@ public function denormalize($data, $type, $format = NULL, array $context = []) {

$warningInfo = (new WarningInfo());

if (isset($data['text'])) {
if ($this->isAssoc($data['text'])) {
$text = $data['text'];
$this->setTextValue($text, $warningInfo);
}
else {
\array_map(function ($text) use ($warningInfo): void {
$this->setTextValue($text, $warningInfo);
}, $data['text'], [$warningInfo]);
}
}

return $warningInfo;
}

/**
* Sets a text value.
*
* @param array $text
* The text array.
* @param \BomWeather\Warning\WarningInfo $warningInfo
* The warning info.
*/
public function setTextValue(array $text, WarningInfo $warningInfo): void {
match ($text['@type']) {
'warning_title' => $warningInfo->setWarningTitle($text['#']),
'preamble' => $warningInfo->setPreamble($text['#']),
'warning_advice' => \array_map(function ($advice) use ($warningInfo): void {
$warningInfo->setWarningAdvice($advice);
}, $text['p']),
'warning_next_issue' => $warningInfo->setWarningNextIssue($text['#']),
default => '',
};
}

}
2 changes: 1 addition & 1 deletion src/Warning/Serializer/WarningNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
namespace BomWeather\Warning\Serializer;

use BomWeather\Forecast\Area;
use BomWeather\Forecast\Forecast;
use BomWeather\Util\BaseNormalizer;
use BomWeather\Warning\Warning;
use BomWeather\Warning\WarningInfo;
Expand Down Expand Up @@ -43,6 +42,7 @@ public function denormalize($data, $type, $format = NULL, array $context = []) {
}

$warningInfo = $this->serializer->denormalize($data['warning']['warning-info'], WarningInfo::class);
$warning->setWarningInfo($warningInfo);

return $warning;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Warning/Serializer/WarningSerializerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
namespace BomWeather\Warning\Serializer;

use BomWeather\Forecast\Serializer\AreaNormalizer;
use BomWeather\Forecast\Serializer\ForecastNormalizer;
use BomWeather\Forecast\Serializer\ForecastPeriodNormalizer;
use Symfony\Component\Serializer\Encoder\XmlEncoder;
use Symfony\Component\Serializer\Normalizer\DateTimeNormalizer;
Expand All @@ -32,6 +31,7 @@ public static function create(string $rootNode = 'product'): Serializer {
new WarningNormalizer(),
new WarningInfoNormalizer(),
new AreaNormalizer(),
new ForecastPeriodNormalizer(),
];
return new Serializer($normalizers, $encoders);
}
Expand Down
22 changes: 21 additions & 1 deletion src/Warning/Warning.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use BomWeather\Forecast\Area;

/**
* A value object for weather forcasts.
* A value object for weather warning.
*/
final class Warning {

Expand Down Expand Up @@ -51,6 +51,11 @@ final class Warning {
*/
protected \DateTimeImmutable $issueTime;

/**
* The warning information.
*/
protected ?WarningInfo $warningInfo = NULL;

/**
* Gets the regions.
*
Expand Down Expand Up @@ -246,4 +251,19 @@ public function addCoast(Area $coast): Warning {
return $this;
}

/**
* Gets the warning information.
*/
public function getWarningInfo(): ?WarningInfo {
return $this->warningInfo;
}

/**
* Sets the warning information.
*/
public function setWarningInfo(?WarningInfo $warningInfo): Warning {
$this->warningInfo = $warningInfo;
return $this;
}

}
4 changes: 1 addition & 3 deletions src/Warning/WarningInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@

namespace BomWeather\Warning;

use BomWeather\Forecast\Area;

/**
* A value object for weather forcasts.
* A value object for weather warning information.
*/
final class WarningInfo {

Expand Down

0 comments on commit 215a5b5

Please sign in to comment.