1
1
<?php
2
- /**
3
- * Luhn Algorithm creator.
4
- *
5
- * @package TheWebSolver\Codegarage\Validation
6
- */
2
+ declare ( strict_types = 1 );
7
3
8
- declare ( strict_types = 1 );
9
-
10
- namespace TheWebSolver \Codegarage ;
4
+ namespace TheWebSolver \Codegarage ;
11
5
12
6
use LogicException ;
13
7
14
8
trait Luhn {
15
9
private const ALLOWED_PATTERN = '/[^0-9]/ ' ;
16
- private const EMPTY = 'REPRESENTS EMPTY CHECKSUM VALUE @ ' . self ::class ;
10
+ private const EMPTY_VALUE = 'REPRESENTS EMPTY CHECKSUM VALUE @Luhn ' ;
17
11
18
12
private bool $ needsDoubling = false ;
19
- private string $ digits = '0 ' ;
13
+ private string $ digits = '0 ' ;
20
14
private int $ checksum ;
21
15
private mixed $ raw ;
22
16
@@ -36,7 +30,7 @@ public function __toString(): string {
36
30
37
31
/** @throws LogicException When initialized without value or constructor value & invoked value mismatch. */
38
32
public function __invoke ( mixed $ data = null ): bool {
39
- return $ this ->ensureNumber ( $ data ?? self ::EMPTY )->isValid ();
33
+ return $ this ->ensureNumber ( $ data ?? self ::EMPTY_VALUE )->isValid ();
40
34
}
41
35
42
36
/**
@@ -59,7 +53,7 @@ public static function validate( mixed $value ): bool {
59
53
60
54
/** @throws LogicException When initialized without value. */
61
55
public function checksum (): int {
62
- return $ this ->checksum ??= $ this ->ensureNumber ( $ this ->raw ?? self ::EMPTY )->add ();
56
+ return $ this ->checksum ??= $ this ->ensureNumber ( $ this ->raw ?? self ::EMPTY_VALUE )->add ();
63
57
}
64
58
65
59
/** @throws LogicException When initialized without value. */
@@ -110,10 +104,10 @@ private function maybeDoubleAndAddDigits( int $value ): int {
110
104
111
105
private function ensureNumber ( mixed $ value ): static {
112
106
if ( ! isset ( $ this ->raw ) ) {
113
- return self ::EMPTY !== $ value ? $ this ->runAlgorithm ( $ value ) : $ this ->throw ( hasValue: false );
107
+ return self ::EMPTY_VALUE !== $ value ? $ this ->runAlgorithm ( $ value ) : $ this ->throw ( hasValue: false );
114
108
}
115
109
116
- return ( self ::EMPTY === $ value || $ this ->raw === $ value ) ? $ this : $ this ->throw ( hasValue: true );
110
+ return ( self ::EMPTY_VALUE === $ value || $ this ->raw === $ value ) ? $ this : $ this ->throw ( hasValue: true );
117
111
}
118
112
119
113
private function throw ( bool $ hasValue ): never {
0 commit comments