SeatMapLayout   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 11
dl 0
loc 72
rs 10
c 1
b 0
f 0
wmc 8

3 Methods

Rating   Name   Duplication   Size   Complexity  
A passes() 0 33 6
A message() 0 3 1
A __construct() 0 2 1
1
<?php
2
3
namespace App\Rules;
4
5
use Illuminate\Contracts\Validation\Rule;
6
7
class SeatMapLayout implements Rule
8
{
9
    /**
10
     * Create a new rule instance.
11
     *
12
     * @return void
13
     */
14
    public function __construct()
15
    {
16
        //
17
    }
18
19
    /**
20
     * Determine if the validation rule passes.
21
     * Only allow json structures like...
22
     * 
23
     * [
24
     *      "aaaaaaa______aaaaaaa",
25
     *      "aaaaaaa______aaaaaaa",
26
     *      "aaaaaaa_____aaaaaaa",
27
     *      "aaaaaaa____aaaaaaa",
28
     *      "aaaaaaa___aaaaaaa",
29
     *      ...
30
     * ]
31
     *
32
     * @param  string  $attribute name of the validated field
33
     * @param  mixed  $value of the validated field
34
     * @return bool
35
     */
36
    public function passes($attribute, $value)
37
    {
38
        if($value === NULL || $value === "") {
39
            // it is allowed for the value to be empty
40
            return true;
41
        }
42
43
        // try to decode the json array with the expected format
44
        $rows = json_decode($value);
45
46
        // if json_decode result is falsy, then json object could
47
        // not be decoded and therefore the input is invalid
48
        if(!$rows) {
49
            return false;
50
        }
51
52
        // iterate over all rows to check that only valid
53
        // characters ('a' and '_') have been submitted
54
        foreach( $rows as $row ) {
55
            // explanation for regex ^[a_]*$
56
            //  ^     = Start of string
57
            //  [a_]  = Only characters "a" and "_" allowed
58
            //  *     = zero or more characters
59
            //  $     = End of string
60
            // Complete: Only match strings that contain from start to end only zero or more characters on the whitelist of a and _
61
            if( !preg_match('/^[a_]*$/', $row) ) {
62
                return false;
63
            }
64
        }
65
66
        // finally return true for a valid input,
67
        // since no previous check proved the contrary
68
        return true;
69
    }
70
71
    /**
72
     * Get the validation error message.
73
     *
74
     * @return string
75
     */
76
    public function message()
77
    {
78
        return 'The layout does not comply to the required format.';
79
    }
80
}
81