KerioApiException.php 2.4 KB

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