-
Notifications
You must be signed in to change notification settings - Fork 3
/
RangeRequest.php
117 lines (98 loc) · 2.62 KB
/
RangeRequest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
<?php
/**
* Created by PhpStorm.
* User: redjik
* Date: 30.07.15
* Time: 14:58
*/
namespace Spira\Responder\Paginator;
use Illuminate\Http\Request;
use Symfony\Component\HttpKernel\Exception\HttpException;
class RangeRequest implements PaginatedRequestDecoratorInterface
{
private $parsed = false;
private $offset = null;
private $limit = null;
private $isGetLast = false;
/**
* @var Request
*/
private $request;
public function __construct(Request $request)
{
$this->request = $request;
}
/**
* @param null|int $default
* @return mixed
*/
public function getOffset($default = null)
{
$this->parse();
return is_null($this->offset)?$default:$this->offset;
}
/**
* @param null|int $default
* @param null|int $max
* @return mixed
*/
public function getLimit($default = null, $max = null)
{
$this->parse();
$limit = is_null($this->limit)?$default:$this->limit;
return ($limit > $max)?$max:$limit;
}
/**
* states that limit should be applied to the end of the result set
* @return bool
*/
public function isGetLast()
{
$this->parse();
return $this->isGetLast;
}
/**
* @return bool
*/
private function parse()
{
if ($this->parsed) {
return true;
}
$range = null;
if ($this->getRequest()->headers) {
$range = $this->getRequest()->headers->get('Range');
}
if (is_null($range)) {
throw new HttpException(400, 'Bad Request');
}
//parsing the template \d-\d (ex. 20-39)
$ranges = explode('-', $range);
//if we have part before dash, we can find offset
if (isset($ranges[0]) && $ranges[0] !== '') {
$this->offset = $ranges[0];
}
//if we have part after dash, we can find limit
if (isset($ranges[1]) && $ranges[1] !== '') {
if (is_null($this->offset)) {
// if we don't have offset, that means we want to get last-n entities
$this->isGetLast = true;
$this->limit = $ranges[1];
} else {
//if we have offset we calculate limit
$this->limit = $ranges[1] - $ranges[0] + 1;
}
}
if (!is_null($this->limit) && $this->limit <= 0) {
throw new HttpException(400, 'Bad Request');
}
return $this->parsed = true;
}
/**
* @return Request
*/
public function getRequest()
{
return $this->request;
}
}