FormatterHalXml.php in RESTful 7.2
File
modules/restful_example/src/Plugin/formatter/FormatterHalXml.php
View source
<?php
namespace Drupal\restful_example\Plugin\formatter;
use Drupal\restful\Plugin\formatter\FormatterHalJson;
use Drupal\restful\Plugin\formatter\FormatterInterface;
class FormatterHalXml extends FormatterHalJson implements FormatterInterface {
protected $contentType = 'application/xml; charset=utf-8';
public function render(array $structured_data) {
return $this
->arrayToXML($structured_data, new \SimpleXMLElement('<api/>'))
->asXML();
}
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;
}
}
Classes
Name |
Description |
FormatterHalXml |
Class FormatterHalXml
@package Drupal\restful\Plugin\formatter |