diff --git a/src/LinguaLeo/wti/WtiApi.php b/src/LinguaLeo/wti/WtiApi.php index 47a7df2..86e8a94 100644 --- a/src/LinguaLeo/wti/WtiApi.php +++ b/src/LinguaLeo/wti/WtiApi.php @@ -123,15 +123,35 @@ public function getStringsByKey($key, $fileId) return $this->listStrings($params); } - public function listStrings($params = null) { + public function listStrings($params = null, $page = 1) + { + if ($params == null) { + $params = []; + } + $params['page'] = intval($page); $this->request = $this->builder() ->setMethod(RequestMethod::GET) ->setEndpoint('strings') ->setParams($params) ->build(); $this->request->run(); - $result = $this->request->getResult(); - return $result ? $result : []; + $requestResult = $this->request->getResult(); + $result = $requestResult ? $requestResult : []; + + if (count($result) && $this->isRequestHasNextPage()) { + $result += $this->listStrings($params, ++$page); + } + + return $result; + } + + private function isRequestHasNextPage() + { + $responseHeaders = $this->request->getHeaders(); + if (isset($responseHeaders['Link'])) { + return true; + } + return false; } /** @@ -141,7 +161,8 @@ public function listStrings($params = null) { * @param $localeCode * @return mixed|null */ - public function getTranslation ($stringId, $localeCode) { + public function getTranslation($stringId, $localeCode) + { $this->request = $this->builder() ->setMethod(RequestMethod::GET) ->setEndpoint('strings/' . $stringId . '/locales/' . $localeCode . '/translations') @@ -384,7 +405,8 @@ public function updateFile( $minorChanges = false, $label = null, $mime = 'application/json' - ) { + ) + { $params = [ 'name' => $name, 'merge' => $merge, diff --git a/src/LinguaLeo/wti/WtiApiRequest.php b/src/LinguaLeo/wti/WtiApiRequest.php index 1ca02c0..3162207 100644 --- a/src/LinguaLeo/wti/WtiApiRequest.php +++ b/src/LinguaLeo/wti/WtiApiRequest.php @@ -11,6 +11,7 @@ class WtiApiRequest private $error; private $errno; private $result; + private $headers; private $isRequestRunned = false; public function __construct($resource) @@ -21,13 +22,38 @@ public function __construct($resource) public function run() { $this->isRequestRunned = true; - $this->result = curl_exec($this->resource); + $result = curl_exec($this->resource); + + $header_size = curl_getinfo($this->resource, CURLINFO_HEADER_SIZE); + $this->headers = $this->prepareHeaders(substr($result, 0, $header_size)); + $this->result = substr($result, $header_size); + if ($this->result === false) { $this->error = curl_error($this->resource); $this->errno = curl_errno($this->resource); } } + private function prepareHeaders ($headersString) { + $headersArray = explode("\n", $headersString); + // Remove first header, HTTP code + array_shift($headersArray); + + $headersAssoc = []; + + foreach ($headersArray as $header) { + if ($header === '') { + continue; + } + preg_match('~^([^:]*)\:(.*)$~', $header, $matches); + if (count($matches) == 3) { + $headersAssoc[$matches[1]] = trim($matches[2]); + } + } + + return $headersAssoc; + } + public function getError() { return $this->error; @@ -54,4 +80,11 @@ public function getResult($assoc = false) return $this->result ? json_decode($this->result, $assoc) : null; } + public function getHeaders () { + if (!$this->isRequestRunned) { + throw new WtiApiException("Request must be performed before getting results."); + } + return $this->headers; + } + } \ No newline at end of file diff --git a/src/LinguaLeo/wti/WtiRequestBuilder.php b/src/LinguaLeo/wti/WtiRequestBuilder.php index 8f8fc83..635e18c 100644 --- a/src/LinguaLeo/wti/WtiRequestBuilder.php +++ b/src/LinguaLeo/wti/WtiRequestBuilder.php @@ -60,6 +60,7 @@ public function build() } curl_setopt($this->resource, CURLOPT_URL, $this->buildRequestUrl()); curl_setopt($this->resource, CURLOPT_RETURNTRANSFER, true); + curl_setopt($this->resource, CURLOPT_HEADER, 1); curl_setopt($this->resource, CURLOPT_CUSTOMREQUEST, $this->method); if ($this->jsonEncodeParams) { curl_setopt($this->resource, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));