Mimetypes   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Test Coverage

Coverage 66.67%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 13
c 1
b 0
f 0
dl 0
loc 33
ccs 4
cts 6
cp 0.6667
rs 10
wmc 6

2 Methods

Rating   Name   Duplication   Size   Complexity  
A fillParameters() 0 3 1
A check() 0 16 5
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
17
class Mimetypes extends AbstractRule
18
{
19
    use FileTrait;
20
21
    protected array $parameters = [];
22
23
    /**
24
     * {@inheritDoc}
25
     */
26
    public function fillParameters(array $params): Rule
27
    {
28 2
        return $this->fillAllowedValuesParameters($params);
29
    }
30
31
    /**
32
     * {@inheritDoc}
33
     */
34
    public function check($value): bool
35
    {
36 2
        $this->requireParameters(['allowed_values']);
37 2
        $this->setAllowedValues($this->parameters = $this->parameter('allowed_values'));
38
39
        if (! $this->isValidFileInstance($value)) {
40
            return false;
41
        }
42
43
        if ($this->shouldBlockPhpUpload($value, $this->parameters)) {
44
            return false;
45
        }
46
47
        return $value->getPath() !== ''
48
                && (in_array($value->getMimeType(), $this->parameters, true)
49 2
                 || in_array(explode('/', $value->getMimeType())[0] . '/*', $this->parameters, true));
50
    }
51
}
52