Skip to content

Commit

Permalink
Fix for invalid Canada postal code; closes benkeen#97
Browse files Browse the repository at this point in the history
  • Loading branch information
benkeen committed Jul 13, 2013
1 parent 36d52cf commit 29c4e45
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 14 deletions.
6 changes: 2 additions & 4 deletions plugins/countries/Canada/Canada.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,14 @@ class Country_Canada extends CountryPlugin {
protected $countrySlug = "canada";
protected $regionNames = "Provinces";
protected $zipFormat = array(
"format" => "AXC XDX",
"format" => "AXC XCX",
"replacements" => array(
"A" => "ABCEGHJKLMNPRSTVXY",
"X" => "123456789",
"C" => "ABCEGHJKLMNPRSTVWXYZ",
"D" => "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"C" => "ABCEGHJKLMNPRSTVWXYZ"
)
);
protected $zipFormatAdvanced = true;

protected $continent = "north_america";

public function install() {
Expand Down
45 changes: 35 additions & 10 deletions plugins/dataTypes/PostalZip/PostalZip.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public function generate($generator, $generationContextData) {
$randomZip = "";
if (empty($rowCountryInfo) && empty($rowRegionInfo)) {
$randCountry = $options[rand(0, count($options)-1)];
$randomZip = $this->convert($this->zipFormats[$randCountry]);
$randomZip = $this->convert($randCountry);
} else {
// if this country is one of the formats that was selected, generate it in that format -
// otherwise just generate a zip in any selected format
Expand All @@ -62,10 +62,10 @@ public function generate($generator, $generationContextData) {
$countrySlug = $rowRegionInfo["randomData"]["country_slug"];
}
if (in_array($countrySlug, $options)) {
$randomZip = $this->convert($this->zipFormats[$countrySlug]);
$randomZip = $this->convert($countrySlug);
} else {
$randCountry = $options[rand(0, count($options)-1)];
$randomZip = $this->convert($this->zipFormats[$randCountry]);
$randomZip = $this->convert($randCountry);
}
}
return array(
Expand Down Expand Up @@ -114,22 +114,47 @@ private function initZipFormats() {
foreach ($countryPlugins as $countryInfo) {
$countrySlug = $countryInfo->getSlug();
$zipFormat = $countryInfo->getZipFormat();
$formats[$countrySlug] = $zipFormat;
$zipFormatAdvanced = $countryInfo->isZipFormatAdvanced();
$formats[$countrySlug] = array(
"format" => $zipFormat,
"isAdvanced"=> $zipFormatAdvanced
);
}

$this->zipFormats = $formats;
}


private function convert($str) {
$formats = explode("|", $str);
if (count($formats) == 1) {
$format = $formats[0];
private function convert($randCountry) {
$zipInfo = $this->zipFormats[$randCountry];

$result = "";
if ($zipInfo["isAdvanced"]) {
$customFormat = $zipInfo["format"]["format"];
$replacements = $zipInfo["format"]["replacements"];

// now iterate over $customFormat and do whatever replacements have been specified
for ($i=0; $i<strlen($customFormat); $i++) {
if (array_key_exists($customFormat[$i], $replacements)) {
$replacementKey = $replacements[$customFormat[$i]];
$randChar = $replacementKey[rand(0, strlen($replacementKey)-1)];
$result .= $randChar;
} else {
$result .= $customFormat[$i];
}
}

} else {
$format = $formats[rand(0, count($formats)-1)];
$formats = explode("|", $zipInfo["format"]);
if (count($formats) == 1) {
$format = $formats[0];
} else {
$format = $formats[rand(0, count($formats)-1)];
}
$result = Utils::generateRandomAlphanumericStr($format);
}

return Utils::generateRandomAlphanumericStr($format);
return $result;
}


Expand Down

0 comments on commit 29c4e45

Please sign in to comment.