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