-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBatchResolver.php
51 lines (38 loc) · 1.2 KB
/
BatchResolver.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
<?php
/**
* Resolves payment card type in a batch.
*
* @package TheWebSolver\Codegarage\Validation
*/
declare( strict_types = 1 );
namespace TheWebSolver\Codegarage\PaymentCard\Traits;
use Generator;
use TheWebSolver\Codegarage\PaymentCard\PaymentCard;
use TheWebSolver\Codegarage\PaymentCard\CardInterface as Card;
trait BatchResolver {
/** @var array<string,string> */
private array $coveredCards;
/** @return array<string,string> */
private function getCoveredCards(): array {
return $this->coveredCards ?? array();
}
private function resetCoveredCards(): void {
$this->coveredCards = array();
}
private function resolveCardFromNumberIn( Generator $batch, string|int $number ): ?Card {
$shouldLoadNextCard = true;
$length = 0;
$matches = null;
do {
if ( ! ( $card = $batch->current() ) instanceof Card ) {
$shouldLoadNextCard = false;
continue;
}
PaymentCard::matchIdRange( $card, $number, $length, $matches );
$shouldLoadNextCard = ! $matches;
$this->coveredCards[ $card->getAlias() ] = ( $matches ? '' : 'in' ) . 'valid';
$batch->next();
} while ( $shouldLoadNextCard && $batch->valid() );
return $matches;
}
}