FileExtensionRule   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 11
c 1
b 0
f 0
dl 0
loc 31
rs 10
wmc 3

3 Methods

Rating   Name   Duplication   Size   Complexity  
A passes() 0 6 1
A __construct() 0 5 1
A message() 0 4 1
1
<?php
2
3
namespace Spatie\MediaLibraryPro\Rules;
4
5
use Illuminate\Contracts\Validation\Rule;
6
7
class FileExtensionRule implements Rule
8
{
9
    protected array $validExtensions = [];
10
11
    public function __construct(array $validExtensions = [])
12
    {
13
        $this->validExtensions = array_map(
14
            fn (string $extension) => strtolower($extension),
15
            $validExtensions,
16
        );
17
    }
18
19
    /**
20
     * @param string $attribute
21
     * @param \Illuminate\Http\UploadedFile $value
22
     *
23
     * @return bool
24
     */
25
    public function passes($attribute, $value): bool
26
    {
27
        return in_array(
28
            strtolower($value->getClientOriginalExtension()),
29
            $this->validExtensions,
30
            strict: true,
31
        );
32
    }
33
34
    public function message(): string
35
    {
36
        return trans('media-library::validation.mime', [
0 ignored issues
show
Bug Best Practice introduced by
The expression return trans('media-libr...his->validExtensions))) could return the type array which is incompatible with the type-hinted return string. Consider adding an additional type-check to rule them out.
Loading history...
37
            'mimes' => implode(', ', $this->validExtensions),
38
        ]);
39
    }
40
}
41