Address.php
6 years ago
CardInfo.php
6 years ago
DonorInfo.php
6 years ago
ValueObjects.php
6 years ago
DonorInfo.php
82 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Give\ValueObjects; |
| 4 | |
| 5 | use InvalidArgumentException; |
| 6 | |
| 7 | class DonorInfo implements ValueObjects { |
| 8 | /** |
| 9 | * WP user id. |
| 10 | * Donor can be connect to WP user. if connect and donating after login as WP user then it set to logged in user id. |
| 11 | * |
| 12 | * @var string |
| 13 | */ |
| 14 | public $wpUserId; |
| 15 | |
| 16 | /** |
| 17 | * Primary email. |
| 18 | * |
| 19 | * @var string |
| 20 | */ |
| 21 | public $email; |
| 22 | |
| 23 | /** |
| 24 | * Donor address. |
| 25 | * |
| 26 | * @var Address |
| 27 | */ |
| 28 | public $address; |
| 29 | |
| 30 | /** |
| 31 | * First name. |
| 32 | * |
| 33 | * @var string |
| 34 | */ |
| 35 | public $firstName; |
| 36 | |
| 37 | /** |
| 38 | * Last name. |
| 39 | * |
| 40 | * @var string |
| 41 | */ |
| 42 | public $lastName; |
| 43 | |
| 44 | /** |
| 45 | * Donor honorific. |
| 46 | * |
| 47 | * @var string |
| 48 | */ |
| 49 | public $honorific; |
| 50 | |
| 51 | /** |
| 52 | * Take array and return object. |
| 53 | * |
| 54 | * @param $array |
| 55 | * |
| 56 | * @return DonorInfo |
| 57 | */ |
| 58 | public static function fromArray( $array ) { |
| 59 | $expectedKeys = [ 'wpUserId', 'firstName', 'email', 'honorific', 'address' ]; |
| 60 | |
| 61 | $array = array_intersect_key( $array, array_flip( $expectedKeys ) ); |
| 62 | |
| 63 | if ( empty( $array ) ) { |
| 64 | throw new InvalidArgumentException( |
| 65 | 'Invalid DonorInfo object, must have the exact following keys: ' . implode( ', ', $expectedKeys ) |
| 66 | ); |
| 67 | } |
| 68 | |
| 69 | $donorInfo = new self(); |
| 70 | foreach ( $array as $key => $value ) { |
| 71 | $donorInfo->{$key} = $value; |
| 72 | } |
| 73 | |
| 74 | // Cast address to Give\ValueObjects\Address object. |
| 75 | if ( ! empty( $array['address'] ) ) { |
| 76 | $donorInfo->address = Address::fromArray( $array['address'] ); |
| 77 | } |
| 78 | |
| 79 | return $donorInfo; |
| 80 | } |
| 81 | } |
| 82 |