Remove duplicate and unused code
This commit is contained in:
parent
cd1bfd7eb6
commit
649cc2fa89
5 changed files with 42 additions and 175 deletions
|
@ -357,24 +357,9 @@ class OC_API {
|
|||
}
|
||||
}
|
||||
|
||||
$response = array(
|
||||
'ocs' => array(
|
||||
'meta' => $result->getMeta(),
|
||||
'data' => $result->getData(),
|
||||
),
|
||||
);
|
||||
if ($format == 'json') {
|
||||
OC_JSON::encodedPrint($response);
|
||||
} else if ($format == 'xml') {
|
||||
header('Content-type: text/xml; charset=UTF-8');
|
||||
$writer = new XMLWriter();
|
||||
$writer->openMemory();
|
||||
$writer->setIndent( true );
|
||||
$writer->startDocument();
|
||||
self::toXML($response, $writer);
|
||||
$writer->endDocument();
|
||||
echo $writer->outputMemory(true);
|
||||
}
|
||||
self::setContentType($format);
|
||||
$body = self::renderResult($result, $format);
|
||||
echo $body;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -411,8 +396,8 @@ class OC_API {
|
|||
/**
|
||||
* Based on the requested format the response content type is set
|
||||
*/
|
||||
public static function setContentType() {
|
||||
$format = self::requestedFormat();
|
||||
public static function setContentType($format = null) {
|
||||
$format = is_null($format) ? self::requestedFormat() : $format;
|
||||
if ($format === 'xml') {
|
||||
header('Content-type: text/xml; charset=UTF-8');
|
||||
return;
|
||||
|
@ -464,4 +449,29 @@ class OC_API {
|
|||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param OC_OCS_Result $result
|
||||
* @param string $format
|
||||
* @return string
|
||||
*/
|
||||
public static function renderResult($result, $format) {
|
||||
$response = array(
|
||||
'ocs' => array(
|
||||
'meta' => $result->getMeta(),
|
||||
'data' => $result->getData(),
|
||||
),
|
||||
);
|
||||
if ($format == 'json') {
|
||||
return OC_JSON::encode($response);
|
||||
}
|
||||
|
||||
$writer = new XMLWriter();
|
||||
$writer->openMemory();
|
||||
$writer->setIndent(true);
|
||||
$writer->startDocument();
|
||||
self::toXML($response, $writer);
|
||||
$writer->endDocument();
|
||||
return $writer->outputMemory(true);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -99,130 +99,4 @@ class OC_OCS {
|
|||
if(isset($_POST)) foreach($_POST as $key=>$value) $txt.='post parameter: '.$key.'->'.$value."\n";
|
||||
return($txt);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* generates the xml or json response for the API call from an multidimenional data array.
|
||||
* @param string $format
|
||||
* @param string $status
|
||||
* @param string $statuscode
|
||||
* @param string $message
|
||||
* @param array $data
|
||||
* @param string $tag
|
||||
* @param string $tagattribute
|
||||
* @param int $dimension
|
||||
* @param int|string $itemscount
|
||||
* @param int|string $itemsperpage
|
||||
* @return string xml/json
|
||||
*/
|
||||
public static function generateXml($format, $status, $statuscode,
|
||||
$message, $data=array(), $tag='', $tagattribute='', $dimension=-1, $itemscount='', $itemsperpage='') {
|
||||
if($format=='json') {
|
||||
$json=array();
|
||||
$json['status']=$status;
|
||||
$json['statuscode']=$statuscode;
|
||||
$json['message']=$message;
|
||||
$json['totalitems']=$itemscount;
|
||||
$json['itemsperpage']=$itemsperpage;
|
||||
$json['data']=$data;
|
||||
return(json_encode($json));
|
||||
}else{
|
||||
$txt='';
|
||||
$writer = xmlwriter_open_memory();
|
||||
xmlwriter_set_indent( $writer, 2 );
|
||||
xmlwriter_start_document($writer );
|
||||
xmlwriter_start_element($writer, 'ocs');
|
||||
xmlwriter_start_element($writer, 'meta');
|
||||
xmlwriter_write_element($writer, 'status', $status);
|
||||
xmlwriter_write_element($writer, 'statuscode', $statuscode);
|
||||
xmlwriter_write_element($writer, 'message', $message);
|
||||
if($itemscount<>'') xmlwriter_write_element($writer, 'totalitems', $itemscount);
|
||||
if(!empty($itemsperpage)) xmlwriter_write_element($writer, 'itemsperpage', $itemsperpage);
|
||||
xmlwriter_end_element($writer);
|
||||
if($dimension=='0') {
|
||||
// 0 dimensions
|
||||
xmlwriter_write_element($writer, 'data', $data);
|
||||
|
||||
}elseif($dimension=='1') {
|
||||
xmlwriter_start_element($writer, 'data');
|
||||
foreach($data as $key=>$entry) {
|
||||
xmlwriter_write_element($writer, $key, $entry);
|
||||
}
|
||||
xmlwriter_end_element($writer);
|
||||
|
||||
}elseif($dimension=='2') {
|
||||
xmlwriter_start_element($writer, 'data');
|
||||
foreach($data as $entry) {
|
||||
xmlwriter_start_element($writer, $tag);
|
||||
if(!empty($tagattribute)) {
|
||||
xmlwriter_write_attribute($writer, 'details', $tagattribute);
|
||||
}
|
||||
foreach($entry as $key=>$value) {
|
||||
if(is_array($value)) {
|
||||
foreach($value as $k=>$v) {
|
||||
xmlwriter_write_element($writer, $k, $v);
|
||||
}
|
||||
} else {
|
||||
xmlwriter_write_element($writer, $key, $value);
|
||||
}
|
||||
}
|
||||
xmlwriter_end_element($writer);
|
||||
}
|
||||
xmlwriter_end_element($writer);
|
||||
|
||||
}elseif($dimension=='3') {
|
||||
xmlwriter_start_element($writer, 'data');
|
||||
foreach($data as $entrykey=>$entry) {
|
||||
xmlwriter_start_element($writer, $tag);
|
||||
if(!empty($tagattribute)) {
|
||||
xmlwriter_write_attribute($writer, 'details', $tagattribute);
|
||||
}
|
||||
foreach($entry as $key=>$value) {
|
||||
if(is_array($value)) {
|
||||
xmlwriter_start_element($writer, $entrykey);
|
||||
foreach($value as $k=>$v) {
|
||||
xmlwriter_write_element($writer, $k, $v);
|
||||
}
|
||||
xmlwriter_end_element($writer);
|
||||
} else {
|
||||
xmlwriter_write_element($writer, $key, $value);
|
||||
}
|
||||
}
|
||||
xmlwriter_end_element($writer);
|
||||
}
|
||||
xmlwriter_end_element($writer);
|
||||
}elseif($dimension=='dynamic') {
|
||||
xmlwriter_start_element($writer, 'data');
|
||||
OC_OCS::toxml($writer, $data, 'comment');
|
||||
xmlwriter_end_element($writer);
|
||||
}
|
||||
|
||||
xmlwriter_end_element($writer);
|
||||
|
||||
xmlwriter_end_document( $writer );
|
||||
$txt.=xmlwriter_output_memory( $writer );
|
||||
unset($writer);
|
||||
return($txt);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param resource $writer
|
||||
* @param array $data
|
||||
* @param string $node
|
||||
*/
|
||||
public static function toXml($writer, $data, $node) {
|
||||
foreach($data as $key => $value) {
|
||||
if (is_numeric($key)) {
|
||||
$key = $node;
|
||||
}
|
||||
if (is_array($value)) {
|
||||
xmlwriter_start_element($writer, $key);
|
||||
OC_OCS::toxml($writer, $value, $node);
|
||||
xmlwriter_end_element($writer);
|
||||
}else{
|
||||
xmlwriter_write_element($writer, $key, $value);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -41,38 +41,26 @@ class OCSResponse extends Response {
|
|||
private $format;
|
||||
private $statuscode;
|
||||
private $message;
|
||||
private $tag;
|
||||
private $tagattribute;
|
||||
private $dimension;
|
||||
private $itemscount;
|
||||
private $itemsperpage;
|
||||
|
||||
/**
|
||||
* generates the xml or json response for the API call from an multidimenional data array.
|
||||
* @param string $format
|
||||
* @param string $status
|
||||
* @param string $statuscode
|
||||
* @param int $statuscode
|
||||
* @param string $message
|
||||
* @param array $data
|
||||
* @param string $tag
|
||||
* @param string $tagattribute
|
||||
* @param int $dimension
|
||||
* @param int|string $itemscount
|
||||
* @param int|string $itemsperpage
|
||||
* @since 8.1.0
|
||||
*/
|
||||
public function __construct($format, $status, $statuscode, $message,
|
||||
$data=[], $tag='', $tagattribute='',
|
||||
$dimension=-1, $itemscount='',
|
||||
public function __construct($format, $statuscode, $message,
|
||||
$data=[], $itemscount='',
|
||||
$itemsperpage='') {
|
||||
$this->format = $format;
|
||||
$this->setStatus($status);
|
||||
$this->statuscode = $statuscode;
|
||||
$this->message = $message;
|
||||
$this->data = $data;
|
||||
$this->tag = $tag;
|
||||
$this->tagattribute = $tagattribute;
|
||||
$this->dimension = $dimension;
|
||||
$this->itemscount = $itemscount;
|
||||
$this->itemsperpage = $itemsperpage;
|
||||
|
||||
|
@ -93,11 +81,11 @@ class OCSResponse extends Response {
|
|||
* @since 8.1.0
|
||||
*/
|
||||
public function render() {
|
||||
return OC_OCS::generateXml(
|
||||
$this->format, $this->getStatus(), $this->statuscode, $this->message,
|
||||
$this->data, $this->tag, $this->tagattribute, $this->dimension,
|
||||
$this->itemscount, $this->itemsperpage
|
||||
);
|
||||
$r = new \OC_OCS_Result($this->data, $this->statuscode, $this->message);
|
||||
$r->setTotalItems($this->itemscount);
|
||||
$r->setItemsPerPage($this->itemsperpage);
|
||||
|
||||
return \OC_API::renderResult($r, $this->format);
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -42,7 +42,7 @@ abstract class OCSController extends ApiController {
|
|||
* constructor of the controller
|
||||
* @param string $appName the name of the app
|
||||
* @param IRequest $request an instance of the request
|
||||
* @param string $corsMethods comma seperated string of HTTP verbs which
|
||||
* @param string $corsMethods comma separated string of HTTP verbs which
|
||||
* should be allowed for websites or webapps when calling your API, defaults to
|
||||
* 'PUT, POST, GET, DELETE, PATCH'
|
||||
* @param string $corsAllowedHeaders comma seperated string of HTTP headers
|
||||
|
@ -84,9 +84,6 @@ abstract class OCSController extends ApiController {
|
|||
'statuscode' => 100,
|
||||
'message' => 'OK',
|
||||
'data' => [],
|
||||
'tag' => '',
|
||||
'tagattribute' => '',
|
||||
'dimension' => 'dynamic',
|
||||
'itemscount' => '',
|
||||
'itemsperpage' => ''
|
||||
];
|
||||
|
@ -97,8 +94,7 @@ abstract class OCSController extends ApiController {
|
|||
|
||||
return new OCSResponse(
|
||||
$format, $params['status'], $params['statuscode'],
|
||||
$params['message'], $params['data'], $params['tag'],
|
||||
$params['tagattribute'], $params['dimension'],
|
||||
$params['message'], $params['data'],
|
||||
$params['itemscount'], $params['itemsperpage']
|
||||
);
|
||||
}
|
||||
|
|
|
@ -47,14 +47,13 @@ class OCSResponseTest extends \Test\TestCase {
|
|||
|
||||
public function testRender() {
|
||||
$response = new OCSResponse(
|
||||
'xml', 'status', 2, 'message', ['test' => 'hi'], 'tag', 'abc',
|
||||
'dynamic', 3, 4
|
||||
'xml', 2, 'message', ['test' => 'hi'], 3, 4
|
||||
);
|
||||
$out = $response->render();
|
||||
$expected = "<?xml version=\"1.0\"?>\n" .
|
||||
"<ocs>\n" .
|
||||
" <meta>\n" .
|
||||
" <status>status</status>\n" .
|
||||
" <status>failure</status>\n" .
|
||||
" <statuscode>2</statuscode>\n" .
|
||||
" <message>message</message>\n" .
|
||||
" <totalitems>3</totalitems>\n" .
|
||||
|
|
Loading…
Reference in a new issue