Passed
Pull Request — master (#8)
by Dees
07:53
created

BaseTypeRule::passes()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 2
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Owowagency\LaravelMedia\Rules;
4
5
use Owowagency\LaravelMedia\Rules\Concerns\ValidatesBase64;
6
7
abstract class BaseTypeRule extends IsBase64
8
{
9
    use ValidatesBase64;
10
11
    /**
12
     * The type to validate.
13
     *
14
     * @var string
15
     */
16
    protected $type;
17
18
    /**
19
     * StartsWithMimeType constructor.
20
     *
21
     * @param  string  $type
22
     */
23
    public function __construct(string $type)
24
    {
25
        $this->type = $type;
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->validateType($this->getMimeType($value));
42
    }
43
44
    /**
45
     * Checks if base64 has valid type.
46
     *
47
     * @param  string  $mimeType
48
     * @return bool
49
     */
50
    abstract protected function validateType(string $mimeType): bool;
51
52
    /**
53
     * Get the validation error message.
54
     *
55
     * @return string
56
     */
57
    public function message(): string
58
    {
59
        return trans('validation.custom.is_base_64_type', ['type' => $this->type]);
0 ignored issues
show
Bug Best Practice introduced by
The expression return trans('validation...'type' => $this->type)) 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...
60
    }
61
}
62