Skip to content
This repository has been archived by the owner on Sep 9, 2022. It is now read-only.

Commit

Permalink
Support for unix dates
Browse files Browse the repository at this point in the history
  • Loading branch information
juampi92 committed Jul 27, 2018
1 parent 8a03a45 commit dc4e7f2
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 7 deletions.
37 changes: 37 additions & 0 deletions src/Cursor.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ class Cursor
{
protected $prev = null;
protected $next = null;
protected $date_identifier = false;

/**
* Cursor constructor.
Expand All @@ -19,6 +20,14 @@ public function __construct($prev = null, $next = null)
$this->next = $next;
}

/**
* @param bool $value
*/
public function setDateIdentifier($value = true)
{
$this->date_identifier = $value;
}

/**
* @return bool
*/
Expand Down Expand Up @@ -58,4 +67,32 @@ public function getNextCursor()
{
return $this->next;
}

/**
* @return mixed
*/
public function getPrevQuery()
{
$prev = $this->getPrevCursor();

if ($this->date_identifier && is_numeric($prev)) {
return date('c', $prev);
}

return $prev;
}

/**
* @return mixed
*/
public function getNextQuery()
{
$next = $this->getNextCursor();

if ($this->date_identifier && is_numeric($next)) {
return date('c', $next);
}

return $next;
}
}
17 changes: 12 additions & 5 deletions src/CursorPaginationServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,25 +66,32 @@ public function registerMacro()
$identifier_sort = $query_orders->firstWhere('column', $options['identifier']);
}

if (!isset($options['request'])) {
$options['request'] = request();
}

if (!isset($options['date_identifier']) && isset($this->model)) {
$options['date_identifier'] = $this->model->hasCast($options['identifier'], ['datetime', 'date']);
}

if (!isset($options['request'])) {
$options['request'] = request();
}

// Resolve the cursor by using the request query params
$cursor = CursorPaginator::resolveCurrentCursor($options['request']);

if (isset($options['date_identifier']) && $options['date_identifier']) {
// Also check that the database does not contain unix dates.
$not_using_unix = !(isset($options['date_unix']) && $options['date_unix']);
$cursor->setDateIdentifier($not_using_unix);
}

// If there's a sorting by the identifier, check if it's desc so the cursor is inverted
$identifier_sort_inverted = $identifier_sort ? $identifier_sort['direction'] === 'desc' : false;

if ($cursor->isPrev()) {
$this->where($options['identifier'], $identifier_sort_inverted ? '>' : '<', $cursor->getPrevCursor());
$this->where($options['identifier'], $identifier_sort_inverted ? '>' : '<', $cursor->getPrevQuery());
}
if ($cursor->isNext()) {
$this->where($options['identifier'], $identifier_sort_inverted ? '<' : '>', $cursor->getNextCursor());
$this->where($options['identifier'], $identifier_sort_inverted ? '<' : '>', $cursor->getNextQuery());
}

// Use configs perPage if it's not defined
Expand Down
2 changes: 0 additions & 2 deletions tests/RequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,6 @@ public function test_inverted_order()

$response = $this->get("/test/inverse?$next_name=$next_cur");

//dd(json_decode($response->getOriginalContent()));

$response->assertJsonFragment([$prev_name => null]);
$response->assertJsonFragment([$next_name => (string) ($next_cur - 5)]);
$response->assertJsonFragment(['per_page' => config('cursor_pagination.per_page')]);
Expand Down

0 comments on commit dc4e7f2

Please sign in to comment.