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; } }