1 | <?php |
||
12 | class Uploadable implements Buildable, Delay |
||
13 | { |
||
14 | const MACRO_METHOD = 'uploadable'; |
||
15 | |||
16 | /** |
||
17 | * @var bool |
||
18 | */ |
||
19 | private $allowOverwrite = false; |
||
20 | |||
21 | /** |
||
22 | * @var bool |
||
23 | */ |
||
24 | private $appendNumber = false; |
||
25 | |||
26 | /** |
||
27 | * @var string |
||
28 | */ |
||
29 | private $path = ''; |
||
30 | |||
31 | /** |
||
32 | * @var string |
||
33 | */ |
||
34 | private $pathMethod = ''; |
||
35 | |||
36 | /** |
||
37 | * @var string |
||
38 | */ |
||
39 | private $callback = ''; |
||
40 | |||
41 | /** |
||
42 | * @var string |
||
43 | */ |
||
44 | private $filenameGenerator = Validator::FILENAME_GENERATOR_NONE; |
||
45 | |||
46 | /** |
||
47 | * @var int |
||
48 | */ |
||
49 | private $maxSize = 0; |
||
50 | |||
51 | /** |
||
52 | * @var string |
||
53 | */ |
||
54 | private $allowedTypes = ''; |
||
55 | |||
56 | /** |
||
57 | * @var string |
||
58 | */ |
||
59 | private $disallowedTypes = ''; |
||
60 | |||
61 | /** |
||
62 | * @var ExtensibleClassMetadata |
||
63 | */ |
||
64 | private $classMetadata; |
||
65 | |||
66 | /** |
||
67 | * @param ExtensibleClassMetadata $classMetadata |
||
68 | */ |
||
69 | 95 | public function __construct(ExtensibleClassMetadata $classMetadata) |
|
73 | |||
74 | /** |
||
75 | * Enable the Uploadable extension. |
||
76 | * |
||
77 | * @return void |
||
78 | */ |
||
79 | public static function enable() |
||
87 | |||
88 | /** |
||
89 | * If this option is true, it will overwrite a file if it already exists. If you set "false", |
||
90 | * an exception will be thrown. |
||
91 | * |
||
92 | * Default: false |
||
93 | * |
||
94 | * @return Uploadable |
||
95 | */ |
||
96 | 1 | public function allowOverwrite() |
|
102 | |||
103 | /** |
||
104 | * If the file already exists and "allowOverwrite" is false, append a number to the filename. |
||
105 | * |
||
106 | * Example: if you're uploading a file named "test.txt", if the file already exists and this option |
||
107 | * is true, the extension will modify the name of the uploaded file to "test-1.txt", where "1" could |
||
108 | * be any number. The extension will check if the file exists until it finds a filename with a number |
||
109 | * as its postfix that is not used. If you use a filename generator and this option is true, it will |
||
110 | * append a number to the filename anyway if a file with the same name already exists. |
||
111 | * |
||
112 | * Default value: false |
||
113 | * |
||
114 | * @return Uploadable |
||
115 | */ |
||
116 | 1 | public function appendNumber() |
|
122 | |||
123 | /** |
||
124 | * The path where the files represented by this entity will be moved. Path can be set in other ways: |
||
125 | * From the listener or from a method. |
||
126 | * |
||
127 | * Default: "". |
||
128 | * |
||
129 | * @param string $path |
||
130 | * |
||
131 | * @return Uploadable |
||
132 | */ |
||
133 | 1 | public function path($path) |
|
139 | |||
140 | /** |
||
141 | * Similar to "path", this represents the name of a method on the entity that will return the path |
||
142 | * where the files represented by this entity will be moved. This is useful in several cases. For |
||
143 | * example, you can set specific paths for specific entities, or you can get the path from other |
||
144 | * sources (like a framework configuration) instead of hard-coding it in the entity. |
||
145 | * |
||
146 | * As first argument this method takes default path, so you can return a path relative to the default. |
||
147 | * |
||
148 | * Default: "". |
||
149 | * |
||
150 | * @param string $methodName |
||
151 | * |
||
152 | * @return Uploadable |
||
153 | */ |
||
154 | 2 | public function pathMethod($methodName) |
|
160 | |||
161 | /** |
||
162 | * Allows you to set a method name. If set, the method will be called after the file is moved. |
||
163 | * |
||
164 | * This method will receive an array with information about the uploaded file, which includes the |
||
165 | * following keys: |
||
166 | * |
||
167 | * <ul> |
||
168 | * <li>fileName: The filename.</li> |
||
169 | * <li>fileExtension: The extension of the file (including the dot). Example: .jpg</li> |
||
170 | * <li>fileWithoutExt: The filename without the extension.</li> |
||
171 | * <li>filePath: The file path. Example: /my/path/filename.jpg</li> |
||
172 | * <li>fileMimeType: The mime-type of the file. Example: text/plain.</li> |
||
173 | * <li>fileSize: Size of the file in bytes. Example: 140000.</li> |
||
174 | * </ul> |
||
175 | * |
||
176 | * Default value: "". |
||
177 | * |
||
178 | * @param string $callback |
||
179 | * |
||
180 | * @return Uploadable |
||
181 | */ |
||
182 | 2 | public function callback($callback) |
|
188 | |||
189 | /** |
||
190 | * Normalizes the filename, leaving only alphanumeric characters in the filename, and replacing |
||
191 | * anything else with a "-". |
||
192 | * |
||
193 | * @return Uploadable |
||
194 | */ |
||
195 | 1 | public function alphanumericFilename() |
|
201 | |||
202 | /** |
||
203 | * Generates a sha1 filename for the file. |
||
204 | * |
||
205 | * @return Uploadable |
||
206 | */ |
||
207 | 1 | public function sha1Filename() |
|
213 | |||
214 | /** |
||
215 | * Set a custom FilenameGenerator class. This class must implement |
||
216 | * `Gedmo\Uploadable\FilenameGenerator\FilenameGeneratorInterface`. |
||
217 | * |
||
218 | * @param string $className |
||
219 | * |
||
220 | * @return Uploadable |
||
221 | */ |
||
222 | 1 | public function customFilename($className) |
|
228 | |||
229 | /** |
||
230 | * Set a maximum size for the file, in bytes. If file size exceeds the value set in this |
||
231 | * configuration, `UploadableMaxSizeException` will be thrown. |
||
232 | * |
||
233 | * Default value: 0, meaning that no size validation will occur. |
||
234 | * |
||
235 | * @param float $bytes |
||
236 | * |
||
237 | * @return Uploadable |
||
238 | */ |
||
239 | 2 | public function maxSize($bytes) |
|
245 | |||
246 | /** |
||
247 | * Allow only specific types. |
||
248 | * |
||
249 | * @param array|string ...$type can be an array or multiple string parameters |
||
250 | * |
||
251 | * @return Uploadable |
||
252 | */ |
||
253 | 4 | public function allow($type) |
|
262 | |||
263 | /** |
||
264 | * Disallow specific types |
||
265 | * |
||
266 | * @param array|string ...$type can be an array or multiple string parameters |
||
267 | * |
||
268 | * @return Uploadable |
||
269 | */ |
||
270 | 4 | public function disallow($type) |
|
279 | |||
280 | /** |
||
281 | * Execute the build process |
||
282 | */ |
||
283 | 94 | public function build() |
|
291 | |||
292 | /** |
||
293 | * Build the configuration, based on defaults, current extension configuration and accumulated parameters. |
||
294 | * |
||
295 | * @return array |
||
296 | */ |
||
297 | 94 | private function getConfiguration() |
|
321 | } |
||
322 |
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.