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

Commit

Permalink
Params name free transformation
Browse files Browse the repository at this point in the history
  • Loading branch information
juampi92 committed Mar 9, 2018
1 parent 0b61b59 commit 8a03a45
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 30 deletions.
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,22 @@ This way, when you do a *'refresh'*, you can fetch more results than if you are

To configure that, set `$perPage = [15, 5]`. That way it'll fetch 15 when you do a `previous_cursor`, and 5 when you do a normal fetch or a `next_cursor`.

## Customizing param names

On the config, change `identifier_name` to change the word `cursor`. Examples: `cursor`, `id`, `pointer`, etc.

Change `navigation_names` to change the words `['previous', 'next']`.
Examples: `['before', 'after']` , `['min', 'max']`

##### Transforming the result:
Edit `transform_name` to format the string differently:

- For `previousCursor` use 'camel_case'
- For `previous-cursor` use 'kebab_case'.
- For `previous_cursor` use 'snake_case'.

Using `null` defaults to camelCase.

## API Docs

### Paginator custom options
Expand Down
9 changes: 5 additions & 4 deletions config/cursor_pagination.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,13 @@

/*
|--------------------------------------------------------------------------
| Camel Case Names
| Transform Name
|--------------------------------------------------------------------------
|
| Use camel case instead of snake case.
| Specify a global function to be called to format the original snake_case.
| Can use Laravel's Helper functions.
|
| Examples: previousCursor (camel) instead of previous_cursor (snake)
| Examples: 'camel_case', 'kebab_case', 'snake_case'.
*/
'camel_case' => false,
'transform_name' => 'snake_case',
];
32 changes: 19 additions & 13 deletions src/CursorPaginator.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,35 +114,41 @@ public static function resolveCurrentCursor(Request $request = null)
public static function cursorQueryNames()
{
$ident = config('cursor_pagination.identifier_name');
$ident = ucfirst($ident);
list($prev, $next) = config('cursor_pagination.navigation_names');

return [
self::formatNames("{$prev}_$ident"),
self::formatNames("{$next}_$ident"),
self::formatNames("{$prev}$ident"),
self::formatNames("{$next}$ident"),
];
}

/**
* @return array
* @param string $name
*
* @return string
*/
public function getCursorQueryNames()
protected static function formatNames($name)
{
if (is_null($this->cursor_queue_names)) {
$this->cursor_queue_names = static::cursorQueryNames();
$transform_name = config('cursor_pagination.transform_name', null);

if (!is_null($transform_name)) {
return call_user_func($transform_name, $name);
}

return $this->cursor_queue_names;
return $name;
}

protected static function formatNames($name)
/**
* @return array
*/
public function getCursorQueryNames()
{
$camel_case = config('cursor_pagination.camel_case', false);

if ($camel_case) {
return camel_case($name);
if (is_null($this->cursor_queue_names)) {
$this->cursor_queue_names = static::cursorQueryNames();
}

return $name;
return $this->cursor_queue_names;
}

public function nextCursor()
Expand Down
12 changes: 9 additions & 3 deletions tests/CursorNameTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,24 @@ class CursorNameTest extends TestCase
{
public function test_camel_case()
{
config(['cursor_pagination.camel_case' => false]);
config(['cursor_pagination.transform_name' => 'snake_case']);
list($prev_name, $next_name) = CursorPaginator::cursorQueryNames();

$this->assertContains('_', $prev_name);
$this->assertContains('_', $next_name);

config(['cursor_pagination.camel_case' => true]);
config(['cursor_pagination.transform_name' => 'camel_case']);
list($prev_name, $next_name) = CursorPaginator::cursorQueryNames();

$this->assertNotContains('_', $prev_name);
$this->assertNotContains('_', $next_name);

config(['cursor_pagination.camel_case' => false]);
config(['cursor_pagination.transform_name' => 'kebab_case']);
list($prev_name, $next_name) = CursorPaginator::cursorQueryNames();

$this->assertContains('-', $prev_name);
$this->assertContains('-', $next_name);

config(['cursor_pagination.transform_name' => null]);
}
}
21 changes: 11 additions & 10 deletions tests/Fixtures/config/simple.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
| of the array will be results per page when it's fetching previous, and
| the second one is for next and normal fetches.
*/
'per_page' => 5,
'per_page' => 5,
/*
|--------------------------------------------------------------------------
| Identifier name
Expand All @@ -38,13 +38,14 @@
'navigation_names' => ['previous', 'next'],

/*
|--------------------------------------------------------------------------
| Camel Case Names
|--------------------------------------------------------------------------
|
| Use camel case instead of snake case.
|
| Examples: previousCursor (camel) instead of previous_cursor (snake)
*/
'camel_case' => false,
|--------------------------------------------------------------------------
| Transform Name
|--------------------------------------------------------------------------
|
| Specify a global function to be called to format the original snake_case.
| Can use Laravel's Helper functions.
|
| Examples: 'camel_case', 'kebab_case', 'snake_case'.
*/
'transform_name' => 'snake_case',
];

1 comment on commit 8a03a45

@juampi92
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Better fixes #3

Please sign in to comment.