1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Slince\Upload; |
4
|
|
|
|
5
|
|
|
use Closure; |
6
|
|
|
use LogicException; |
7
|
|
|
use Slince\Upload\Constraint\ConstraintInterface; |
8
|
|
|
use Slince\Upload\Constraint\ExtensionConstraint; |
9
|
|
|
use Slince\Upload\Constraint\MimeTypeConstraint; |
10
|
|
|
use Slince\Upload\Constraint\SizeConstraint; |
11
|
|
|
use Slince\Upload\Filesystem\FilesystemInterface; |
12
|
|
|
use Slince\Upload\Filesystem\Local; |
13
|
|
|
use Slince\Upload\Naming\GenericNamer; |
14
|
|
|
use Slince\Upload\Naming\NamerInterface; |
15
|
|
|
use Slince\Upload\Naming\ClosureNamer; |
16
|
|
|
use Slince\Upload\Processor\ChainProcessor; |
17
|
|
|
use Slince\Upload\Processor\ClosureProcessor; |
18
|
|
|
use Slince\Upload\Processor\ProcessorInterface; |
19
|
|
|
|
20
|
|
|
class UploadHandlerBuilder |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @var boolean |
24
|
|
|
*/ |
25
|
|
|
protected $overwrite = false; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var NamerInterface |
29
|
|
|
*/ |
30
|
|
|
protected $namer; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var FilesystemInterface |
34
|
|
|
*/ |
35
|
|
|
protected $filesystem; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var ConstraintInterface[] |
39
|
|
|
*/ |
40
|
|
|
protected $constraints = []; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var ProcessorInterface[] |
44
|
|
|
*/ |
45
|
|
|
protected $processors = []; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Sets overwrite mode. |
49
|
|
|
* |
50
|
|
|
* @param boolean $overwrite |
51
|
|
|
* @return $this |
52
|
|
|
*/ |
53
|
|
|
public function overwrite(bool $overwrite = true): self |
54
|
|
|
{ |
55
|
|
|
$this->overwrite = $overwrite; |
56
|
|
|
return $this; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Set allowed mime types. |
61
|
|
|
* |
62
|
|
|
* @param array|string $mimeTypes |
63
|
|
|
* @param string|null $errorMessageTemplate |
64
|
|
|
* @return $this |
65
|
|
|
*/ |
66
|
|
|
public function allowMimeTypes($mimeTypes, ?string $errorMessageTemplate = null): self |
67
|
|
|
{ |
68
|
|
|
if (!is_array($mimeTypes)) { |
69
|
|
|
$mimeTypes = [$mimeTypes]; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
$constraint = new MimeTypeConstraint($mimeTypes); |
73
|
|
|
if (null !== $errorMessageTemplate) { |
74
|
|
|
$constraint->setErrorMessage($errorMessageTemplate); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
$this->constraints[] = $constraint; |
78
|
|
|
return $this; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Set size range |
83
|
|
|
* |
84
|
|
|
* @param string|int|null $from |
85
|
|
|
* @param string|int|null $to |
86
|
|
|
* @param string|null $errorMessageTemplate |
87
|
|
|
* @return $this |
88
|
|
|
*/ |
89
|
|
|
public function sizeBetween($from, $to, ?string $errorMessageTemplate = null): self |
90
|
|
|
{ |
91
|
|
|
$constraint = new SizeConstraint($from, $to); |
92
|
|
|
if (null !== $errorMessageTemplate) { |
93
|
|
|
$constraint->setErrorMessage($errorMessageTemplate); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
$this->constraints[] = $constraint; |
97
|
|
|
return $this; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Set allowed extensions |
102
|
|
|
* |
103
|
|
|
* @param array|string $extensions |
104
|
|
|
* @param string|null $errorMessageTemplate |
105
|
|
|
* @return $this |
106
|
|
|
*/ |
107
|
|
|
public function allowExtensions($extensions, ?string $errorMessageTemplate = null): self |
108
|
|
|
{ |
109
|
|
|
if (!is_array($extensions)) { |
110
|
|
|
$extensions = [$extensions]; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
$constraint = new ExtensionConstraint($extensions); |
114
|
|
|
if (null !== $errorMessageTemplate) { |
115
|
|
|
$constraint->setErrorMessage($errorMessageTemplate); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
$this->constraints[] = $constraint; |
119
|
|
|
return $this; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Sets namer. |
124
|
|
|
* |
125
|
|
|
* @param Closure|NamerInterface $namer |
126
|
|
|
* @return $this |
127
|
|
|
*/ |
128
|
|
|
public function naming($namer): self |
129
|
|
|
{ |
130
|
|
|
if ($namer instanceof Closure) { |
131
|
|
|
$namer = new ClosureNamer($namer); |
132
|
|
|
} |
133
|
|
|
$this->namer = $namer; |
134
|
|
|
return $this; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Set filesystem. |
139
|
|
|
* |
140
|
|
|
* @param FilesystemInterface $filesystem |
141
|
|
|
* @return $this |
142
|
|
|
*/ |
143
|
|
|
public function setFilesystem(FilesystemInterface $filesystem): self |
144
|
|
|
{ |
145
|
|
|
$this->filesystem = $filesystem; |
146
|
|
|
return $this; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Sets save path |
151
|
|
|
* |
152
|
|
|
* @param string $path |
153
|
|
|
* @return $this |
154
|
|
|
*/ |
155
|
|
|
public function saveTo(string $path): self |
156
|
|
|
{ |
157
|
|
|
return $this->setFilesystem(new Local($path)); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Add a constraint |
162
|
|
|
* |
163
|
|
|
* @param ConstraintInterface $constraint |
164
|
|
|
* @return $this |
165
|
|
|
*/ |
166
|
|
|
public function addConstraint(ConstraintInterface $constraint): self |
167
|
|
|
{ |
168
|
|
|
$this->constraints[] = $constraint; |
169
|
|
|
return $this; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* Add to processor list |
174
|
|
|
* |
175
|
|
|
* @param Closure|ProcessorInterface $processor |
176
|
|
|
* @return $this |
177
|
|
|
*/ |
178
|
|
|
public function addProcessor($processor): self |
179
|
|
|
{ |
180
|
|
|
if ($processor instanceof Closure) { |
181
|
|
|
$processor = new ClosureProcessor($processor); |
182
|
|
|
} |
183
|
|
|
$this->processors[] = $processor; |
184
|
|
|
return $this; |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* Make upload handler |
189
|
|
|
* |
190
|
|
|
* @return UploadHandler |
191
|
|
|
*/ |
192
|
|
|
public function getHandler(): UploadHandler |
193
|
|
|
{ |
194
|
|
|
if (null === $this->namer) { |
195
|
|
|
$this->namer = new GenericNamer(); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
if (null === $this->filesystem) { |
199
|
|
|
throw new LogicException('You should set a filesystem for the builder.'); |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
return new UploadHandler($this->filesystem, $this->namer, |
203
|
|
|
new Validator($this->constraints), |
204
|
|
|
new ChainProcessor($this->processors), |
205
|
|
|
$this->overwrite |
206
|
|
|
); |
207
|
|
|
} |
208
|
|
|
} |
209
|
|
|
|