Cursor.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <?php
  2. namespace Illuminate\Pagination;
  3. use Illuminate\Contracts\Support\Arrayable;
  4. use UnexpectedValueException;
  5. class Cursor implements Arrayable
  6. {
  7. /**
  8. * The parameters associated with the cursor.
  9. *
  10. * @var array
  11. */
  12. protected $parameters;
  13. /**
  14. * Determine whether the cursor points to the next or previous set of items.
  15. *
  16. * @var bool
  17. */
  18. protected $pointsToNextItems;
  19. /**
  20. * Create a new cursor instance.
  21. *
  22. * @param array $parameters
  23. * @param bool $pointsToNextItems
  24. */
  25. public function __construct(array $parameters, $pointsToNextItems = true)
  26. {
  27. $this->parameters = $parameters;
  28. $this->pointsToNextItems = $pointsToNextItems;
  29. }
  30. /**
  31. * Get the given parameter from the cursor.
  32. *
  33. * @param string $parameterName
  34. * @return string|null
  35. *
  36. * @throws \UnexpectedValueException
  37. */
  38. public function parameter(string $parameterName)
  39. {
  40. if (! array_key_exists($parameterName, $this->parameters)) {
  41. throw new UnexpectedValueException("Unable to find parameter [{$parameterName}] in pagination item.");
  42. }
  43. return $this->parameters[$parameterName];
  44. }
  45. /**
  46. * Get the given parameters from the cursor.
  47. *
  48. * @param array $parameterNames
  49. * @return array
  50. */
  51. public function parameters(array $parameterNames)
  52. {
  53. return collect($parameterNames)->map(function ($parameterName) {
  54. return $this->parameter($parameterName);
  55. })->toArray();
  56. }
  57. /**
  58. * Determine whether the cursor points to the next set of items.
  59. *
  60. * @return bool
  61. */
  62. public function pointsToNextItems()
  63. {
  64. return $this->pointsToNextItems;
  65. }
  66. /**
  67. * Determine whether the cursor points to the previous set of items.
  68. *
  69. * @return bool
  70. */
  71. public function pointsToPreviousItems()
  72. {
  73. return ! $this->pointsToNextItems;
  74. }
  75. /**
  76. * Get the array representation of the cursor.
  77. *
  78. * @return array
  79. */
  80. public function toArray()
  81. {
  82. return array_merge($this->parameters, [
  83. '_pointsToNextItems' => $this->pointsToNextItems,
  84. ]);
  85. }
  86. /**
  87. * Get the encoded string representation of the cursor to construct a URL.
  88. *
  89. * @return string
  90. */
  91. public function encode()
  92. {
  93. return str_replace(['+', '/', '='], ['-', '_', ''], base64_encode(json_encode($this->toArray())));
  94. }
  95. /**
  96. * Get a cursor instance from the encoded string representation.
  97. *
  98. * @param string|null $encodedString
  99. * @return static|null
  100. */
  101. public static function fromEncoded($encodedString)
  102. {
  103. if (is_null($encodedString) || ! is_string($encodedString)) {
  104. return null;
  105. }
  106. $parameters = json_decode(base64_decode(str_replace(['-', '_'], ['+', '/'], $encodedString)), true);
  107. if (json_last_error() !== JSON_ERROR_NONE) {
  108. return null;
  109. }
  110. $pointsToNextItems = $parameters['_pointsToNextItems'];
  111. unset($parameters['_pointsToNextItems']);
  112. return new static($parameters, $pointsToNextItems);
  113. }
  114. }