-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: ensure resource id's are always encoded/decoded when appropriate…
… within a cursor test: test cursor id encoding/decoding
- Loading branch information
Gregory Haddow
committed
Aug 6, 2024
1 parent
06ee7ad
commit 1ef14ee
Showing
6 changed files
with
427 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<?php | ||
|
||
namespace LaravelJsonApi\Eloquent\Pagination\Cursor; | ||
|
||
use Illuminate\Pagination\Cursor as LaravelCursor; | ||
use LaravelJsonApi\Core\Schema\IdParser; | ||
|
||
class CursorParser | ||
{ | ||
public function __construct(private IdParser $idParser, private string $keyName) | ||
{ | ||
|
||
} | ||
|
||
public function encode(LaravelCursor $cursor): string | ||
{ | ||
$key = $cursor->parameter($this->keyName); | ||
if (!$key) { | ||
return $cursor->encode(); | ||
} | ||
|
||
$encodedId = $this->idParser->encode($key); | ||
$parameters = $cursor->toArray(); | ||
unset($parameters['_pointsToNextItems']); | ||
$parameters[$this->keyName] = $encodedId; | ||
|
||
$newCursor = new LaravelCursor($parameters, $cursor->pointsToNextItems()); | ||
return $newCursor->encode(); | ||
} | ||
|
||
public function decode(Cursor $cursor): ?LaravelCursor | ||
{ | ||
$encodedCursor = $cursor->isBefore() ? $cursor->getBefore() : $cursor->getAfter(); | ||
if (!is_string($encodedCursor)) { | ||
return null; | ||
} | ||
|
||
$parameters = json_decode(base64_decode(str_replace(['-', '_'], ['+', '/'], $encodedCursor)), true); | ||
|
||
if (json_last_error() !== JSON_ERROR_NONE) { | ||
return null; | ||
} | ||
|
||
$pointsToNextItems = $parameters['_pointsToNextItems']; | ||
unset($parameters['_pointsToNextItems']); | ||
if (isset($parameters[$this->keyName])) { | ||
$parameters[$this->keyName] = $this->idParser->decode( | ||
$parameters[$this->keyName], | ||
); | ||
} | ||
|
||
return new LaravelCursor($parameters, $pointsToNextItems); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.