Repository navigation
Expand file tree
/
Copy pathAccept.php
More file actions
64 lines (57 loc) · 1.56 KB
/
Copy pathAccept.php
File metadata and controls
64 lines (57 loc) · 1.56 KB
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
<?php
namespace Dingo\Api\Http\Validation;
use Illuminate\Http\Request;
use Dingo\Api\Contract\Http\Validator;
use Dingo\Api\Http\Parser\Accept as AcceptParser;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
class Accept implements Validator
{
/**
* Accept parser instance.
*
* @var \Dingo\Api\Http\Parser\Accept
*/
protected $accept;
/**
* Indicates if the accept matching is strict.
*
* @var bool
*/
protected $strict;
/**
* Create a new accept validator instance.
*
* @param \Dingo\Api\Http\Parser\Accept $accept
* @param bool $strict
*
* @return void
*/
public function __construct(AcceptParser $accept, $strict = false)
{
$this->accept = $accept;
$this->strict = $strict;
}
/**
* Validate the accept header on the request. If this fails it will throw
* an HTTP exception that will be caught by the middleware. This
* validator should always be run last and must not return
* a success boolean.
*
* @param \Illuminate\Http\Request $request
*
* @throws \Symfony\Component\HttpKernel\Exception\BadRequestHttpException
*
* @return bool
*/
public function validate(Request $request)
{
try {
$this->accept->parse($request, $this->strict);
} catch (BadRequestHttpException $exception) {
if ($request->getMethod() === 'OPTIONS') {
return true;
}
throw $exception;
}
}
}