KerioApiException.php 2.5 KB

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