You are here

class FormatterHalXml in RESTful 7.2

Class FormatterHalXml @package Drupal\restful\Plugin\formatter

Plugin annotation


@Formatter(
  id = "hal_xml",
  label = "HAL+XML",
  description = "Output in using the HAL conventions and XML format.",
  curie = {
    "name": "hal",
    "path": "doc/rels",
    "template": "/{rel}",
  },
)

Hierarchy

Expanded class hierarchy of FormatterHalXml

File

modules/restful_example/src/Plugin/formatter/FormatterHalXml.php, line 28
Contains \Drupal\restful\Plugin\formatter\FormatterHalJson.

Namespace

Drupal\restful_example\Plugin\formatter
View source
class FormatterHalXml extends FormatterHalJson implements FormatterInterface {

  /**
   * Content Type
   *
   * @var string
   */
  protected $contentType = 'application/xml; charset=utf-8';

  /**
   * {@inheritdoc}
   */
  public function render(array $structured_data) {
    return $this
      ->arrayToXML($structured_data, new \SimpleXMLElement('<api/>'))
      ->asXML();
  }

  /**
   * Converts the input array into an XML formatted string.
   *
   * @param array $data
   *   The input array.
   * @param \SimpleXMLElement $xml
   *   The object that will perform the conversion.
   *
   * @return \SimpleXMLElement
   */
  protected function arrayToXML(array $data, \SimpleXMLElement $xml) {
    foreach ($data as $key => $value) {
      if (is_array($value)) {
        if (!is_numeric($key)) {
          $subnode = $xml
            ->addChild("{$key}");
          $this
            ->arrayToXML($value, $subnode);
        }
        else {
          $subnode = $xml
            ->addChild("item{$key}");
          $this
            ->arrayToXML($value, $subnode);
        }
      }
      else {
        $xml
          ->addChild("{$key}", htmlspecialchars("{$value}"));
      }
    }
    return $xml;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ConfigurablePluginTrait::$instanceConfiguration protected property Plugin instance configuration.
ConfigurablePluginTrait::calculateDependencies public function
ConfigurablePluginTrait::defaultConfiguration public function 1
ConfigurablePluginTrait::getConfiguration public function
ConfigurablePluginTrait::setConfiguration public function
Formatter::$resource protected property The resource handler containing more info about the request.
Formatter::cacheFragments protected static function Gets a cache fragments based on the data to be rendered.
Formatter::calculateItemsPerPage protected function Helper function that calculates the number of items per page.
Formatter::createCacheController protected function Gets a cache controller based on the data to be rendered.
Formatter::format public function Formats the un-structured data into the output format. Overrides FormatterInterface::format
Formatter::getCachedData protected function Gets the cached computed value for the fields to be rendered.
Formatter::getCacheHash protected function Gets the cached computed value for the fields to be rendered.
Formatter::getResource public function Gets the underlying resource. Overrides FormatterInterface::getResource
Formatter::isCacheEnabled protected function Checks if the passed in data to be rendered can be cached.
Formatter::isIterable protected static function Helper function to know if a variable is iterable or not.
Formatter::limitFields protected function Returns only the allowed fields by filtering out the other ones.
Formatter::parseBody public function Parses the body string into the common format. Overrides FormatterInterface::parseBody 2
Formatter::setCachedData protected function Gets the cached computed value for the fields to be rendered.
Formatter::setResource public function Sets the underlying resource. Overrides FormatterInterface::setResource
Formatter::unprefixInputOptions protected static function Given a prefix, return the allowed fields that apply removing the prefix.
Formatter::__construct public function
FormatterHalJson::addHateoas protected function Add HATEOAS links to list of item.
FormatterHalJson::CURIE_SEPARATOR constant
FormatterHalJson::extractFieldValues protected function Extracts the actual values from the resource fields.
FormatterHalJson::getContentTypeHeader public function Returns the content type for the selected output format. Overrides Formatter::getContentTypeHeader
FormatterHalJson::getCurie protected function Checks if the current plugin has a defined curie.
FormatterHalJson::prepare public function Massages the raw data to create a structured array to pass to the renderer. Overrides FormatterInterface::prepare
FormatterHalJson::withCurie protected function Prefix a property name with the curie, if present.
FormatterHalXml::$contentType protected property Content Type Overrides FormatterHalJson::$contentType
FormatterHalXml::arrayToXML protected function Converts the input array into an XML formatted string.
FormatterHalXml::render public function Renders an array in the selected format. Overrides FormatterHalJson::render