Address.php
5 years ago
CardInfo.php
5 years ago
DonorInfo.php
5 years ago
Money.php
5 years ago
ValueObjects.php
6 years ago
CardInfo.php
90 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Give\ValueObjects; |
| 4 | |
| 5 | use Give\Framework\Exceptions\Primitives\InvalidArgumentException; |
| 6 | |
| 7 | /** |
| 8 | * Class CardInfo |
| 9 | * |
| 10 | * @package Give\ValueObjects |
| 11 | * |
| 12 | * @since 2.7.0 |
| 13 | */ |
| 14 | class CardInfo implements ValueObjects { |
| 15 | /** |
| 16 | * Cardholder name. |
| 17 | * |
| 18 | * @var string |
| 19 | */ |
| 20 | public $name; |
| 21 | |
| 22 | /** |
| 23 | * Cardholder name. |
| 24 | * |
| 25 | * @var string |
| 26 | */ |
| 27 | public $number; |
| 28 | |
| 29 | /** |
| 30 | * Card security pin. |
| 31 | * |
| 32 | * @var string |
| 33 | */ |
| 34 | public $cvc; |
| 35 | |
| 36 | /** |
| 37 | * Card expire month |
| 38 | * |
| 39 | * @var string |
| 40 | */ |
| 41 | public $expMonth; |
| 42 | |
| 43 | /** |
| 44 | * Card expire year. |
| 45 | * |
| 46 | * @var string |
| 47 | */ |
| 48 | public $expYear; |
| 49 | |
| 50 | /** |
| 51 | * Address. |
| 52 | * |
| 53 | * @var Address |
| 54 | */ |
| 55 | public $address; |
| 56 | |
| 57 | |
| 58 | /** |
| 59 | * Take array and return object. |
| 60 | * |
| 61 | * @param $array |
| 62 | * |
| 63 | * @return CardInfo |
| 64 | */ |
| 65 | public static function fromArray( $array ) { |
| 66 | $expectedKeys = [ 'name', 'cvc', 'expMonth', 'expYear', 'number', 'address' ]; |
| 67 | |
| 68 | $array = array_intersect_key( $array, array_flip( $expectedKeys ) ); |
| 69 | |
| 70 | if ( empty( $array ) ) { |
| 71 | throw new InvalidArgumentException( |
| 72 | 'Invalid DonorInfo object, must have the exact following keys: ' . implode( ', ', $expectedKeys ) |
| 73 | ); |
| 74 | } |
| 75 | |
| 76 | // Cast array "address" to Give\ValueObjects\Address object. |
| 77 | if ( ! empty( $array['address'] ) ) { |
| 78 | $array['address'] = Address::fromArray( $array['address'] ); |
| 79 | } |
| 80 | |
| 81 | $cardInfo = new self(); |
| 82 | |
| 83 | foreach ( $array as $key => $value ) { |
| 84 | $cardInfo->{$key} = $value; |
| 85 | } |
| 86 | |
| 87 | return $cardInfo; |
| 88 | } |
| 89 | } |
| 90 |