PluginProbe
FluentCommunity – Ultra-Fast High-Performance Social Network, Community, LMS & Online Courses / 2.8.1
FluentCommunity – Ultra-Fast High-Performance Social Network, Community, LMS & Online Courses v2.8.1
2.11.0 2.10.0 2.10.01 2.9.1 2.9.0 2.8.1 2.8.0 2.7.7 2.7.5 2.7.0 2.6.01 2.6.0 2.5.0 2.4.01 trunk 1.0.90 1.0.91 1.0.92 1.0.93 1.0.94 1.0.95 1.0.96 1.0.97 1.0.98 1.0.99 All 78 releases
fluent-community / vendor / wpfluent / framework / src / WPFluent / Pagination / Cursor.php

Cursor.php in FluentCommunity – Ultra-Fast High-Performance Social Network, Community, LMS & Online Courses 2.8.1, at vendor/wpfluent/framework/src/WPFluent/Pagination/Cursor.php

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