* @package WB * @subpackage base */ WBClass::load( 'WBRest_Serializer' ); /** * REST Serializer / Unserializer XML * * @version 0.3.0 * @package WB * @subpackage base */ class WBRest_Serializer_Xml extends WBRest_Serializer { /** * format string of server error * * @var string */ protected $errorFrame = ' error rest:%d %s '; /** * Unserialize Request * * Convert Request body to data array * * @param string $body * @param string name of data element, if any * @return array $data */ public function unserialize($body, $dataElement = 'data') { // prepare XML_Userializer $params = array( 'whitespace' => 'keep', 'complexType' => 'array', 'parseAttributes' => false, 'attributesArray' => '_attributes', 'forceEnum' => array('item') ); WBClass::load('XML_Unserializer'); $unser = new XML_Unserializer($params); $res = $unser->unserialize($body, false); if( $res instanceof PEAR_Error ) { WBClass::load( 'WBException_Pear' ); throw new WBException_Pear( $res->getMessage(), 1, __CLASS__ ); } $all = $unser->getUnserializedData(); return $this->extractUnserializedElement($all, $dataElement); } /** * Serialize data * * Convert array data for response * * @param array $data * @return string $body */ public function serialize($data) { WBClass::load('XML_Serializer'); $opts = array( XML_SERIALIZER_OPTION_ROOT_NAME => 'response', XML_SERIALIZER_OPTION_ROOT_ATTRIBS => array( 'version' => '1.0' ), XML_SERIALIZER_OPTION_LINEBREAKS => "\n", XML_SERIALIZER_OPTION_INDENT => '', XML_SERIALIZER_OPTION_DEFAULT_TAG => 'item', XML_SERIALIZER_OPTION_XML_DECL_ENABLED => true, XML_SERIALIZER_OPTION_XML_ENCODING => 'utf-8', /* XML_SERIALIZER_OPTION_DOCTYPE_ENABLED => true, XML_SERIALIZER_OPTION_DOCTYPE => array( 'uri' => 'http://xml.wombat.exit0.net/rest', 'id' => 'Wombat REST 1.0' ) */ ); // make output more human readable if ($this->debug) { $opts[XML_SERIALIZER_OPTION_INDENT] = ' '; } $this->ser = new XML_Serializer($opts); $result = $this->ser->serialize($data); if (PEAR::isError($result)) { WBClass::load( 'WBException_Pear' ); throw new WBException_Pear( $result->getMessage(), 1, __CLASS__ ); } return $this->ser->getSerializedData(); } }