From 1874c16ae9b8f02a8b20d11934442d6a99769aac Mon Sep 17 00:00:00 2001 From: Javed Ahmadzai Date: Wed, 2 Aug 2017 22:04:43 +0500 Subject: [PATCH] fixed cache verses for offset --- src/Chapter.php | 38 +++++++++++++++++--------------------- 1 file changed, 17 insertions(+), 21 deletions(-) diff --git a/src/Chapter.php b/src/Chapter.php index 1aa6f5a..0b156b5 100644 --- a/src/Chapter.php +++ b/src/Chapter.php @@ -226,52 +226,48 @@ public function verse(array $options = [], array $tokens = []) $limit = $options['limit']; } - $start = $offset; - $end = ($offset + ($limit - 1)); if (filesize($file) !== 0) { $data = require $file; - $cached = array_filter($data, function ($key) use ($data, &$offset, &$limit, $start, $end) { - if ($key >= $start && $key <= $end) { - $offset = $key; + $cached = array_filter($data, function ($key) use ($data, &$offset, &$limit) { + if (isset($data[$key])) { $limit--; - if (isset($data[$key])) { - return $data[$key]; - } + $offset = $key + 1; + + return $data[$key]; } }, ARRAY_FILTER_USE_KEY); } + if ($limit === 0 || (isset($cached) && count($cached) === self::CHAPTERS[$this->chapter])) { - $data = $cached; + $data = array_values($cached); goto DATA; } + $options['offset'] = $offset - 1; if ($limit === 1) { $options['limit'] = $limit + 1; } } - if (isset($options['translations'])) { - $translations = $options['translations']; - } + $http_query = http_build_query($options); - if (!empty($translations) && is_array($translations)) { - array_walk($translations, function ($key, $value, $default = 'translations') use (&$build_query) { + if (isset($options['translations']) && is_array($options['translations'])) { + array_walk($options['translations'], function ($key, $value, $default = 'translations') use (&$build_query) { $build_query[] = http_build_query([$default => $key]); }); unset($options['translations']); - $http_query = http_build_query($options) . '&' . implode('&', $build_query); - } else { - $http_query = http_build_query($options); - } + $http_query .= '&' . implode('&', $build_query); + } + echo $http_query; $query = $this->request->send( "chapters/{$this->chapter}/verses", $http_query ); if (empty($query['verses'])) { - return $query['verses']; + return $query; } $start = $query['verses'][0]['verse_number']; @@ -291,9 +287,9 @@ public function verse(array $options = [], array $tokens = []) ); } if (!empty($cached)) { - $data = array_replace($cached, $query); + $data = array_values(array_replace($cached, $query)); } else { - $data = $query; + $data = array_values($query); } DATA: