1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Usage: |
5
|
|
|
* $myAnswer = |
6
|
|
|
* CountryPrice_Translation::get_country_url_provider() |
7
|
|
|
* ->getSomething(); |
8
|
|
|
* |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
class CountryURLProvider extends Object implements CountryURLProviderInterface |
|
|
|
|
12
|
|
|
{ |
13
|
|
|
|
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* returns the selected country code if there is one ... |
17
|
|
|
* as an uppercase code, e.g. NZ |
18
|
|
|
* @param string|null $url |
19
|
|
|
* |
20
|
|
|
* @return bool |
21
|
|
|
*/ |
22
|
|
|
public function hasCountrySegment($url = '') |
23
|
|
|
{ |
24
|
|
|
return $this->CurrentCountrySegment($url) ? true : false; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* returns the selected country code if there is one ... |
29
|
|
|
* as an uppercase code, e.g. NZ |
30
|
|
|
* @param string|null $url |
31
|
|
|
* |
32
|
|
|
* @return string|null |
33
|
|
|
*/ |
34
|
|
|
public function CurrentCountrySegment($url = '') |
35
|
|
|
{ |
36
|
|
|
$url = $this->getCurrentURL($url); |
37
|
|
|
|
38
|
|
|
$parts = parse_url($url); |
39
|
|
|
if (isset($parts['path'])) { |
40
|
|
|
$path = trim($parts['path'], '/'); |
41
|
|
|
$array = explode('/', $path); |
42
|
|
|
$potentialCountry = isset($array[0]) ? trim($array[0]) : ''; |
43
|
|
|
if (strlen($potentialCountry) === 2) { |
44
|
|
|
$potentialCountry = strtoupper($potentialCountry); |
45
|
|
|
$check = EcommerceCountry::get()->filter(['Code' => $potentialCountry])->count(); |
46
|
|
|
if ($check == 1) { |
47
|
|
|
return $potentialCountry; |
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* replaces a country code in a URL with another one |
55
|
|
|
* |
56
|
|
|
* @param string $newCountryCode e.g. NZ / nz |
57
|
|
|
* @param string $url |
58
|
|
|
* |
59
|
|
|
* @return string|null only returns a string if it is different from the original! |
60
|
|
|
*/ |
61
|
|
|
public function replaceCountryCodeInUrl($newCountryCode, $url = '') |
62
|
|
|
{ |
63
|
|
|
$url = $this->getCurrentURL($url); |
64
|
|
|
|
65
|
|
|
$newCountryCode = strtolower($newCountryCode); |
66
|
|
|
$oldURL = $url; |
67
|
|
|
$parsedUrl = parse_url($url); |
68
|
|
|
if (isset($parsedUrl['path']) && isset($parsedUrl['host'])) { |
69
|
|
|
$path = $parsedUrl['path']; |
70
|
|
|
$path = trim($path, '/'); |
71
|
|
|
$pathParts = explode('/', $path); |
72
|
|
|
$currentCountryCode = $this->CurrentCountrySegment($url); |
|
|
|
|
73
|
|
|
if ($currentCountryCode) { |
|
|
|
|
74
|
|
|
$pathParts[0] = $newCountryCode; |
75
|
|
|
} else { |
76
|
|
|
array_unshift($pathParts, $newCountryCode); |
77
|
|
|
} |
78
|
|
|
$parsedUrl['path'] = implode('/', $pathParts); |
79
|
|
|
$newURL = |
80
|
|
|
$parsedUrl['scheme'] . |
81
|
|
|
'://' . |
82
|
|
|
Controller::join_links( |
83
|
|
|
$parsedUrl['host'], |
84
|
|
|
$parsedUrl['path'] |
85
|
|
|
); |
86
|
|
|
if (isset($parsedUrl['query'])) { |
87
|
|
|
$newURL = $newURL . '?' . $parsedUrl['query']; |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
if ($oldURL !== $newURL) { |
91
|
|
|
return $newURL; |
|
|
|
|
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
return ''; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* |
99
|
|
|
* @param string|null $url can be a relative one or nothing at all ... |
100
|
|
|
* |
101
|
|
|
* @return string full URL currently being called. |
|
|
|
|
102
|
|
|
*/ |
103
|
|
|
public function getCurrentURL($url = '') |
|
|
|
|
104
|
|
|
{ |
105
|
|
|
if ($url) { |
|
|
|
|
106
|
|
|
return Director::absoluteURL($url); |
107
|
|
|
} |
108
|
|
|
$protocol = Director::is_https() ? 'https://' : 'http://'; |
109
|
|
|
|
110
|
|
|
return $protocol.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']; |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.