CursorPaginator::cursor()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 4
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Flugg\Responder\Pagination;
4
5
use Closure;
6
use Illuminate\Support\Collection;
7
use LogicException;
8
9
/**
10
 * A paginator class for handling cursor-based pagination.
11
 *
12
 * @package flugger/laravel-responder
13
 * @author  Alexander Tømmerås <[email protected]>
14
 * @license The MIT License
15
 */
16
class CursorPaginator
17
{
18
    /**
19
     * A list of the items being paginated.
20
     *
21
     * @var \Illuminate\Support\Collection
22
     */
23
    protected $items;
24
25
    /**
26
     * The current cursor reference.
27
     *
28
     * @var int|string|null
29
     */
30
    protected $cursor;
31
32
    /**
33
     * The previous cursor reference.
34
     *
35
     * @var int|string|null
36
     */
37
    protected $previousCursor;
38
39
    /**
40
     * The next cursor reference.
41
     *
42
     * @var int|string|null
43
     */
44
    protected $nextCursor;
45
46
    /**
47
     * The current cursor resolver callback.
48
     *
49
     * @var \Closure|null
50
     */
51
    protected static $currentCursorResolver;
52
53
    /**
54
     * Create a new paginator instance.
55
     *
56
     * @param \Illuminate\Support\Collection|array|null $data
57
     * @param int|string|null                           $cursor
58
     * @param int|string|null                           $previousCursor
59
     * @param int|string|null                           $nextCursor
60
     */
61 3
    public function __construct($data, $cursor, $previousCursor, $nextCursor)
62
    {
63 3
        $this->cursor = $cursor;
64 3
        $this->previousCursor = $previousCursor;
65 3
        $this->nextCursor = $nextCursor;
66
67 3
        $this->set($data);
68 3
    }
69
70
    /**
71
     * Retrieve the current cursor reference.
72
     *
73
     * @return int|string|null
74
     */
75 1
    public function cursor()
76
    {
77 1
        return $this->cursor;
78
    }
79
80
    /**
81
     * Retireve the next cursor reference.
82
     *
83
     * @return int|string|null
84
     */
85 1
    public function previous()
86
    {
87 1
        return $this->previousCursor;
88
    }
89
90
    /**
91
     * Retireve the next cursor reference.
92
     *
93
     * @return int|string|null
94
     */
95 1
    public function next()
96
    {
97 1
        return $this->nextCursor;
98
    }
99
100
    /**
101
     * Get the slice of items being paginated.
102
     *
103
     * @return array
104
     */
105 2
    public function items(): array
106
    {
107 2
        return $this->items->all();
108
    }
109
110
    /**
111
     * Get the paginator's underlying collection.
112
     *
113
     * @return \Illuminate\Support\Collection
114
     */
115 1
    public function get(): Collection
116
    {
117 1
        return $this->items;
118
    }
119
120
    /**
121
     * Set the paginator's underlying collection.
122
     *
123
     * @param  \Illuminate\Support\Collection|array|null $data
124
     * @return self
125
     */
126 3
    public function set($data): CursorPaginator
127
    {
128 3
        $this->items = $data instanceof Collection ? $data : collect($data);
129
130 3
        return $this;
131
    }
132
133
    /**
134
     * Resolve the current cursor using the cursor resolver.
135
     *
136
     * @param  string $name
137
     * @return mixed
138
     * @throws \LogicException
139
     */
140 2
    public static function resolveCursor(string $name = 'cursor')
141
    {
142 2
        if (isset(static::$currentCursorResolver)) {
143 1
            return call_user_func(static::$currentCursorResolver, $name);
144
        }
145
146 1
        throw new LogicException("Could not resolve cursor with the name [{$name}].");
147
    }
148
149
    /**
150
     * Set the current cursor resolver callback.
151
     *
152
     * @param  \Closure $resolver
153
     * @return void
154
     */
155 1
    public static function cursorResolver(Closure $resolver)
156
    {
157 1
        static::$currentCursorResolver = $resolver;
158 1
    }
159
}
160