Hex   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 0
dl 0
loc 43
ccs 11
cts 11
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A passes() 0 12 2
A message() 0 4 1
1
<?php
2
3
namespace LVR\Colour;
4
5
use Illuminate\Contracts\Validation\Rule;
6
7
class Hex implements Rule
8
{
9
    /**
10
     * @var bool
11
     */
12
    protected $forceFull;
13
14 2
    public function __construct($forceFull = false)
15
    {
16 2
        $this->forceFull = $forceFull;
17 2
    }
18
19
    /**
20
     * Determine if the validation rule passes.
21
     *
22
     * @param  string $attribute
23
     * @param  mixed  $value
24
     *
25
     * @return bool
26
     */
27 2
    public function passes($attribute, $value)
28
    {
29 2
        $pattern = '/^#([a-fA-F0-9]{6}';
30
31 2
        if (!$this->forceFull) {
32 2
            $pattern .= '|[a-fA-F0-9]{3}';
33
        }
34
35 2
        $pattern .= ')$/';
36
37 2
        return (bool) preg_match($pattern, $value);
38
    }
39
40
    /**
41
     * Get the validation error message.
42
     *
43
     * @return string
44
     */
45 2
    public function message()
46
    {
47 2
        return 'The hex code is not valid';
48
    }
49
}
50