Просмотр файла vendor/laravel/framework/src/Illuminate/Pagination/Cursor.php

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