OCSResponse.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Bernhard Posselt <dev@bernhard-posselt.com>
  6. * @author Lukas Reschke <lukas@statuscode.ch>
  7. * @author Morris Jobke <hey@morrisjobke.de>
  8. * @author Roeland Jago Douma <roeland@famdouma.nl>
  9. * @author Thomas Müller <thomas.mueller@tmit.eu>
  10. *
  11. * @license AGPL-3.0
  12. *
  13. * This code is free software: you can redistribute it and/or modify
  14. * it under the terms of the GNU Affero General Public License, version 3,
  15. * as published by the Free Software Foundation.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU Affero General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Affero General Public License, version 3,
  23. * along with this program. If not, see <http://www.gnu.org/licenses/>
  24. *
  25. */
  26. /**
  27. * Public interface of ownCloud for apps to use.
  28. * AppFramework\HTTP\JSONResponse class
  29. */
  30. namespace OCP\AppFramework\Http;
  31. /**
  32. * A renderer for OCS responses
  33. * @since 8.1.0
  34. * @deprecated 9.2.0 To implement an OCS endpoint extend the OCSController
  35. */
  36. class OCSResponse extends Response {
  37. private $data;
  38. private $format;
  39. private $statuscode;
  40. private $message;
  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 int $statuscode
  47. * @param string $message
  48. * @param array $data
  49. * @param int|string $itemscount
  50. * @param int|string $itemsperpage
  51. * @since 8.1.0
  52. * @deprecated 9.2.0 To implement an OCS endpoint extend the OCSController
  53. */
  54. public function __construct($format, $statuscode, $message,
  55. $data=[], $itemscount='',
  56. $itemsperpage='') {
  57. $this->format = $format;
  58. $this->statuscode = $statuscode;
  59. $this->message = $message;
  60. $this->data = $data;
  61. $this->itemscount = $itemscount;
  62. $this->itemsperpage = $itemsperpage;
  63. // set the correct header based on the format parameter
  64. if ($format === 'json') {
  65. $this->addHeader(
  66. 'Content-Type', 'application/json; charset=utf-8'
  67. );
  68. } else {
  69. $this->addHeader(
  70. 'Content-Type', 'application/xml; charset=utf-8'
  71. );
  72. }
  73. }
  74. /**
  75. * @return string
  76. * @since 8.1.0
  77. * @deprecated 9.2.0 To implement an OCS endpoint extend the OCSController
  78. * @suppress PhanDeprecatedClass
  79. */
  80. public function render() {
  81. $r = new \OC\OCS\Result($this->data, $this->statuscode, $this->message);
  82. $r->setTotalItems($this->itemscount);
  83. $r->setItemsPerPage($this->itemsperpage);
  84. return \OC_API::renderResult($this->format, $r->getMeta(), $r->getData());
  85. }
  86. }