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