Code Coverage
 
Classes and Traits
Functions and Methods
Lines
Total
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
20 / 20
CheckSiteDetailsImagesListRule
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
3 / 3
13
100.00% covered (success)
100.00%
20 / 20
 __construct
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
2 / 2
 passes
100.00% covered (success)
100.00%
1 / 1
11
100.00% covered (success)
100.00%
17 / 17
 message
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
1 / 1
1<?php
2
3namespace Qmp\Laravel\SiteGeneratorDetail\Rules;
4
5use Illuminate\Contracts\Validation\Rule;
6use Illuminate\Support\Facades\Validator;
7use Qmp\Laravel\ValidationRules\Rules\Base64Format;
8
9
10
11class CheckSiteDetailsImagesListRule implements Rule
12{
13    private $errors;
14
15    /**
16     * Create a new rule instance.
17     *
18     * @return void
19     */
20    public function __construct()
21    {
22        $this->errors = [];
23    }
24
25    /**
26     * Determine if the validation rule passes.
27     *
28     * @param  string  $attribute
29     * @param  mixed  $value
30     * @return bool
31     */
32    public function passes($attribute, $value)
33    {
34        foreach ($value as $image)
35        {
36            if(isset($image['active'])) {
37                if(!isset($image['name']) || !isset($image['dataURL']) || !preg_match('/\/[a-zA-Z_-]+\/[0-9a-z-A-Z_-]+.[a-z]{3}/', $image['dataURL']))
38                {
39                    $this->errors[] = 'An already uploaded files is not valid object ';
40                    return false;
41                }
42            }
43
44            if(isset($image['upload']) && isset($image['status']))
45            {
46                if(!isset($image['dataURL' ]) || !isset($image['upload']['filename'])){
47                    $this->errors[] = 'An uploaded files is not valid object ';
48
49                    return false;
50                }
51
52                $validator = Validator::make(
53                    ['dataURL' => $image['dataURL']],
54                    ['dataURL' => new Base64Format(['image/*'])]
55                );
56
57                if(!$validator->passes())
58                {
59                    $errors = $validator->errors();
60                    $this->errors = array_merge($this->errors, $errors->all());
61
62                    return false;
63                }
64            }
65        }
66
67        return true;
68    }
69
70    /**
71     * Get the validation error message.
72     *
73     * @return string
74     */
75    public function message()
76    {
77        return 'The :attribute contains at least one invalid object. ' . implode(' ', $this->errors);
78    }
79}