Code Coverage
 
Classes and Traits
Functions and Methods
Lines
Total
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
CRAP
100.00% covered (success)
100.00%
16 / 16
Base64Format
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
6
100.00% covered (success)
100.00%
16 / 16
 check
100.00% covered (success)
100.00%
1 / 1
6
100.00% covered (success)
100.00%
16 / 16
1<?php
2
3namespace Qmp\Laravel\ValidationRules\Rules;
4
5use Illuminate\Support\Facades\Validator;
6
7class Base64Format extends Rule
8{
9    /**
10     * Undocumented variable
11     *
12     * @var string
13     */
14    protected $message = 'The :attribute must have an encoded base64 value';
15
16    /**
17     * Undocumented function
18     *
19     * @param string $attribute
20     * @param mixed $value
21     * @param array $parameters
22     * @return bool
23     */
24    public function check(string $attribute, $value, array $parameters): bool
25    {
26        $this->setError('The format must be : ' . implode(', ', $parameters));
27
28        $validator = Validator::make(
29            ['value' => $value],
30            ['value' => 'base64']
31        );
32
33        if (!$validator->passes()) {
34            $errors = $validator->errors();
35            $this->setError(implode(', ',  $errors->all()));
36            return false;
37        }
38
39        $data = explode(';', $value)[0];
40
41        foreach ($parameters as $format) {
42            if (preg_match('/\/\*/', $format)) {
43                if (strpos(explode('/', $data)[0], explode('/', $format)[0]) !== false) {
44                    return true;
45                }
46            } else {
47                if (in_array(explode('/', $data)[1], $parameters)) {
48                    return true;
49                }
50            }
51        }
52
53        return false;
54    }
55}