EanUtil   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 111
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 24
c 1
b 0
f 0
dl 0
loc 111
rs 10
wmc 17

8 Methods

Rating   Name   Duplication   Size   Complexity  
A toEight() 0 3 1
A fakeEan13() 0 4 1
A isValid() 0 7 5
A fakeEan8() 0 4 1
A isValidEight() 0 4 1
A toThirteen() 0 3 1
A isValidThirteen() 0 14 3
A fixLen() 0 8 4
1
<?php
2
3
namespace CelsoNery\EanUtils\Services\Traits;
4
5
/**
6
 * This is a Traits to Validate a EAN/GTIN
7
 */
8
trait EanUtil
9
{
10
    /**
11
     * Start Method to validate EAN/GTIN
12
     *
13
     * @param string $ean
14
     * @return bool
15
     */
16
    public function isValid(string $ean): bool
17
    {
18
        if (strlen($ean) < 8 || ((strlen($ean) > 8) && (strlen($ean) < 13))) {
19
            return false;
20
        }
21
22
        return (strlen($ean) == 8) ? $this->isValidEight($ean) : $this->isValidThirteen($ean);
23
    }
24
25
    /**
26
     * Method to validate EAN/GTIN 13
27
     *
28
     * @param string $ean
29
     * @return bool
30
     */
31
    private function isValidThirteen(string $ean): bool
32
    {
33
        if ((!preg_match("/^[0-9]{13}$/", $ean)) || (strlen($ean) < 13))
34
            return false;
35
36
        $digits = str_split($ean);
37
38
        $even = $digits[1] + $digits[3] + $digits[5] + $digits[7] + $digits[9] + $digits[11];
39
        $odd = $digits[0] + $digits[2] + $digits[4] + $digits[6] + $digits[8] + $digits[10];
40
41
        $result = $odd + $even * 3;
42
        $checkSum = 10 - $result % 10;
43
44
        return $checkSum == $digits[12];
45
46
    }
47
48
    /**
49
     * Method to validate EAN/GTIN 8
50
     *
51
     * @param string $ean
52
     * @return bool
53
     */
54
    private function isValidEight(string $ean): bool
55
    {
56
        $ean = $this->toThirteen($ean);
57
        return $this->isValidThirteen($ean);
58
    }
59
60
    /**
61
     * Method to generate EAN/GTIN 8 fake
62
     *
63
     * @return string
64
     */
65
    public function fakeEan8(): string
66
    {
67
        // TODO: Generate method to return a Ean8 fake
68
        return '';
69
    }
70
71
    /**
72
     * Method to generate EAN/GTIN 13 fake
73
     *
74
     * @return string
75
     */
76
    public function fakeEan13(): string
77
    {
78
        // TODO: Generate mehod to return a Ean13 fake
79
        return '';
80
    }
81
82
    /**
83
     * Method to fix length EAN/GTIN and complete with 0
84
     *
85
     * @param string $ean
86
     * @return string
87
     */
88
    protected function fixLen(string $ean): string
89
    {
90
        if (strlen($ean) < 8) {
91
            return $this->toEight($ean);
92
        } elseif (strlen($ean) > 8 && strlen($ean) < 13) {
93
            return $this->toThirteen($ean);
94
        } else {
95
            return '';
96
        }
97
    }
98
99
    /**
100
     * Method to fix length EAN/GTIN 8 and complete with 0
101
     *
102
     * @param string $ean
103
     * @return string
104
     */
105
    private function toEight(string $ean): string
106
    {
107
        return str_pad($ean, 8, '0', STR_PAD_LEFT);
108
    }
109
110
    /**
111
     * Method to fix length EAN/GTIN 13 and complete with 0
112
     *
113
     * @param string $ean
114
     * @return string
115
     */
116
    private function toThirteen(string $ean): string
117
    {
118
        return str_pad($ean, 13, '0', STR_PAD_LEFT);
119
    }
120
}
121