Use GeometryWrapper as type when normalizing to geometry_kml format

This commit is contained in:
Paul Weidner 2023-11-20 16:07:02 -08:00 committed by Michael Stenta
parent 8f2efe2724
commit 94c9fe9ed0
2 changed files with 5 additions and 19 deletions

View File

@ -7,6 +7,7 @@ use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\File\FileSystemInterface;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\farm_geo\GeometryWrapper;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\Serializer\SerializerInterface;
@ -148,7 +149,7 @@ class KmlImporter extends FormBase {
// Deserialize the KML placemarks into WKT geometry.
/** @var \Drupal\farm_geo\GeometryWrapper[] $geometries */
$geometries = $this->serializer->deserialize($data, 'geometry_wrapper', 'geometry_kml');
$geometries = $this->serializer->deserialize($data, GeometryWrapper::class, 'geometry_kml');
// Bail if no geometries were found.
if (empty($geometries)) {

View File

@ -22,7 +22,7 @@ class KmlNormalizer implements NormalizerInterface, DenormalizerInterface {
/**
* The supported type to denormalize to.
*/
const TYPE = 'geometry_wrapper';
const TYPE = GeometryWrapper::class;
/**
* The GeoPHP service.
@ -84,22 +84,7 @@ class KmlNormalizer implements NormalizerInterface, DenormalizerInterface {
* {@inheritdoc}
*/
public function supportsNormalization($data, $format = NULL) {
// First check if the format is supported.
if ($format !== static::FORMAT) {
return FALSE;
}
// Change data to an array.
if (!is_array($data)) {
$data = [$data];
}
// Ensure all items are GeometryWrappers.
$invalid_count = count(array_filter($data, function ($object) {
return !$object instanceof GeometryWrapper;
}));
return $invalid_count === 0;
return $data instanceof GeometryWrapper && $format == static::FORMAT;
}
/**
@ -168,7 +153,7 @@ class KmlNormalizer implements NormalizerInterface, DenormalizerInterface {
*/
public function getSupportedTypes(?string $format): array {
return [
GeometryWrapper::class => TRUE,
static::TYPE => TRUE,
];
}