1 | <?php |
||
13 | class Handler implements UploadHandlerInterface |
||
14 | { |
||
15 | // constants for constructor options |
||
16 | const OPTION_PREFIX = 'prefix'; |
||
17 | const OPTION_OVERWRITE = 'overwrite'; |
||
18 | const OPTION_AUTOCONFIRM = 'autoconfirm'; |
||
19 | |||
20 | // constants for validation rules |
||
21 | const RULE_EXTENSION = 'extension'; |
||
22 | const RULE_SIZE = 'size'; |
||
23 | const RULE_IMAGE = 'image'; |
||
24 | const RULE_IMAGE_HEIGHT = 'imageheight'; |
||
25 | const RULE_IMAGE_WIDTH = 'imagewidth'; |
||
26 | const RULE_IMAGE_RATIO = 'imageratio'; |
||
27 | |||
28 | /** |
||
29 | * @var ContainerInterface |
||
30 | */ |
||
31 | protected $container; |
||
32 | |||
33 | /** |
||
34 | * Prefix to be added to the file. |
||
35 | * It can be a subfolder (if it ends with '/', a string to be used as prefix) |
||
36 | * or a callback that returns a string |
||
37 | * |
||
38 | * @var string|callback |
||
39 | */ |
||
40 | protected $prefix = ''; |
||
41 | |||
42 | /** |
||
43 | * When uploading a file that has the same name as a file that is |
||
44 | * already in the container should it overwrite it or use another name |
||
45 | * |
||
46 | * @var boolean |
||
47 | */ |
||
48 | protected $overwrite = false; |
||
49 | |||
50 | /** |
||
51 | * Whether or not the uploaded files are auto confirmed |
||
52 | * |
||
53 | * @var boolean |
||
54 | */ |
||
55 | protected $autoconfirm = false; |
||
56 | |||
57 | /** |
||
58 | * @var \Sirius\Validation\ValueValidator |
||
59 | */ |
||
60 | protected $validator; |
||
61 | |||
62 | /** |
||
63 | * @var function|callback |
||
64 | */ |
||
65 | protected $sanitizerCallback; |
||
66 | |||
67 | /** |
||
68 | * @param $directoryOrContainer |
||
69 | * @param array $options |
||
70 | * @param ValueValidator $validator |
||
71 | 15 | * @throws InvalidContainerException |
|
72 | */ |
||
73 | 15 | public function __construct($directoryOrContainer, $options = [], ValueValidator $validator = null) |
|
102 | |||
103 | /** |
||
104 | * Enable/disable upload overwrite |
||
105 | * |
||
106 | * @param bool $overwrite |
||
107 | 15 | * |
|
108 | * @return Handler |
||
109 | 15 | */ |
|
110 | public function setOverwrite($overwrite) |
||
116 | |||
117 | /** |
||
118 | * File prefix for the upload. Can be |
||
119 | * - a folder (if it ends with /) |
||
120 | * - a string to be used as prefix |
||
121 | * - a function that returns a string |
||
122 | * |
||
123 | 15 | * @param string|callable $prefix |
|
124 | * |
||
125 | 15 | * @return Handler |
|
126 | */ |
||
127 | 15 | public function setPrefix($prefix) |
|
133 | |||
134 | /** |
||
135 | * Enable/disable upload autoconfirmation |
||
136 | * Autoconfirmation does not require calling `confirm()` |
||
137 | 15 | * |
|
138 | * @param boolean $autoconfirm |
||
139 | 15 | * |
|
140 | * @return Handler |
||
141 | 15 | */ |
|
142 | public function setAutoconfirm($autoconfirm) |
||
148 | |||
149 | /** |
||
150 | * Set the sanitizer function for cleaning up the file names |
||
151 | 2 | * |
|
152 | * @param callable $callback |
||
153 | 2 | * |
|
154 | 1 | * @return Handler |
|
155 | * @throws \InvalidArgumentException |
||
156 | 1 | */ |
|
157 | 1 | public function setSanitizerCallback($callback) |
|
165 | |||
166 | /** |
||
167 | * Add validation rule (extension|size|width|height|ratio) |
||
168 | * |
||
169 | 2 | * @param string $name |
|
170 | * @param mixed $options |
||
171 | * @param string $errorMessageTemplate |
||
172 | 2 | * @param string $label |
|
173 | 2 | * |
|
174 | 2 | * @return Handler |
|
175 | 2 | */ |
|
176 | 2 | public function addRule($name, $options = null, $errorMessageTemplate = null, $label = null):Handler |
|
194 | 12 | ||
195 | /** |
||
196 | 12 | * Processes a file upload and returns an upload result file/collection |
|
197 | * |
||
198 | 12 | * @param array $files |
|
199 | 11 | * @return Result\Collection|Result\File|ResultInterface |
|
200 | 12 | */ |
|
201 | public function process($files = []):ResultInterface |
||
215 | |||
216 | /** |
||
217 | * Processes a single uploaded file |
||
218 | 11 | * - sanitize the name |
|
219 | * - validates the file |
||
220 | * - if valid, moves the file to the container |
||
221 | 11 | * |
|
222 | * @param array $file |
||
223 | * @return array |
||
224 | 11 | */ |
|
225 | protected function processSingleFile(array $file):array |
||
271 | 11 | ||
272 | /** |
||
273 | 11 | * Validates a file according to the rules configured on the handler |
|
274 | 2 | * |
|
275 | 2 | * @param $file |
|
276 | * @return mixed |
||
277 | 11 | */ |
|
278 | protected function validateFile($file) |
||
286 | |||
287 | 11 | /** |
|
288 | * Sanitize the name of the uploaded file by stripping away bad characters |
||
289 | 11 | * and replacing "invalid" characters with underscore _ |
|
290 | 1 | * |
|
291 | * @param string $name |
||
292 | 10 | * @return string |
|
293 | */ |
||
294 | protected function sanitizeFileName($name) |
||
301 | } |
||
302 |