|
| 1 | +<?php |
| 2 | +namespace Sipay\Paymethods; |
| 3 | + |
| 4 | +class Card implements Paymethod |
| 5 | +{ |
| 6 | + private $card_number; |
| 7 | + private $year; |
| 8 | + private $month; |
| 9 | + |
| 10 | + public function __construct(string $card_number, int $year, int $month) |
| 11 | + { |
| 12 | + $this->set_card_number($card_number); |
| 13 | + $this->set_expiration_date($year, $month); |
| 14 | + } |
| 15 | + |
| 16 | + public function get_card_number() |
| 17 | + { |
| 18 | + return $this->card_number; |
| 19 | + } |
| 20 | + |
| 21 | + public function set_card_number(string $card_number) |
| 22 | + { |
| 23 | + if(!preg_match("/^[0-9]{14,19}$/", $card_number)) { |
| 24 | + throw new \Exception('$card_number don\'t match with pattern.'); |
| 25 | + } |
| 26 | + |
| 27 | + $this->card_number = $card_number; |
| 28 | + } |
| 29 | + |
| 30 | + public function get_year() |
| 31 | + { |
| 32 | + return $this->year; |
| 33 | + } |
| 34 | + |
| 35 | + public function get_month() |
| 36 | + { |
| 37 | + return $this->month; |
| 38 | + } |
| 39 | + |
| 40 | + public function set_expiration_date(int $year, int $month) |
| 41 | + { |
| 42 | + if($year < 1000 or $year >9999) { |
| 43 | + throw new \Exception('$year doesn\'t have a correct value.'); |
| 44 | + } |
| 45 | + |
| 46 | + $this->year = $year; |
| 47 | + |
| 48 | + if($month < 1 or $month > 12) { |
| 49 | + throw new \Exception('$month doesn\'t have a correct value.'); |
| 50 | + } |
| 51 | + |
| 52 | + $this->month = $month; |
| 53 | + |
| 54 | + if($this->is_expirate()) { |
| 55 | + throw new \Exception('Card is expired.'); |
| 56 | + } |
| 57 | + |
| 58 | + } |
| 59 | + |
| 60 | + private function is_expirate() |
| 61 | + { |
| 62 | + $year = intval(date('Y')); |
| 63 | + $month = intval(date('m')); |
| 64 | + return $this->year < $year or ($this->year == $year and $this->month < $month); |
| 65 | + } |
| 66 | + |
| 67 | + public function to_json() |
| 68 | + { |
| 69 | + return array( |
| 70 | + 'pan' => $this->card_number, |
| 71 | + 'year' => $this->year, |
| 72 | + 'month' => $this->month |
| 73 | + ); |
| 74 | + } |
| 75 | +} |
0 commit comments