You are here

class RestfulFormatterHalXml in RESTful 7

@file Contains RestfulFormatterHalJson.

Hierarchy

Expanded class hierarchy of RestfulFormatterHalXml

1 string reference to 'RestfulFormatterHalXml'
hal_xml.inc in modules/restful_example/plugins/formatter/hal_xml.inc

File

modules/restful_example/plugins/formatter/RestfulFormatterHalXml.class.php, line 8
Contains RestfulFormatterHalJson.

View source
class RestfulFormatterHalXml extends \RestfulFormatterHalJson implements \RestfulFormatterInterface {

  /**
   * 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
RestfulFormatterBase::$handler protected property The entity handler containing more info about the request.
RestfulFormatterBase::format public function Formats the un-structured data into the output format. Overrides RestfulFormatterInterface::format
RestfulFormatterBase::__construct public function Generic constructor. Overrides RestfulPluginBase::__construct
RestfulFormatterHalJson::addHateoas protected function Add HATEOAS links to list of item.
RestfulFormatterHalJson::addHateoasRow protected function Add Hateoas to a single row.
RestfulFormatterHalJson::getContentTypeHeader public function Returns the content type for the selected output format. Overrides RestfulFormatterBase::getContentTypeHeader
RestfulFormatterHalJson::getCurie protected function Checks if the current plugin has a defined curie.
RestfulFormatterHalJson::moveReferencesToEmbeds protected function Move the fields referencing other resources to the _embed key.
RestfulFormatterHalJson::prepare public function Massages the raw data to create a structured array to pass to the renderer. Overrides RestfulFormatterInterface::prepare
RestfulFormatterHalJson::prepareRow public function Massage the data of a single row.
RestfulFormatterHalJson::withCurie protected function Prefix a property name with the curie, if present.
RestfulFormatterHalXml::$contentType protected property Content Type Overrides RestfulFormatterHalJson::$contentType
RestfulFormatterHalXml::arrayToXML protected function Converts the input array into an XML formatted string.
RestfulFormatterHalXml::render public function Renders an array in the selected format. Overrides RestfulFormatterHalJson::render
RestfulPluginBase::$plugin protected property The plugin definition array.
RestfulPluginBase::getPlugin public function Gets information about the restful plugin. Overrides RestfulPluginInterface::getPlugin
RestfulPluginBase::getPluginKey public function Gets information about the restful plugin key. Overrides RestfulPluginInterface::getPluginKey
RestfulPluginBase::setPlugin public function Sets information about the restful plugin. Overrides RestfulPluginInterface::setPlugin
RestfulPluginBase::setPluginKey public function Gets information about the restful plugin key. Overrides RestfulPluginInterface::setPluginKey