Base64Max::passes()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 2
dl 0
loc 15
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Owowagency\LaravelMedia\Rules;
4
5
use Illuminate\Support\Arr;
6
7
class Base64Max extends IsBase64
8
{
9
    /**
10
     * The size in kilobytes.
11
     *
12
     * @var float
13
     */
14
    protected $size;
15
16
    /**
17
     * The Base64Max constructor.
18
     *
19
     * @param  float  $size
20
     */
21
    public function __construct(float $size)
22
    {
23
        $this->size = $size;
24
    }
25
26
    /**
27
     * Determine if the validation rule passes.
28
     *
29
     * @param  string  $attribute
30
     * @param  mixed  $value
31
     * @return bool
32
     */
33
    public function passes($attribute, $value)
34
    {
35
        $values = Arr::wrap($value);
36
37
        // Before we can validate the size we need to make sure that all values
38
        // are a base64 string.
39
        if (! parent::passes($attribute, $values)) {
40
            return false;
41
        }
42
43
        $size = array_reduce($values, function ($carry, $value) {
44
            return $carry + $this->getSize($value);
45
        });
46
47
        return $size <= $this->size;
48
    }
49
50
    /**
51
     * Get the validation error message.
52
     *
53
     * @return string
54
     */
55
    public function message(): string
56
    {
57
        return trans('validation.max.file', [
0 ignored issues
show
Bug Best Practice introduced by
The expression return trans('validation...('max' => $this->size)) 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...
58
            'max' => $this->size,
59
        ]);
60
    }
61
}
62