本文整理汇总了PHP中DataValidator::is_valid_iso_country_code方法的典型用法代码示例。如果您正苦于以下问题:PHP DataValidator::is_valid_iso_country_code方法的具体用法?PHP DataValidator::is_valid_iso_country_code怎么用?PHP DataValidator::is_valid_iso_country_code使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类DataValidator
的用法示例。
在下文中一共展示了DataValidator::is_valid_iso_country_code方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: setCountry
/**
* normalize helper function
* Setting the country correctly. Country is... kinda important.
* If we have no country, or nonsense, we try to get something rational
* through GeoIP lookup.
*/
protected function setCountry()
{
$regen = true;
$country = '';
if ($this->isSomething('country')) {
$country = strtoupper($this->getVal('country'));
if (DataValidator::is_valid_iso_country_code($country)) {
$regen = false;
} else {
//check to see if it's one of those other codes that comes out of CN, for the logs
//If this logs annoying quantities of nothing useful, go ahead and kill this whole else block later.
//we're still going to try to regen.
$near_countries = array('XX', 'EU', 'AP', 'A1', 'A2', 'O1');
if (!in_array($country, $near_countries)) {
$this->logger->warning(__FUNCTION__ . ": {$country} is not a country, or a recognized placeholder.");
}
}
} else {
$this->logger->warning(__FUNCTION__ . ': Country not set.');
}
//try to regenerate the country if we still don't have a valid one yet
if ($regen) {
// If no valid country was passed, try to do GeoIP lookup
// Requires php5-geoip package
if (function_exists('geoip_country_code_by_name')) {
$ip = $this->getVal('user_ip');
if (WmfFramework::validateIP($ip)) {
//I hate @suppression at least as much as you do, but this geoip function is being genuinely horrible.
//try/catch did not help me suppress the notice it emits when it can't find a host.
//The goggles; They do *nothing*.
// TODO: to change error_reporting is less worse?
$country = @geoip_country_code_by_name($ip);
if (!$country) {
$this->logger->warning(__FUNCTION__ . ": GeoIP lookup function found nothing for {$ip}! No country available.");
}
}
} else {
$this->logger->warning('GeoIP lookup function is missing! No country available.');
}
//still nothing good? Give up.
if (!DataValidator::is_valid_iso_country_code($country)) {
$country = 'XX';
}
}
if ($country != $this->getVal('country')) {
$this->setVal('country', $country);
}
}