KerioApiException.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. namespace ThurData\Servers\KerioEmail\Api;
  3. use Exception;
  4. /**
  5. * This file is part of the kerio-api-php.
  6. *
  7. * Copyright (c) Kerio Technologies s.r.o.
  8. *
  9. * For the full copyright and license information, please view
  10. * the file license.txt that was distributed with this source code
  11. * or visit Developer Zone. (http://www.kerio.com/developers)
  12. *
  13. * Do not modify this source code.
  14. * Any changes may be overwritten by a new version.
  15. */
  16. /**
  17. * Kerio API Exception Class.
  18. *
  19. * This class extends Exception class to provide CSS-based error message formating.
  20. *
  21. * @copyright Copyright &copy; 2012-2012 Kerio Technologies s.r.o.
  22. * @license http://www.kerio.com/developers/license/sdk-agreement
  23. * @version 1.4.0.234
  24. */
  25. class KerioApiException extends Exception {
  26. /**
  27. * Positional parameters
  28. * @var array
  29. */
  30. private $positionalParameters = array();
  31. /**
  32. * Error code
  33. * @var integer
  34. */
  35. protected $code;
  36. /**
  37. * Error message
  38. * @var string
  39. */
  40. protected $message = '';
  41. /**
  42. * Request message
  43. * @var string
  44. */
  45. protected $request = '';
  46. /**
  47. * Response message
  48. * @var string
  49. */
  50. protected $response = '';
  51. /**
  52. * Exception contructor.
  53. *
  54. * @param string Message to display
  55. * @param mixed Can be integer or string
  56. * @param array Positional parameters in message
  57. * @return void
  58. */
  59. public function __construct($message, $code = '', $positionalParameters = '', $request = '', $response = '') {
  60. $this->message = $message;
  61. if (is_int($code) || is_string($code)) {
  62. $this->code = $code;
  63. }
  64. if (is_array($positionalParameters)) {
  65. $this->positionalParameters = $positionalParameters;
  66. $this->setPositionalParameterToString();
  67. }
  68. if (is_string($request)) {
  69. $this->request = $request;
  70. }
  71. if (is_string($response)) {
  72. $this->response = $response;
  73. }
  74. }
  75. /**
  76. * Get request data.
  77. *
  78. * @return string JSON request
  79. */
  80. public function getRequest() {
  81. return $this->request;
  82. }
  83. /**
  84. * Get response data.
  85. *
  86. * @return string JSON response
  87. */
  88. public function getResponse() {
  89. return $this->response;
  90. }
  91. /**
  92. * Replace positional parameter with a string
  93. *
  94. * @return void
  95. */
  96. private function setPositionalParameterToString() {
  97. if (preg_match_all('/%\d/', $this->message, $matches)) {
  98. /* Found positional parameters */
  99. $index = 0;
  100. foreach ($matches[0] as $occurence) {
  101. $replaceWith = $this->positionalParameters[$index];
  102. $this->message = str_replace($occurence, $replaceWith, $this->message);
  103. $index++;
  104. }
  105. }
  106. }
  107. }