-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathShippingLocation.php
118 lines (103 loc) · 2.69 KB
/
ShippingLocation.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
<?php
declare( strict_types=1 );
namespace Automattic\WooCommerce\GoogleListingsAndAds\Shipping;
defined( 'ABSPATH' ) || exit;
/**
* Class ShippingLocation
*
* @package Automattic\WooCommerce\GoogleListingsAndAds\Shipping
*
* @since 2.1.0
*/
class ShippingLocation {
public const COUNTRY_AREA = 'country_area';
public const STATE_AREA = 'state_area';
public const POSTCODE_AREA = 'postcode_area';
/**
* @var int
*/
protected $google_id;
/**
* @var string
*/
protected $country;
/**
* @var string
*/
protected $state;
/**
* @var ShippingRegion
*/
protected $shipping_region;
/**
* ShippingLocation constructor.
*
* @param int $google_id
* @param string $country
* @param string|null $state
* @param ShippingRegion|null $shipping_region
*/
public function __construct( int $google_id, string $country, ?string $state = null, ShippingRegion $shipping_region = null ) {
$this->google_id = $google_id;
$this->country = $country;
$this->state = $state;
$this->shipping_region = $shipping_region;
}
/**
* @return int
*/
public function get_google_id(): int {
return $this->google_id;
}
/**
* @return string
*/
public function get_country(): string {
return $this->country;
}
/**
* @return string|null
*/
public function get_state(): ?string {
return $this->state;
}
/**
* @return ShippingRegion|null
*/
public function get_shipping_region(): ?ShippingRegion {
return $this->shipping_region;
}
/**
* Return the applicable shipping area for this shipping location. e.g. whether it applies to a whole country, state, or postcodes.
*
* @return string
*/
public function get_applicable_area(): string {
if ( ! empty( $this->get_shipping_region() ) ) {
// ShippingLocation applies to a select postal code ranges of a country
return self::POSTCODE_AREA;
} elseif ( ! empty( $this->get_state() ) ) {
// ShippingLocation applies to a state/province of a country
return self::STATE_AREA;
} else {
// ShippingLocation applies to a whole country
return self::COUNTRY_AREA;
}
}
/**
* Returns the string representation of this ShippingLocation.
*
* @return string
*/
public function __toString() {
$code = $this->get_country();
if ( ! empty( $this->get_shipping_region() ) ) {
// We assume that each postcode is unique within any supported country (a requirement set by Google API).
// Therefore, there is no need to include the state name in the location string even if it's provided.
$code .= '::' . $this->get_shipping_region();
} elseif ( ! empty( $this->get_state() ) ) {
$code .= '_' . $this->get_state();
}
return $code;
}
}