|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Lincable\Http; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Http\Request; |
|
6
|
|
|
use Lincable\Http\File\FileResolver; |
|
7
|
|
|
use Lincable\Concerns\BuildClassnames; |
|
8
|
|
|
use Illuminate\Foundation\Http\FormRequest; |
|
9
|
|
|
use Illuminate\Contracts\Validation\Factory; |
|
10
|
|
|
use Symfony\Component\HttpFoundation\File\File; |
|
11
|
|
|
|
|
12
|
|
|
abstract class FileRequest extends FormRequest |
|
13
|
|
|
{ |
|
14
|
|
|
use BuildClassnames; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* The file parameter name. |
|
18
|
|
|
* |
|
19
|
|
|
* @var string|null |
|
20
|
|
|
*/ |
|
21
|
|
|
protected $parameter; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Rules to validate the file on request. |
|
25
|
|
|
* |
|
26
|
|
|
* @return mixed |
|
27
|
|
|
*/ |
|
28
|
|
|
abstract public function rules(); |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Return the file on request. |
|
32
|
|
|
* |
|
33
|
|
|
* @return \Illuminate\Http\UploadedFile |
|
34
|
|
|
*/ |
|
35
|
3 |
|
public function getFile() |
|
36
|
|
|
{ |
|
37
|
3 |
|
return $this->file($this->getParameter()); |
|
|
|
|
|
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Return the parameter name. |
|
42
|
|
|
* |
|
43
|
|
|
* @return string |
|
44
|
|
|
*/ |
|
45
|
5 |
|
public function getParameter() |
|
46
|
|
|
{ |
|
47
|
5 |
|
if ($this->parameter === null) { |
|
48
|
4 |
|
$this->parameter = $this->retrieveParameter(); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
5 |
|
return $this->parameter; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Set the parameter name. |
|
56
|
|
|
* |
|
57
|
|
|
* @param string $parameter |
|
58
|
|
|
* @return this |
|
59
|
|
|
*/ |
|
60
|
1 |
|
public function setParameter(string $parameter) |
|
61
|
|
|
{ |
|
62
|
1 |
|
$this->parameter = $parameter; |
|
63
|
|
|
|
|
64
|
1 |
|
return $this; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Shortcut for @method setParameter. |
|
69
|
|
|
* |
|
70
|
|
|
* @param string $parameter |
|
71
|
|
|
* @return this |
|
72
|
|
|
*/ |
|
73
|
1 |
|
public function as(string $parameter) |
|
74
|
|
|
{ |
|
75
|
1 |
|
return $this->setParameter($parameter); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Prepare the file for sending. |
|
80
|
|
|
* |
|
81
|
|
|
* @return mixed |
|
82
|
|
|
*/ |
|
83
|
3 |
|
public function prepareFile() |
|
84
|
|
|
{ |
|
85
|
3 |
|
$file = $this->moveFileToTempDirectory(); |
|
86
|
|
|
|
|
87
|
3 |
|
return $this->executeFileEvents($file); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
*{@inheritDoc} |
|
92
|
|
|
*/ |
|
93
|
1 |
|
public function validator(Factory $factory) |
|
94
|
|
|
{ |
|
95
|
1 |
|
return $factory->make( |
|
96
|
1 |
|
$this->validationData(), $this->parseValidationRules(), |
|
97
|
1 |
|
$this->messages(), $this->attributes() |
|
98
|
|
|
); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Return the parameter name from class name. |
|
103
|
|
|
* |
|
104
|
|
|
* @return string |
|
105
|
|
|
*/ |
|
106
|
4 |
|
protected function retrieveParameter() |
|
107
|
|
|
{ |
|
108
|
4 |
|
$className = static::class; |
|
109
|
|
|
|
|
110
|
4 |
|
return $this->nameFromClass($className, class_basename(self::class)); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* Get the rules for the file validation. |
|
115
|
|
|
* |
|
116
|
|
|
* @return array |
|
117
|
|
|
*/ |
|
118
|
1 |
|
protected function parseValidationRules() |
|
119
|
|
|
{ |
|
120
|
1 |
|
return [$this->getParameter() => $this->container->call([$this, 'rules'])]; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* Move the file to a temporary destination. |
|
125
|
|
|
* |
|
126
|
|
|
* @return \Symfony\Component\HttpFoundation\File\File |
|
127
|
|
|
*/ |
|
128
|
3 |
|
protected function moveFileToTempDirectory() |
|
129
|
|
|
{ |
|
130
|
3 |
|
return FileResolver::resolve($this->getFile()); |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* Execute some generic event methods on class if available. |
|
135
|
|
|
* |
|
136
|
|
|
* Here the file can be changed, optimized, etc... |
|
137
|
|
|
* |
|
138
|
|
|
* @param \Symfony\Component\HttpFoundation\File\File $file |
|
139
|
|
|
* @return mixed |
|
140
|
|
|
*/ |
|
141
|
3 |
|
protected function executeFileEvents(File $file) |
|
142
|
|
|
{ |
|
143
|
3 |
|
$callable = [$this, 'beforeSend']; |
|
144
|
|
|
|
|
145
|
3 |
|
if (method_exists($callable[0], $callable[1])) { |
|
146
|
|
|
|
|
147
|
|
|
// Handle the result from event call. |
|
148
|
1 |
|
if ($result = $this->container->call($callable, [$file])) { |
|
149
|
1 |
|
return $result; |
|
150
|
|
|
} |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
2 |
|
return $file; |
|
154
|
|
|
} |
|
155
|
|
|
} |
|
156
|
|
|
|