You are here

public function KmlNormalizer::normalize in farmOS 2.x

File

modules/core/kml/src/Normalizer/KmlNormalizer.php, line 47

Class

KmlNormalizer
Normalizes GeometryWrapper objects into array for the Kml encoder.

Namespace

Drupal\farm_kml\Normalizer

Code

public function normalize($object, $format = NULL, array $context = []) {

  /** @var \Drupal\farm_geo\GeometryWrapper $object */

  // Convert the geometry to KML.
  $kml_string = $object->geometry
    ->out('kml');

  // Parse the KML string into an XML object.
  // This is necessary so that we can encode the KML into XML with the
  // rest of the asset data.
  $kml = simplexml_load_string($kml_string);
  $kml_name = $kml
    ->getName();
  $kml_value = $kml
    ->children();

  // Build a placemark definition.
  $placemark = [
    '#' => [
      $kml_name => $kml_value,
    ],
  ];

  // Add an ID if provided.
  if (isset($object->properties['id'])) {
    $placemark['@id'] = $object->properties['id'];
  }

  // Add standard KML properties if provided.
  $properties = $this
    ->supportedProperties();
  foreach ($properties as $property_name) {
    if (isset($object->properties[$property_name])) {
      $placemark['#'][$property_name] = $object->properties[$property_name];
    }
  }
  return $placemark;
}