Gtin   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 13
c 1
b 0
f 0
dl 0
loc 49
rs 10
wmc 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 2 1
A message() 0 3 1
A passes() 0 23 4
1
<?php declare(strict_types=1);
2
3
namespace JustSteveKing\GtinPHP\Rules;
4
5
use Illuminate\Contracts\Validation\Rule;
6
use JustSteveKing\GtinPHP\Gtin as GtinValidator;
7
8
class Gtin implements Rule
9
{
10
    protected string $message;
11
12
    public function __construct()
13
    {
14
        //
15
    }
16
17
    /**
18
     * Determine if the validation rule passes.
19
     *
20
     * @param  string  $attribute
21
     * @param  mixed  $value
22
     * @return bool
23
     */
24
    public function passes($attribute, $value)
25
    {
26
        // GTIN is between 8 and 14 characters long
27
        if (! GtinValidator::length($value)) {
28
            $this->message = 'The GTIN must be between 8 and 14 characters long';
29
30
            return false;
31
        }
32
33
        // GTIN is a number
34
        if (! GtinValidator::integer($value)) {
35
            $this->message = 'The GTIN must be an integer';
36
37
            return false;
38
        }
39
40
        if (! GtinValidator::inspect($value)) {
41
            $this->message = "The GTIN value {$value} does not pass algorithmic inspection.";
42
43
            return false;
44
        }
45
46
        return true;
47
    }
48
49
    /**
50
     * Get the validation error message.
51
     *
52
     * @return string
53
     */
54
    public function message()
55
    {
56
        return $this->message;
57
    }
58
}
59