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