Source of file XmlSerializer.php
Size: 1,557 Bytes - Last Modified: 2021-12-23T10:07:36+00:00
/var/www/docs.ssmods.com/process/src/code/serializers/XmlSerializer.php
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162 | <?php namespace Ntb\RestAPI; /** * Serializer for xml. * @author Christian Blank <c.blank@notthatbad.net> */ class XmlSerializer extends \SS_Object implements IRestSerializer { /** * @config */ private static $is_active = true; /** * The content type. * @var string */ private $contentType = "application/xml"; /** * Serializes the given data into a xml string. * * @param array $data the data that should be serialized * @return string a xml formatted string */ public function serialize($data) { $xml = new \SimpleXMLElement('<result/>'); $this->toXml($xml, $data); return $xml->asXML(); } public function contentType() { return $this->contentType; } private function toXml(\SimpleXMLElement $object, array $data) { foreach( $data as $key => $value ) { if(is_array($value) ) { if( is_numeric($key) ){ $key = 'item'.$key; //dealing with <0/>..<n/> issues } $subnode = $object->addChild($key); $this->toXml($subnode, $value); } else { $object->addChild("$key",htmlspecialchars("$value")); } } } /** * Indicates if the serializer is active. * Serializers can be deactivated to use another implementation for the same mime type. * * @return boolean */ public function active() { return $this->config()->get('is_active'); } } |