Address.php
4 years ago
CardInfo.php
4 years ago
DonorInfo.php
4 years ago
Money.php
4 years ago
ValueObjects.php
4 years ago
Address.php
72 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Give\ValueObjects; |
| 4 | |
| 5 | use Give\Framework\Exceptions\Primitives\InvalidArgumentException; |
| 6 | |
| 7 | /** |
| 8 | * Class Address. |
| 9 | */ |
| 10 | class Address implements ValueObjects |
| 11 | { |
| 12 | |
| 13 | /** |
| 14 | * @var string |
| 15 | */ |
| 16 | public $line1; |
| 17 | |
| 18 | /** |
| 19 | * @var string |
| 20 | */ |
| 21 | public $line2; |
| 22 | |
| 23 | /** |
| 24 | * @var string |
| 25 | */ |
| 26 | public $city; |
| 27 | |
| 28 | /** |
| 29 | * @var string |
| 30 | */ |
| 31 | public $state; |
| 32 | |
| 33 | /** |
| 34 | * @var string |
| 35 | */ |
| 36 | public $postalCode; |
| 37 | |
| 38 | /** |
| 39 | * @var string |
| 40 | */ |
| 41 | public $country; |
| 42 | |
| 43 | /** |
| 44 | * Take array and return object. |
| 45 | * |
| 46 | * @since 2.7.0 |
| 47 | * |
| 48 | * @param array $array |
| 49 | * |
| 50 | * @return Address |
| 51 | */ |
| 52 | public static function fromArray($array) |
| 53 | { |
| 54 | $expectedKeys = ['line1', 'line2', 'city', 'state', 'postalCode', 'country']; |
| 55 | |
| 56 | $array = array_intersect_key($array, array_flip($expectedKeys)); |
| 57 | |
| 58 | if (empty($array)) { |
| 59 | throw new InvalidArgumentException( |
| 60 | 'Invalid Address object, must have the exact following keys: ' . implode(', ', $expectedKeys) |
| 61 | ); |
| 62 | } |
| 63 | |
| 64 | $address = new self(); |
| 65 | foreach ($array as $key => $value) { |
| 66 | $address->$key = $value; |
| 67 | } |
| 68 | |
| 69 | return $address; |
| 70 | } |
| 71 | } |
| 72 |