@@ -10,62 +10,62 @@ |
||
| 10 | 10 | |
| 11 | 11 | class CountryURLProvider extends Object implements CountryURLProviderInterface |
| 12 | 12 | { |
| 13 | - private static $country_segments = array('nz', 'au', 'gb', 'eu', 'jp', 'us', 'zz'); |
|
| 13 | + private static $country_segments = array('nz', 'au', 'gb', 'eu', 'jp', 'us', 'zz'); |
|
| 14 | 14 | |
| 15 | - public function hasCountrySegment($url = ''){ |
|
| 15 | + public function hasCountrySegment($url = '') { |
|
| 16 | 16 | $url = $this->getDefaultURL($url); |
| 17 | 17 | $parsedUrl = parse_url($url); |
| 18 | 18 | $pathSegments = explode("/", $parsedUrl['path']); |
| 19 | 19 | $firstSegment = ''; |
| 20 | - $countries = Config::inst()->get('CountryURLProvider', 'country_segments'); |
|
| 21 | - foreach ($pathSegments as $position => $segment){ |
|
| 22 | - if($segment){ |
|
| 20 | + $countries = Config::inst()->get('CountryURLProvider', 'country_segments'); |
|
| 21 | + foreach ($pathSegments as $position => $segment) { |
|
| 22 | + if ($segment) { |
|
| 23 | 23 | $firstSegment = $segment; |
| 24 | 24 | break; |
| 25 | 25 | } |
| 26 | 26 | } |
| 27 | - if(in_array($firstSegment, $countries)){ |
|
| 27 | + if (in_array($firstSegment, $countries)) { |
|
| 28 | 28 | return true; |
| 29 | 29 | } |
| 30 | 30 | return false; |
| 31 | 31 | } |
| 32 | 32 | |
| 33 | - public function replaceCountryCodeInUrl($countryCode, $url = ''){ |
|
| 33 | + public function replaceCountryCodeInUrl($countryCode, $url = '') { |
|
| 34 | 34 | $url = $this->getDefaultURL($url); |
| 35 | 35 | $parsedUrl = parse_url($url); |
| 36 | 36 | $pathParts = explode('/', $parsedUrl['path']); |
| 37 | - $countries = Config::inst()->get('CountryURLProvider', 'country_segments'); |
|
| 38 | - foreach($pathParts as $pathPartsKey => $pathPart) { |
|
| 37 | + $countries = Config::inst()->get('CountryURLProvider', 'country_segments'); |
|
| 38 | + foreach ($pathParts as $pathPartsKey => $pathPart) { |
|
| 39 | 39 | //check for first match |
| 40 | - if(in_array($pathPart, $countries)) { |
|
| 40 | + if (in_array($pathPart, $countries)) { |
|
| 41 | 41 | $pathParts[$pathPartsKey] = strtolower($countryCode); |
| 42 | 42 | break; |
| 43 | 43 | } |
| 44 | 44 | } |
| 45 | 45 | $parsedUrl['path'] = implode('/', $pathParts); |
| 46 | - $url = $parsedUrl['scheme']. '://'. $parsedUrl['host']. $parsedUrl['path']; |
|
| 47 | - if(isset($parsedUrl['query'])){ |
|
| 48 | - $url = $url . $parsedUrl['query']; |
|
| 46 | + $url = $parsedUrl['scheme'].'://'.$parsedUrl['host'].$parsedUrl['path']; |
|
| 47 | + if (isset($parsedUrl['query'])) { |
|
| 48 | + $url = $url.$parsedUrl['query']; |
|
| 49 | 49 | } |
| 50 | 50 | return $url; |
| 51 | 51 | } |
| 52 | 52 | |
| 53 | - public function addCountryCodeToUrl($countryCode, $url = ''){ |
|
| 53 | + public function addCountryCodeToUrl($countryCode, $url = '') { |
|
| 54 | 54 | $url = $this->getDefaultURL($url); |
| 55 | 55 | $parsedUrl = parse_url($url); |
| 56 | - $url = $parsedUrl['scheme']. '://'. $parsedUrl['host']. '/'. strtolower($countryCode) . $parsedUrl['path']; |
|
| 57 | - if(isset($parsedUrl['query'])){ |
|
| 58 | - $url = $url . $parsedUrl['query']; |
|
| 56 | + $url = $parsedUrl['scheme'].'://'.$parsedUrl['host'].'/'.strtolower($countryCode).$parsedUrl['path']; |
|
| 57 | + if (isset($parsedUrl['query'])) { |
|
| 58 | + $url = $url.$parsedUrl['query']; |
|
| 59 | 59 | } |
| 60 | 60 | return $url; |
| 61 | 61 | } |
| 62 | 62 | |
| 63 | 63 | private function getDefaultURL($url = '') |
| 64 | 64 | { |
| 65 | - if($url) { |
|
| 65 | + if ($url) { |
|
| 66 | 66 | return Director::absoluteURL($url); |
| 67 | 67 | } |
| 68 | - return (isset($_SERVER['HTTPS']) ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]"; |
|
| 68 | + return (isset($_SERVER['HTTPS']) ? "https" : "http")."://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]"; |
|
| 69 | 69 | } |
| 70 | 70 | |
| 71 | 71 | } |