/** * Encode array structure into this parser's format type * * If Cpanel_Parser_JSON::EXPANDED_MODE was set via {@link setMode()} * then the returned string will have excessive whitespace and line breaking * characters * * NOTE: The return string is will be the JSON value of the original * server response, wrapped in the "<cpanelresult></cpanelresult>" tags as * that is what a raw LiveJSON response looks like. This is NOT XML! They * are simple markers/tags used by the Live response protocol. * * NOTE: unlike {@link parse()}, this method WILL throw an Exception If * encoding fails. There should be no ambiguity, if the response objects * "_response" container has invalid data, there is a problem (that needs * to be addressed by the script/application developer). * * @param Cpanel_Query_Object $obj Response object containing data to * encode * * @see Cpanel_Parser_Interface::encodeQueryObject() * * @return string JSON string * @throws Exception If $obj is an invalid response object * @throws Exception If encoding fails */ public function encodeQueryObject($obj) { if (!$obj instanceof Cpanel_Query_Object) { throw new Exception('Parser can only encode known query object'); } $arr = $obj->getResponse('array'); $jsonStr = ''; $msg = ''; try { $jsonStr = json_encode($arr); } catch (Exception $e) { $msg = ". " . $e->getMessage(); $msg .= '. Store data likely contains a resource' . ' or non-UTF8 string.'; } if (!empty($msg) || empty($jsonStr)) { throw new Exception('JSON encoding error for ' . __FUNCTION__ . $msg); } if ($this->mode == self::EXPANDED_MODE) { $jsonStr = $this->prettyPrint($jsonStr); } return "<cpanelresult>{$jsonStr}</cpanelresult>"; }
/** * Encode array structure into this parser's format type * * Encoding is only performed with DOMDocument. Returned XML string will * be formated with line breaking and whitespace characters. * * @param Cpanel_Query_Object $obj Response object containing data to * encode * * @see Cpanel_Parser_Interface::encodeQueryObject() * * @return string XML document * @throws Exception If $obj is an invalid response object */ public function encodeQueryObject($obj) { if (!$obj instanceof Cpanel_Query_Object) { throw new Exception('Parser can only encode known query object'); } $arr = $obj->getResponse('array'); $this->_dom = new DOMDocument('1.0'); $this->_dom->preserveWhiteSpace = false; $this->_dom->formatOutput = true; // TODO: add error detection for empty arr if (count($arr) > 1) { $root = $this->_dom->createElement('result'); $this->_dom->appendChild($root); $this->_recurse_node_build($arr, $root); } elseif (count($arr) == 1) { $this->_recurse_node_build($arr, $this->_dom); } $r = $this->_dom->saveXML(); unset($this->_dom); return $r; }