Skip to content

Commit

Permalink
fixed cache verses for offset
Browse files Browse the repository at this point in the history
  • Loading branch information
theahmadzai committed Aug 2, 2017
1 parent 83d0429 commit 1874c16
Showing 1 changed file with 17 additions and 21 deletions.
38 changes: 17 additions & 21 deletions src/Chapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'];
Expand All @@ -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:
Expand Down

0 comments on commit 1874c16

Please sign in to comment.