Issues (13)

src/Rules/Base64Max.php (1 issue)

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