OCSResponse.php 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. parent::__construct();
  58. $this->format = $format;
  59. $this->statuscode = $statuscode;
  60. $this->message = $message;
  61. $this->data = $data;
  62. $this->itemscount = $itemscount;
  63. $this->itemsperpage = $itemsperpage;
  64. // set the correct header based on the format parameter
  65. if ($format === 'json') {
  66. $this->addHeader(
  67. 'Content-Type', 'application/json; charset=utf-8'
  68. );
  69. } else {
  70. $this->addHeader(
  71. 'Content-Type', 'application/xml; charset=utf-8'
  72. );
  73. }
  74. }
  75. /**
  76. * @return string
  77. * @since 8.1.0
  78. * @deprecated 9.2.0 To implement an OCS endpoint extend the OCSController
  79. * @suppress PhanDeprecatedClass
  80. */
  81. public function render() {
  82. $r = new \OC\OCS\Result($this->data, $this->statuscode, $this->message);
  83. $r->setTotalItems($this->itemscount);
  84. $r->setItemsPerPage($this->itemsperpage);
  85. return \OC_API::renderResult($this->format, $r->getMeta(), $r->getData());
  86. }
  87. }