You are here

class Kml in farmOS 2.x

Provides a KML encoder that extends from the XML encoder.

An array of Placemark data prepared for XmlEncoder is expected when encoding.

When decoding, the array of Placemark data is returned with an additional "xml" key containing the individual placemark XML in a string.

Hierarchy

  • class \Drupal\serialization\Encoder\XmlEncoder implements \Symfony\Component\Serializer\SerializerAwareInterface, \Symfony\Component\Serializer\Encoder\EncoderInterface, \Symfony\Component\Serializer\Encoder\DecoderInterface uses \Symfony\Component\Serializer\SerializerAwareTrait
    • class \Drupal\farm_kml\Encoder\Kml

Expanded class hierarchy of Kml

See also

\Symfony\Component\Serializer\Encoder\XmlEncoder

1 string reference to 'Kml'
farm_kml.services.yml in modules/core/kml/farm_kml.services.yml
modules/core/kml/farm_kml.services.yml
1 service uses Kml
serializer.farm_kml.kml.encoder in modules/core/kml/farm_kml.services.yml
Drupal\farm_kml\Encoder\Kml

File

modules/core/kml/src/Encoder/Kml.php, line 17

Namespace

Drupal\farm_kml\Encoder
View source
class Kml extends XmlEncoder {

  /**
   * {@inheritdoc}
   */
  protected static $format = [
    'geometry_kml',
  ];

  /**
   * {@inheritdoc}
   */
  public function encode($data, $format, array $context = []) {

    // Build XML document to encode.
    $xml = [
      '@xmlns' => 'http://earth.google.com/kml/2.1',
      'Document' => [
        'Placemark' => $data,
      ],
    ];

    // Provide default context for the KML format.
    $xml_context = [
      'xml_version' => '1.0',
      'xml_encoding' => 'UTF-8',
      'xml_format_output' => TRUE,
      'xml_root_node_name' => 'kml',
    ] + $context;

    // Encode using the XML encoder.
    return $this
      ->getBaseEncoder()
      ->encode($xml, 'xml', $xml_context);
  }

  /**
   * {@inheritdoc}
   */
  public function decode($data, $format, array $context = []) {

    // Start an array of decoded placemark data.
    $decoded_placemarks = [];

    // Build an XML object.
    $xml = simplexml_load_string($data);

    // If empty, or failed to parse, bail.
    if (empty($xml)) {
      return $decoded_placemarks;
    }

    // Determine the root. Sometimes it is "Document".
    $root = $xml;
    if (isset($xml->Document)) {
      $root = $xml->Document;
    }

    // Start an array of placemarks to decode.
    $placemarks = [];

    // If the KML is organized into folders, iterate through them.
    if (isset($root->Folder)) {
      foreach ($root->Folder as $folder) {
        if (isset($folder->Placemark)) {
          foreach ($folder->Placemark as $placemark) {
            $placemarks[] = $placemark;
          }
        }
      }
    }

    // Also check the root for any placemarks.
    if (isset($root->Placemark)) {
      foreach ($root->Placemark as $placemark) {
        $placemarks[] = $placemark;
      }
    }

    // Decode each placemark into an array.
    // Include the individual placemark as an XML string.
    foreach ($placemarks as $placemark) {
      $geometry = (array) $placemark;
      $geometry['xml'] = $placemark
        ->asXML();
      $decoded_placemarks[] = $geometry;
    }
    return $decoded_placemarks;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
Kml::$format protected static property The formats that this Encoder supports. Overrides XmlEncoder::$format
Kml::decode public function Overrides XmlEncoder::decode
Kml::encode public function Overrides XmlEncoder::encode
XmlEncoder::$baseEncoder protected property An instance of the Symfony XmlEncoder to perform the actual encoding.
XmlEncoder::getBaseEncoder public function Gets the base encoder instance.
XmlEncoder::setBaseEncoder public function Sets the base encoder instance.
XmlEncoder::supportsDecoding public function
XmlEncoder::supportsEncoding public function