Skip to content

Commit

Permalink
WIP: Initial commit.
Browse files Browse the repository at this point in the history
  • Loading branch information
mohit-rocks committed Jan 24, 2024
1 parent f7a65bb commit d2f8b4d
Show file tree
Hide file tree
Showing 5 changed files with 502 additions and 0 deletions.
38 changes: 38 additions & 0 deletions src/Warning/Serializer/WarningInfoNormalizer.php
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

View workflow job for this annotation

GitHub Actions / build (8.3)

Unused use statement

Check warning on line 7 in src/Warning/Serializer/WarningInfoNormalizer.php

View workflow job for this annotation

GitHub Actions / build (--prefer-lowest, 8.1)

Unused use statement

Check warning on line 7 in src/Warning/Serializer/WarningInfoNormalizer.php

View workflow job for this annotation

GitHub Actions / build (8.1)

Unused use statement

Check warning on line 7 in src/Warning/Serializer/WarningInfoNormalizer.php

View workflow job for this annotation

GitHub Actions / build (--prefer-lowest, 8.3)

Unused use statement

Check warning on line 7 in src/Warning/Serializer/WarningInfoNormalizer.php

View workflow job for this annotation

GitHub Actions / build (8.2)

Unused use statement
use BomWeather\Forecast\Forecast;

Check warning on line 8 in src/Warning/Serializer/WarningInfoNormalizer.php

View workflow job for this annotation

GitHub Actions / build (8.3)

Unused use statement

Check warning on line 8 in src/Warning/Serializer/WarningInfoNormalizer.php

View workflow job for this annotation

GitHub Actions / build (--prefer-lowest, 8.1)

Unused use statement

Check warning on line 8 in src/Warning/Serializer/WarningInfoNormalizer.php

View workflow job for this annotation

GitHub Actions / build (8.1)

Unused use statement

Check warning on line 8 in src/Warning/Serializer/WarningInfoNormalizer.php

View workflow job for this annotation

GitHub Actions / build (--prefer-lowest, 8.3)

Unused use statement

Check warning on line 8 in src/Warning/Serializer/WarningInfoNormalizer.php

View workflow job for this annotation

GitHub Actions / build (8.2)

Unused use statement
use BomWeather\Util\BaseNormalizer;
use BomWeather\Warning\Warning;

Check warning on line 10 in src/Warning/Serializer/WarningInfoNormalizer.php

View workflow job for this annotation

GitHub Actions / build (8.3)

Unused use statement

Check warning on line 10 in src/Warning/Serializer/WarningInfoNormalizer.php

View workflow job for this annotation

GitHub Actions / build (--prefer-lowest, 8.1)

Unused use statement

Check warning on line 10 in src/Warning/Serializer/WarningInfoNormalizer.php

View workflow job for this annotation

GitHub Actions / build (8.1)

Unused use statement

Check warning on line 10 in src/Warning/Serializer/WarningInfoNormalizer.php

View workflow job for this annotation

GitHub Actions / build (--prefer-lowest, 8.3)

Unused use statement

Check warning on line 10 in src/Warning/Serializer/WarningInfoNormalizer.php

View workflow job for this annotation

GitHub Actions / build (8.2)

Unused use statement
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

View workflow job for this annotation

GitHub Actions / build (8.3)

Functions must not contain multiple empty lines in a row; found 2 empty lines

Check failure on line 33 in src/Warning/Serializer/WarningInfoNormalizer.php

View workflow job for this annotation

GitHub Actions / build (--prefer-lowest, 8.1)

Functions must not contain multiple empty lines in a row; found 2 empty lines

Check failure on line 33 in src/Warning/Serializer/WarningInfoNormalizer.php

View workflow job for this annotation

GitHub Actions / build (8.1)

Functions must not contain multiple empty lines in a row; found 2 empty lines

Check failure on line 33 in src/Warning/Serializer/WarningInfoNormalizer.php

View workflow job for this annotation

GitHub Actions / build (--prefer-lowest, 8.3)

Functions must not contain multiple empty lines in a row; found 2 empty lines

Check failure on line 33 in src/Warning/Serializer/WarningInfoNormalizer.php

View workflow job for this annotation

GitHub Actions / build (8.2)

Functions must not contain multiple empty lines in a row; found 2 empty lines

return $warningInfo;
}

}
82 changes: 82 additions & 0 deletions src/Warning/Serializer/WarningNormalizer.php
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

View workflow job for this annotation

GitHub Actions / build (8.3)

Unused use statement

Check warning on line 8 in src/Warning/Serializer/WarningNormalizer.php

View workflow job for this annotation

GitHub Actions / build (--prefer-lowest, 8.1)

Unused use statement

Check warning on line 8 in src/Warning/Serializer/WarningNormalizer.php

View workflow job for this annotation

GitHub Actions / build (8.1)

Unused use statement

Check warning on line 8 in src/Warning/Serializer/WarningNormalizer.php

View workflow job for this annotation

GitHub Actions / build (--prefer-lowest, 8.3)

Unused use statement

Check warning on line 8 in src/Warning/Serializer/WarningNormalizer.php

View workflow job for this annotation

GitHub Actions / build (8.2)

Unused use statement
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;
}
}

}
39 changes: 39 additions & 0 deletions src/Warning/Serializer/WarningSerializerFactory.php
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

View workflow job for this annotation

GitHub Actions / build (8.3)

Unused use statement

Check warning on line 8 in src/Warning/Serializer/WarningSerializerFactory.php

View workflow job for this annotation

GitHub Actions / build (--prefer-lowest, 8.1)

Unused use statement

Check warning on line 8 in src/Warning/Serializer/WarningSerializerFactory.php

View workflow job for this annotation

GitHub Actions / build (8.1)

Unused use statement

Check warning on line 8 in src/Warning/Serializer/WarningSerializerFactory.php

View workflow job for this annotation

GitHub Actions / build (--prefer-lowest, 8.3)

Unused use statement

Check warning on line 8 in src/Warning/Serializer/WarningSerializerFactory.php

View workflow job for this annotation

GitHub Actions / build (8.2)

Unused use statement
use BomWeather\Forecast\Serializer\ForecastPeriodNormalizer;

Check warning on line 9 in src/Warning/Serializer/WarningSerializerFactory.php

View workflow job for this annotation

GitHub Actions / build (8.3)

Unused use statement

Check warning on line 9 in src/Warning/Serializer/WarningSerializerFactory.php

View workflow job for this annotation

GitHub Actions / build (--prefer-lowest, 8.1)

Unused use statement

Check warning on line 9 in src/Warning/Serializer/WarningSerializerFactory.php

View workflow job for this annotation

GitHub Actions / build (8.1)

Unused use statement

Check warning on line 9 in src/Warning/Serializer/WarningSerializerFactory.php

View workflow job for this annotation

GitHub Actions / build (--prefer-lowest, 8.3)

Unused use statement

Check warning on line 9 in src/Warning/Serializer/WarningSerializerFactory.php

View workflow job for this annotation

GitHub Actions / build (8.2)

Unused use statement
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);
}

}
Loading

0 comments on commit d2f8b4d

Please sign in to comment.