-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace BomWeather\Warning\Serializer; | ||
|
||
use BomWeather\Forecast\Area; | ||
Check warning on line 7 in src/Warning/Serializer/WarningInfoNormalizer.php
|
||
use BomWeather\Forecast\Forecast; | ||
Check warning on line 8 in src/Warning/Serializer/WarningInfoNormalizer.php
|
||
use BomWeather\Util\BaseNormalizer; | ||
use BomWeather\Warning\Warning; | ||
Check warning on line 10 in src/Warning/Serializer/WarningInfoNormalizer.php
|
||
use BomWeather\Warning\WarningInfo; | ||
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface; | ||
|
||
/** | ||
* Forecast normalizer. | ||
*/ | ||
class WarningInfoNormalizer extends BaseNormalizer { | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected string|array $supportedInterfaceOrClass = WarningInfo::class; | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function denormalize($data, $type, $format = NULL, array $context = []) { | ||
if (!$this->serializer instanceof DenormalizerInterface) { | ||
throw new \RuntimeException('The serializer must implement the DenormalizerInterface.'); | ||
} | ||
|
||
$warningInfo = (new WarningInfo()); | ||
|
||
Check failure on line 33 in src/Warning/Serializer/WarningInfoNormalizer.php
|
||
|
||
return $warningInfo; | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace BomWeather\Warning\Serializer; | ||
|
||
use BomWeather\Forecast\Area; | ||
use BomWeather\Forecast\Forecast; | ||
Check warning on line 8 in src/Warning/Serializer/WarningNormalizer.php
|
||
use BomWeather\Util\BaseNormalizer; | ||
use BomWeather\Warning\Warning; | ||
use BomWeather\Warning\WarningInfo; | ||
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface; | ||
|
||
/** | ||
* Forecast normalizer. | ||
*/ | ||
class WarningNormalizer extends BaseNormalizer { | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected string|array $supportedInterfaceOrClass = Warning::class; | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function denormalize($data, $type, $format = NULL, array $context = []) { | ||
if (!$this->serializer instanceof DenormalizerInterface) { | ||
throw new \RuntimeException('The serializer must implement the DenormalizerInterface.'); | ||
} | ||
|
||
$warning = (new Warning()) | ||
->setIssueTime($this->serializer->denormalize($data['amoc']['issue-time-utc'], \DateTimeImmutable::class)); | ||
|
||
if ($this->isAssoc($data['warning']['area'])) { | ||
$area = $data['warning']['area']; | ||
$this->setValue($area, $warning); | ||
} | ||
else { | ||
\array_map(function ($area) use ($warning): void { | ||
$this->setValue($area, $warning); | ||
}, $data['warning']['area'], [$warning]); | ||
} | ||
|
||
$warningInfo = $this->serializer->denormalize($data['warning']['warning-info'], WarningInfo::class); | ||
|
||
return $warning; | ||
} | ||
|
||
/** | ||
* Sets the area value. | ||
* | ||
* @param array $area | ||
* The area data. | ||
* @param \BomWeather\Warning\Warning $warning | ||
* The warning. | ||
*/ | ||
public function setValue(array $area, Warning $warning): void { | ||
switch ($area['@type']) { | ||
case Area::TYPE_REGION: | ||
$warning->addRegion($this->serializer->denormalize($area, Area::class)); | ||
break; | ||
|
||
case Area::TYPE_DISTRICT: | ||
$warning->addDistrict($this->serializer->denormalize($area, Area::class)); | ||
break; | ||
|
||
case Area::TYPE_METROPOLITAN: | ||
$warning->addMetropolitanArea($this->serializer->denormalize($area, Area::class)); | ||
break; | ||
|
||
case Area::TYPE_LOCATION: | ||
$warning->addLocation($this->serializer->denormalize($area, Area::class)); | ||
break; | ||
|
||
case Area::TYPE_COAST: | ||
$warning->addCoast($this->serializer->denormalize($area, Area::class)); | ||
break; | ||
} | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace BomWeather\Warning\Serializer; | ||
|
||
use BomWeather\Forecast\Serializer\AreaNormalizer; | ||
use BomWeather\Forecast\Serializer\ForecastNormalizer; | ||
Check warning on line 8 in src/Warning/Serializer/WarningSerializerFactory.php
|
||
use BomWeather\Forecast\Serializer\ForecastPeriodNormalizer; | ||
Check warning on line 9 in src/Warning/Serializer/WarningSerializerFactory.php
|
||
use Symfony\Component\Serializer\Encoder\XmlEncoder; | ||
use Symfony\Component\Serializer\Normalizer\DateTimeNormalizer; | ||
use Symfony\Component\Serializer\Serializer; | ||
|
||
/** | ||
* Factory for creating a weather warning data. | ||
*/ | ||
class WarningSerializerFactory { | ||
|
||
/** | ||
* Creates a new forecast serializer. | ||
* | ||
* @param string $rootNode | ||
* (optional) The root node of the XML. | ||
* | ||
* @return \Symfony\Component\Serializer\Serializer | ||
* The serializer. | ||
*/ | ||
public static function create(string $rootNode = 'product'): Serializer { | ||
$encoders = [new XmlEncoder([XmlEncoder::ROOT_NODE_NAME => $rootNode])]; | ||
$normalizers = [ | ||
new DateTimeNormalizer([DateTimeNormalizer::TIMEZONE_KEY => 'UTC']), | ||
new WarningNormalizer(), | ||
new WarningInfoNormalizer(), | ||
new AreaNormalizer(), | ||
]; | ||
return new Serializer($normalizers, $encoders); | ||
} | ||
|
||
} |