1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace LVR\CreditCard; |
4
|
|
|
|
5
|
|
|
use LVR\CreditCard\Cards\Jcb; |
6
|
|
|
use LVR\CreditCard\Cards\Visa; |
7
|
|
|
use LVR\CreditCard\Cards\Dankort; |
8
|
|
|
use LVR\CreditCard\Cards\Maestro; |
9
|
|
|
use LVR\CreditCard\Cards\UnionPay; |
10
|
|
|
use LVR\CreditCard\Cards\Discovery; |
11
|
|
|
use LVR\CreditCard\Cards\DinersClub; |
12
|
|
|
use LVR\CreditCard\Cards\Mastercard; |
13
|
|
|
use LVR\CreditCard\Cards\VisaElectron; |
14
|
|
|
use LVR\CreditCard\Cards\AmericanExpress; |
15
|
|
|
use LVR\CreditCard\Cards\Forbrugsforeningen; |
16
|
|
|
use LVR\CreditCard\Exceptions\CreditCardException; |
17
|
|
|
|
18
|
|
|
class Factory |
19
|
|
|
{ |
20
|
|
|
protected static $available_cards = [ |
21
|
|
|
// Firs debit cards |
22
|
|
|
Dankort::class, |
23
|
|
|
Forbrugsforeningen::class, |
24
|
|
|
Maestro::class, |
25
|
|
|
VisaElectron::class, |
26
|
|
|
// Debit cards |
27
|
|
|
AmericanExpress::class, |
28
|
|
|
DinersClub::class, |
29
|
|
|
Discovery::class, |
30
|
|
|
Jcb::class, |
31
|
|
|
Mastercard::class, |
32
|
|
|
UnionPay::class, |
33
|
|
|
Visa::class, |
34
|
|
|
]; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param string $card_number |
38
|
|
|
* |
39
|
|
|
* @return \LVR\CreditCard\Cards\Card |
40
|
|
|
* @throws \LVR\CreditCard\Exceptions\CreditCardException |
41
|
|
|
*/ |
42
|
45 |
|
public static function makeFromNumber(string $card_number) |
43
|
|
|
{ |
44
|
45 |
|
return self::determineCardByNumber($card_number); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param string $card_number |
49
|
|
|
* |
50
|
|
|
* @return mixed |
51
|
|
|
* @throws \LVR\CreditCard\Exceptions\CreditCardException |
52
|
|
|
*/ |
53
|
45 |
|
protected static function determineCardByNumber(string $card_number) |
54
|
|
|
{ |
55
|
45 |
|
foreach (self::$available_cards as $card) { |
56
|
45 |
|
if (preg_match($card::$pattern, $card_number)) { |
57
|
45 |
|
return new $card($card_number); |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|
61
|
12 |
|
throw new CreditCardException('Card not found.'); |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|