Created
March 14, 2013 11:26
-
-
Save jjok/5160617 to your computer and use it in GitHub Desktop.
Process regular expressions for postcode validation from Unicode Common Locale Data Repository.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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