AWSRoute53Response.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. <?php
  2. namespace MGModule\DNSManager2\mgLibs\custom\dns\submodules\AWSRoute53;
  3. use MGModule\DNSManager2\mgLibs\custom\dns\submodules\AWSRoute53 as awsRoute53;
  4. class AWSRoute53Response implements \MGModule\DNSManager2\mgLibs\custom\dns\submodules\AWSRoute53\AWSRoute53ResponseInterface
  5. {
  6. private $responseBody;
  7. private $parsedBody;
  8. private $responseType;
  9. private $responseMessage;
  10. public function prepareResponse($responseBody)
  11. {
  12. $this->responseBody = $responseBody;
  13. $this->parseResponse();
  14. return $this;
  15. }
  16. private function parseResponse()
  17. {
  18. $this->parsedBody = simplexml_load_string($this->responseBody);
  19. if(!$this->parsedBody)
  20. {
  21. $this->prepareTextResponse('error', 'Invalid Response');
  22. return;
  23. }
  24. $respErr = awsRoute53\AWSRoute53ResponseParseHelper::checkResponseForErrors($this->parsedBody);
  25. if($respErr)
  26. {
  27. $this->prepareTextResponse('error', $this->parseErrorMessage($respErr['message']));
  28. return;
  29. }
  30. $this->responseType = $this->responseType ? $this->responseType : 'success';
  31. }
  32. public function prepareTextResponse($type, $message)
  33. {
  34. $this->responseType = $type;
  35. $this->responseMessage = $message;
  36. }
  37. public function getResponseType()
  38. {
  39. return $this->responseType;
  40. }
  41. public function getResponseMessage()
  42. {
  43. return $this->responseMessage;
  44. }
  45. public function getRawResponseBody()
  46. {
  47. return $this->responseBody;
  48. }
  49. public function getParsedResponseBody()
  50. {
  51. return $this->parsedBody;
  52. }
  53. public function parseErrorMessage($message)
  54. {
  55. foreach(awsRoute53\AWSRoute53ResponseMessages::$messages as $key => $value)
  56. {
  57. $keys = array();
  58. $this->matchWordKeys($key, $keys);
  59. if(count($keys) === 0 && $message === $key)
  60. {
  61. return $value;
  62. }
  63. $searchPattern = $this->getSearchPattern($key, $keys);
  64. $match = preg_match($searchPattern, $message);
  65. if((int)$match === 1)
  66. {
  67. $tmpMessage = $this->replaceKeys($message, $key, $value, $keys);
  68. if($tmpMessage && $tmpMessage !== '')
  69. {
  70. return $tmpMessage;
  71. }
  72. }
  73. }
  74. return $message;
  75. }
  76. private function matchWordKeys($text, &$keys)
  77. {
  78. $keyWordList = explode(' ', $text);
  79. foreach($keyWordList as $word)
  80. {
  81. if((substr($word, -1) === '}' && substr($word, 0, 1) === '{') || (substr($word, -2) === '},' && substr($word, 0, 1) === '{'))
  82. {
  83. $tmpWord = substr(trim($word, ','), 1, -1);
  84. if(!in_array($tmpWord, $keys))
  85. {
  86. $keys[] = $tmpWord;
  87. }
  88. }
  89. }
  90. }
  91. private function getSearchPattern($text, $keys)
  92. {
  93. foreach ($keys as $keyWord)
  94. {
  95. $text = str_replace('{'.$keyWord.'}', ".+", $text);
  96. }
  97. return "/".$text."/";
  98. }
  99. private function replaceKeys($message, $key, $value, $keys)
  100. {
  101. $toChange = $this->getToChangeList($keys, $key);
  102. while(count($toChange) > 0)
  103. {
  104. if(count($toChange) === 1)
  105. {
  106. $first = $toChange[0];
  107. $tmpStart = $first['position'] + strlen($first['keyWord']) + 2;
  108. $betwean = substr($key, $tmpStart, strlen($key) - $tmpStart);
  109. $newValue = substr($message, $first['position'], (strpos($message, $betwean) - $first['position']));
  110. $key = str_replace('{'.$first['keyWord'].'}', $newValue, $key);
  111. $value = str_replace('{'.$first['keyWord'].'}', $newValue, $value);
  112. }
  113. else
  114. {
  115. $first = $toChange[0];
  116. $next = $toChange[1];
  117. $tmpStart = $first['position'] + strlen($first['keyWord']) + 2;
  118. $betwean = substr($key, $tmpStart, $next['position'] - $tmpStart);
  119. $strPosBetwean = $tmpStart + (strpos(substr($message, $tmpStart), $betwean));
  120. $newValue = substr($message, $first['position'], $strPosBetwean - $first['position']);
  121. $key = str_replace('{'.$first['keyWord'].'}', $newValue, $key);
  122. $value = str_replace('{'.$first['keyWord'].'}', $newValue, $value);
  123. }
  124. $toChange = $this->getToChangeList($keys, $key);
  125. }
  126. return $value;
  127. }
  128. public function arraySortByValue($key)
  129. {
  130. return function ($obA, $obB) use ($key)
  131. {
  132. return strnatcmp($obA[$key], $obB[$key]);
  133. };
  134. }
  135. private function getToChangeList($keys, $key)
  136. {
  137. $toChange = array();
  138. foreach($keys as $keyWord)
  139. {
  140. $tmpPos = strpos($key, '{'.$keyWord.'}');
  141. if($tmpPos || $tmpPos === 0)
  142. {
  143. $toChange[] = array('keyWord' => $keyWord, 'position' => $tmpPos);
  144. }
  145. }
  146. usort($toChange, $this->arraySortByValue('position'));
  147. return $toChange;
  148. }
  149. }