Response.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. /**
  3. * Web API response.
  4. *
  5. * Copyright © Magento, Inc. All rights reserved.
  6. * See COPYING.txt for license details.
  7. */
  8. namespace Magento\Framework\Webapi;
  9. class Response extends \Magento\Framework\HTTP\PhpEnvironment\Response implements
  10. \Magento\Framework\App\Response\HttpInterface
  11. {
  12. /**
  13. * Character set which must be used in response.
  14. */
  15. const RESPONSE_CHARSET = 'utf-8';
  16. /**#@+
  17. * Default message types.
  18. */
  19. const MESSAGE_TYPE_SUCCESS = 'success';
  20. const MESSAGE_TYPE_ERROR = 'error';
  21. const MESSAGE_TYPE_WARNING = 'warning';
  22. /**#@- */
  23. /**#@+
  24. * Success HTTP response codes.
  25. */
  26. const HTTP_OK = 200;
  27. /**#@-*/
  28. /**#@-*/
  29. protected $_messages = [];
  30. /**
  31. * Set header appropriate to specified MIME type.
  32. *
  33. * @param string $mimeType MIME type
  34. * @return $this
  35. */
  36. public function setMimeType($mimeType)
  37. {
  38. return $this->setHeader('Content-Type', "{$mimeType}; charset=" . self::RESPONSE_CHARSET, true);
  39. }
  40. /**
  41. * Add message to response.
  42. *
  43. * @param string $message
  44. * @param string $code
  45. * @param array $params
  46. * @param string $type
  47. * @return $this
  48. */
  49. public function addMessage($message, $code, $params = [], $type = self::MESSAGE_TYPE_ERROR)
  50. {
  51. $params['message'] = $message;
  52. $params['code'] = $code;
  53. $this->_messages[$type][] = $params;
  54. return $this;
  55. }
  56. /**
  57. * Has messages.
  58. *
  59. * @return bool
  60. */
  61. public function hasMessages()
  62. {
  63. return (bool)count($this->_messages) > 0;
  64. }
  65. /**
  66. * Return messages.
  67. *
  68. * @return array
  69. */
  70. public function getMessages()
  71. {
  72. return $this->_messages;
  73. }
  74. /**
  75. * Clear messages.
  76. *
  77. * @return $this
  78. */
  79. public function clearMessages()
  80. {
  81. $this->_messages = [];
  82. return $this;
  83. }
  84. }