| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178 |
- <?php
- namespace MGModule\DNSManager2\mgLibs\custom\dns\submodules\AWSRoute53;
- use MGModule\DNSManager2\mgLibs\custom\dns\submodules\AWSRoute53 as awsRoute53;
- class AWSRoute53Response implements \MGModule\DNSManager2\mgLibs\custom\dns\submodules\AWSRoute53\AWSRoute53ResponseInterface
- {
- private $responseBody;
- private $parsedBody;
- private $responseType;
- private $responseMessage;
-
- public function prepareResponse($responseBody)
- {
- $this->responseBody = $responseBody;
- $this->parseResponse();
-
- return $this;
- }
-
- private function parseResponse()
- {
- $this->parsedBody = simplexml_load_string($this->responseBody);
- if(!$this->parsedBody)
- {
- $this->prepareTextResponse('error', 'Invalid Response');
- return;
- }
-
- $respErr = awsRoute53\AWSRoute53ResponseParseHelper::checkResponseForErrors($this->parsedBody);
- if($respErr)
- {
- $this->prepareTextResponse('error', $this->parseErrorMessage($respErr['message']));
- return;
- }
- $this->responseType = $this->responseType ? $this->responseType : 'success';
- }
-
- public function prepareTextResponse($type, $message)
- {
- $this->responseType = $type;
- $this->responseMessage = $message;
- }
-
- public function getResponseType()
- {
- return $this->responseType;
- }
-
- public function getResponseMessage()
- {
- return $this->responseMessage;
- }
-
- public function getRawResponseBody()
- {
- return $this->responseBody;
- }
-
- public function getParsedResponseBody()
- {
- return $this->parsedBody;
- }
-
- public function parseErrorMessage($message)
- {
- foreach(awsRoute53\AWSRoute53ResponseMessages::$messages as $key => $value)
- {
- $keys = array();
- $this->matchWordKeys($key, $keys);
- if(count($keys) === 0 && $message === $key)
- {
- return $value;
- }
-
- $searchPattern = $this->getSearchPattern($key, $keys);
- $match = preg_match($searchPattern, $message);
- if((int)$match === 1)
- {
- $tmpMessage = $this->replaceKeys($message, $key, $value, $keys);
- if($tmpMessage && $tmpMessage !== '')
- {
- return $tmpMessage;
- }
- }
- }
- return $message;
- }
-
- private function matchWordKeys($text, &$keys)
- {
- $keyWordList = explode(' ', $text);
- foreach($keyWordList as $word)
- {
- if((substr($word, -1) === '}' && substr($word, 0, 1) === '{') || (substr($word, -2) === '},' && substr($word, 0, 1) === '{'))
- {
- $tmpWord = substr(trim($word, ','), 1, -1);
- if(!in_array($tmpWord, $keys))
- {
- $keys[] = $tmpWord;
- }
- }
- }
- }
-
- private function getSearchPattern($text, $keys)
- {
- foreach ($keys as $keyWord)
- {
- $text = str_replace('{'.$keyWord.'}', ".+", $text);
- }
-
- return "/".$text."/";
- }
-
- private function replaceKeys($message, $key, $value, $keys)
- {
- $toChange = $this->getToChangeList($keys, $key);
- while(count($toChange) > 0)
- {
- if(count($toChange) === 1)
- {
- $first = $toChange[0];
-
- $tmpStart = $first['position'] + strlen($first['keyWord']) + 2;
- $betwean = substr($key, $tmpStart, strlen($key) - $tmpStart);
- $newValue = substr($message, $first['position'], (strpos($message, $betwean) - $first['position']));
-
- $key = str_replace('{'.$first['keyWord'].'}', $newValue, $key);
- $value = str_replace('{'.$first['keyWord'].'}', $newValue, $value);
- }
- else
- {
- $first = $toChange[0];
- $next = $toChange[1];
-
- $tmpStart = $first['position'] + strlen($first['keyWord']) + 2;
- $betwean = substr($key, $tmpStart, $next['position'] - $tmpStart);
- $strPosBetwean = $tmpStart + (strpos(substr($message, $tmpStart), $betwean));
- $newValue = substr($message, $first['position'], $strPosBetwean - $first['position']);
-
- $key = str_replace('{'.$first['keyWord'].'}', $newValue, $key);
- $value = str_replace('{'.$first['keyWord'].'}', $newValue, $value);
- }
-
- $toChange = $this->getToChangeList($keys, $key);
- }
-
- return $value;
- }
-
- public function arraySortByValue($key)
- {
- return function ($obA, $obB) use ($key)
- {
- return strnatcmp($obA[$key], $obB[$key]);
- };
- }
-
- private function getToChangeList($keys, $key)
- {
- $toChange = array();
- foreach($keys as $keyWord)
- {
- $tmpPos = strpos($key, '{'.$keyWord.'}');
- if($tmpPos || $tmpPos === 0)
- {
- $toChange[] = array('keyWord' => $keyWord, 'position' => $tmpPos);
- }
- }
- usort($toChange, $this->arraySortByValue('position'));
-
- return $toChange;
- }
- }
|