StartsWithMimeType   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 11
dl 0
loc 60
rs 10
c 0
b 0
f 0
wmc 5

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A message() 0 3 1
A passes() 0 7 2
A startsWithMimeType() 0 7 1
1
<?php
2
3
namespace Owowagency\LaravelMedia\Rules;
4
5
use Owowagency\LaravelMedia\Rules\Concerns\ValidatesBase64;
6
7
class StartsWithMimeType extends IsBase64
8
{
9
    use ValidatesBase64;
10
11
    /**
12
     * The mime type to validate.
13
     *
14
     * @var string
15
     */
16
    protected $mimeType;
17
18
    /**
19
     * StartsWithMimeType constructor.
20
     *
21
     * @param  string  $mimeType
22
     */
23
    public function __construct(string $mimeType)
24
    {
25
        $this->mimeType = $mimeType;
26
    }
27
28
    /**
29
     * Determine if the validation rule passes.
30
     *
31
     * @param  string  $attribute
32
     * @param  mixed  $value
33
     * @return bool
34
     */
35
    public function passes($attribute, $value)
36
    {
37
        if (! $this->isBase64($value)) {
38
            return false;
39
        }
40
41
        return $this->startsWithMimeType($value);
42
    }
43
44
    /**
45
     * Checks if base64 is image.
46
     *
47
     * @param  string  $value
48
     * @return bool
49
     */
50
    protected function startsWithMimeType(string $value): bool
51
    {
52
        $mimeType = $this->getMimeType($value);
53
54
        $exploded = explode('/', $mimeType);
55
56
        return ($exploded[0] == $this->mimeType);
57
    }
58
59
    /**
60
     * Get the validation error message.
61
     *
62
     * @return string
63
     */
64
    public function message(): string
65
    {
66
        return trans('validation.custom.is_base_64_type', ['type' => $this->mimeType]);
0 ignored issues
show
Bug Best Practice introduced by
The expression return trans('validation...e' => $this->mimeType)) 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...
67
    }
68
}
69