1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of Dimtrovich/Validation. |
5
|
|
|
* |
6
|
|
|
* (c) 2023 Dimitri Sitchet Tomkeu <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view |
9
|
|
|
* the LICENSE file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Dimtrovich\Validation\Rules; |
13
|
|
|
|
14
|
|
|
use Rakit\Validation\Rule; |
15
|
|
|
|
16
|
|
|
class Ean extends AbstractRule |
17
|
|
|
{ |
18
|
|
|
protected array $lengths = []; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* {@inheritDoc} |
22
|
|
|
*/ |
23
|
|
|
public function fillParameters(array $params): Rule |
24
|
|
|
{ |
25
|
|
|
if (empty($params)) { |
26
|
2 |
|
$params = [8, 13]; |
27
|
|
|
} |
28
|
|
|
|
29
|
6 |
|
$this->lengths = $params; |
30
|
|
|
|
31
|
6 |
|
return $this; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Check if the current value has the lenghts of EAN-8 or EAN-13 |
36
|
|
|
* |
37
|
|
|
* @credit <a href="https://github.com/Intervention/validation">Intervention/validation - \Intervention\Validation\Rules\Ean</a> |
38
|
|
|
* |
39
|
|
|
* @param mixed $value |
40
|
|
|
*/ |
41
|
|
|
public function check($value): bool |
42
|
|
|
{ |
43
|
4 |
|
return is_numeric($value) && $this->hasAllowedLength($value) && $this->checksumMatches($value); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Determine if the current value has the lenghts of EAN-8 or EAN-13 |
48
|
|
|
*/ |
49
|
|
|
public function hasAllowedLength(string $value): bool |
50
|
|
|
{ |
51
|
6 |
|
return in_array(strlen($value), $this->lengths, false); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Try to calculate the EAN checksum of the current value and check the matching. |
56
|
|
|
*/ |
57
|
|
|
protected function checksumMatches(string $value): bool |
58
|
|
|
{ |
59
|
6 |
|
return $this->calculateChecksum($value) === $this->cutChecksum($value); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Cut out the checksum of the current value and return |
64
|
|
|
*/ |
65
|
|
|
protected function cutChecksum(string $value): int |
66
|
|
|
{ |
67
|
6 |
|
return (int) (substr($value, -1)); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Calculate modulo checksum of given value |
72
|
|
|
*/ |
73
|
|
|
protected function calculateChecksum(string $value): int |
74
|
|
|
{ |
75
|
6 |
|
$checksum = 0; |
76
|
|
|
|
77
|
|
|
// chars without check digit in reverse |
78
|
6 |
|
$chars = array_reverse(str_split(substr($value, 0, -1))); |
|
|
|
|
79
|
|
|
|
80
|
|
|
foreach ($chars as $key => $char) { |
81
|
6 |
|
$multiplier = $key % 2 ? 1 : 3; |
82
|
6 |
|
$checksum += (int) $char * $multiplier; |
83
|
|
|
} |
84
|
|
|
|
85
|
6 |
|
$remainder = $checksum % 10; |
86
|
|
|
|
87
|
|
|
if ($remainder === 0) { |
88
|
6 |
|
return 0; |
89
|
|
|
} |
90
|
|
|
|
91
|
6 |
|
return 10 - $remainder; |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|