Beispiel #1
0
 * This script creates an array of currencies from a trusted source.
 * The result of the script is exported in PHP format to be used by the library at runtime.
 *
 * This script is meant to be run by project maintainers, on a regular basis.
 */
$data = file_get_contents('http://www.currency-iso.org/dam/downloads/table_a1.xml');
$document = new DOMDocument();
$document->loadXML($data);
$countries = $document->getElementsByTagName('CcyNtry');
$result = [];
foreach ($countries as $country) {
    /** @var DOMElement $country */
    $name = getDomElementString($country, 'CcyNm');
    $currencyCode = getDomElementString($country, 'Ccy');
    $numericCode = getDomElementString($country, 'CcyNbr');
    $minorUnits = getDomElementString($country, 'CcyMnrUnts');
    if ($name === null || $currencyCode === null && $numericCode === null && $minorUnits == null) {
        continue;
    }
    if ($minorUnits == 'N.A.') {
        continue;
    }
    $name = checkName($name);
    $currencyCode = checkCurrencyCode($currencyCode);
    $numericCode = checkNumericCode($numericCode);
    $minorUnits = checkMinorUnits($minorUnits);
    $value = [$currencyCode, $numericCode, $name, $minorUnits];
    if (isset($result[$currencyCode])) {
        if ($result[$currencyCode] !== $value) {
            throw new \RuntimeException('Inconsistent values found for currency code ' . $currencyCode);
        }
Beispiel #2
0
<?php

$data = file_get_contents('http://www.currency-iso.org/dam/downloads/table_a1.xml');
$document = new DOMDocument();
$document->loadXML($data);
$countries = $document->getElementsByTagName('CcyNtry');
$countryToCurrency = [];
foreach ($countries as $country) {
    /** @var DOMElement $country */
    $countryName = getDomElementString($country, 'CtryNm');
    $currencyCode = getDomElementString($country, 'Ccy');
    if ($currencyCode !== null && preg_match('/^[A-Z]{3}$/', $currencyCode) == 0) {
        throw new \RuntimeException('Invalid currency code: ' . $currencyCode);
    }
    $countryToCurrency[$countryName] = $currencyCode;
}
file_put_contents('country-to-currency.php', sprintf("<?php return %s;\n", var_export($countryToCurrency, true)));
/**
 * @param DOMElement $element
 * @param string     $name
 *
 * @return string|null
 */
function getDomElementString(DOMElement $element, $name)
{
    foreach ($element->getElementsByTagName($name) as $child) {
        /** @var $child DOMElement */
        return $child->textContent;
    }
    return null;
}