Skip to content

Instantly share code, notes, and snippets.

@jjok
Created March 14, 2013 11:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jjok/5160617 to your computer and use it in GitHub Desktop.
Save jjok/5160617 to your computer and use it in GitHub Desktop.
Process regular expressions for postcode validation from Unicode Common Locale Data Repository.
<?php
namespace SuperGroup\PostcodeRegexImporter;
use Closure;
use DOMDocument;
/**
* Processes regular expressions for postcodes from CLDR (Unicode Common
* Locale Data Repository) XML file.
*
* <p>The file is located at core\common\supplemental\postalCodeData.xml in the zip file.</p>
*
* <code>
* $xml = new DOMDocument();
* $xml->load('postalCodeData.xml');
* $importer = new SuperGroup\PostcodeRegexImporter\PostcodeRegexImporter($xml);
* $importer->import(function($country_code, $regex) {
* echo "$country_code $regex\n";
* });
* </code>
*
* @author Jonathan Jefferies
* @see http://cldr.unicode.org/index/downloads
*/
class PostcodeRegexImporter {
/**
* The postalCodeData.xml file.
* @var DOMDocument
*/
protected $file;
/**
* Set the XML file to be imported.
* @param DOMDocument $file
*/
public function __construct(DOMDocument $file) {
$this->file = $file;
}
/**
* Get each regex and country code pair and pass to callback function.
* @param Closure $callback A callback function to process each regex.
*/
public function import(Closure $callback) {
foreach($this->file->getElementsByTagName('postCodeRegex') as $node) {
$callback($node->getAttribute('territoryId'), $node->nodeValue);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment