ocsresponse.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php
  2. /**
  3. * @author Bernhard Posselt <dev@bernhard-posselt.com>
  4. * @author Morris Jobke <hey@morrisjobke.de>
  5. *
  6. * @copyright Copyright (c) 2015, ownCloud, Inc.
  7. * @license AGPL-3.0
  8. *
  9. * This code is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License, version 3,
  11. * as published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU Affero General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public License, version 3,
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>
  20. *
  21. */
  22. /**
  23. * Public interface of ownCloud for apps to use.
  24. * AppFramework\HTTP\JSONResponse class
  25. */
  26. namespace OCP\AppFramework\Http;
  27. use OCP\AppFramework\Http;
  28. use OC_OCS;
  29. /**
  30. * A renderer for OCS responses
  31. * @since 8.1.0
  32. */
  33. class OCSResponse extends Response {
  34. private $data;
  35. private $format;
  36. private $statuscode;
  37. private $message;
  38. private $tag;
  39. private $tagattribute;
  40. private $dimension;
  41. private $itemscount;
  42. private $itemsperpage;
  43. /**
  44. * generates the xml or json response for the API call from an multidimenional data array.
  45. * @param string $format
  46. * @param string $status
  47. * @param string $statuscode
  48. * @param string $message
  49. * @param array $data
  50. * @param string $tag
  51. * @param string $tagattribute
  52. * @param int $dimension
  53. * @param int|string $itemscount
  54. * @param int|string $itemsperpage
  55. * @since 8.1.0
  56. */
  57. public function __construct($format, $status, $statuscode, $message,
  58. $data=[], $tag='', $tagattribute='',
  59. $dimension=-1, $itemscount='',
  60. $itemsperpage='') {
  61. $this->format = $format;
  62. $this->setStatus($status);
  63. $this->statuscode = $statuscode;
  64. $this->message = $message;
  65. $this->data = $data;
  66. $this->tag = $tag;
  67. $this->tagattribute = $tagattribute;
  68. $this->dimension = $dimension;
  69. $this->itemscount = $itemscount;
  70. $this->itemsperpage = $itemsperpage;
  71. // set the correct header based on the format parameter
  72. if ($format === 'json') {
  73. $this->addHeader(
  74. 'Content-Type', 'application/json; charset=utf-8'
  75. );
  76. } else {
  77. $this->addHeader(
  78. 'Content-Type', 'application/xml; charset=utf-8'
  79. );
  80. }
  81. }
  82. /**
  83. * @return string
  84. * @since 8.1.0
  85. */
  86. public function render() {
  87. return OC_OCS::generateXml(
  88. $this->format, $this->getStatus(), $this->statuscode, $this->message,
  89. $this->data, $this->tag, $this->tagattribute, $this->dimension,
  90. $this->itemscount, $this->itemsperpage
  91. );
  92. }
  93. }