|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of Dimtrovich/Validation. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) 2023 Dimitri Sitchet Tomkeu <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view |
|
9
|
|
|
* the LICENSE file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Dimtrovich\Validation\Rules; |
|
13
|
|
|
|
|
14
|
|
|
use Dimtrovich\Validation\Traits\FileTrait; |
|
15
|
|
|
use Rakit\Validation\Rule; |
|
16
|
|
|
use Rakit\Validation\Rules\Mimes as RulesMimes; |
|
17
|
|
|
|
|
18
|
|
|
class Mimes extends AbstractRule |
|
19
|
|
|
{ |
|
20
|
|
|
use FileTrait; |
|
21
|
|
|
|
|
22
|
|
|
protected array $parameters = []; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* {@inheritDoc} |
|
26
|
|
|
*/ |
|
27
|
|
|
public function fillParameters(array $params): Rule |
|
28
|
|
|
{ |
|
29
|
4 |
|
return $this->fillAllowedValuesParameters($params); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* {@inheritDoc} |
|
34
|
|
|
*/ |
|
35
|
|
|
public function check($value): bool |
|
36
|
|
|
{ |
|
37
|
4 |
|
$this->requireParameters(['allowed_values']); |
|
38
|
4 |
|
$this->setAllowedValues($this->parameters = $this->parameter('allowed_values')); |
|
39
|
|
|
|
|
40
|
|
|
if (! $this->isValidFileInstance($value)) { |
|
41
|
4 |
|
$rule = new RulesMimes(); |
|
42
|
|
|
$rule->setAttribute($this->getAttribute()); |
|
|
|
|
|
|
43
|
|
|
$rule->setValidation($this->validation); |
|
|
|
|
|
|
44
|
|
|
$rule->setKey($this->getKey()); |
|
45
|
|
|
$rule->setParameters($this->getParameters()); |
|
46
|
|
|
$rule->setMessage($this->getMessage()); |
|
47
|
|
|
$rule->allowTypes($this->parameters); |
|
48
|
|
|
|
|
49
|
|
|
return $rule->check($value); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
if ($this->shouldBlockPhpUpload($value, $this->parameters)) { |
|
53
|
|
|
return false; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
4 |
|
return $value->getPath() !== '' && in_array($value->guessExtension(), $this->parameters, true); |
|
57
|
|
|
} |
|
58
|
|
|
} |
|
59
|
|
|
|