Repository navigation
Expand file tree
/
Copy pathRequestValidator.php
More file actions
107 lines (94 loc) · 2.33 KB
/
Copy pathRequestValidator.php
File metadata and controls
107 lines (94 loc) · 2.33 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
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
<?php
namespace Dingo\Api\Http;
use Illuminate\Container\Container;
use Dingo\Api\Http\Validation\Accept;
use Dingo\Api\Http\Validation\Domain;
use Dingo\Api\Http\Validation\Prefix;
use Dingo\Api\Contract\Http\Validator;
use Illuminate\Http\Request as IlluminateRequest;
class RequestValidator
{
/**
* Container instance.
*
* @var \Illuminate\Container\Container
*/
protected $container;
/**
* Array of request validators.
*
* @var array
*/
protected $validators = [
Domain::class,
Prefix::class,
];
/**
* Create a new request validator instance.
*
* @param \Illuminate\Container\Container $container
*
* @return void
*/
public function __construct(Container $container)
{
$this->container = $container;
}
/**
* Replace the validators.
*
* @param array $validators
*
* @return void
*/
public function replace(array $validators)
{
$this->validators = $validators;
}
/**
* Merge an array of validators.
*
* @param array $validators
*
* @return void
*/
public function merge(array $validators)
{
$this->validators = array_merge($this->validators, $validators);
}
/**
* Extend the validators.
*
* @param string|\Dingo\Api\Http\Validator $validator
*
* @return void
*/
public function extend($validator)
{
$this->validators[] = $validator;
}
/**
* Validate a request.
*
* @param \Illuminate\Http\Request $request
*
* @return bool
*/
public function validateRequest(IlluminateRequest $request)
{
$passed = false;
foreach ($this->validators as $validator) {
$validator = $this->container->make($validator);
if ($validator instanceof Validator && $validator->validate($request)) {
$passed = true;
}
}
// The accept validator will always be run once any of the previous validators have
// been run. This ensures that we only run the accept validator once we know we
// have a request that is targeting the API.
if ($passed) {
$this->container->make(Accept::class)->validate($request);
}
return $passed;
}
}