public function __construct($source, GatewaysLoader $gateways_loader = null) { parent::__construct($source); // TODO: Change the autogenerated stub $this->gates_loader = $gateways_loader; }
private function expandZoneData(array $data, $prefix = '') { $out = array(); $out_props = array(); // get out all props from current level // // foreach ($data as $code => $value) { // $_code = str_replace(array('+', '-', '/', ' ', '(', ')'), '', $code); // // if (!is_numeric($_code)) { // $out_props[$code] = $value; // unset($data[$code]); // } // } foreach ($data as $code => $country) { $code = (string) $code; $code = str_replace(array('+', '-', '/', ' ', '(', ')'), '', $code); if (!is_numeric($code)) { $out_props[$code] = $country; continue; } // process numbers and ranges if (is_array($country)) { $country = $this->expandZoneData($country, $prefix . $code); } elseif ($country === '--') { $country = false; // not supported } elseif ($country === '++') { // support sub-zone files $_prefix = $prefix . $code; $country = parent::load($_prefix[0] . DIRECTORY_SEPARATOR . $_prefix, true); } elseif ($country === '==') { $country = array(); } elseif ($country) { // support short notation <dialing or area code or any other range> : <country alpha-2 code> $country = array('~' => array('geo' => array('country_alpha2' => $country), 'code' => array('country' => $prefix . $code))); } else { $country = false; // no country value was provided } if (strpos($code, '-')) { // codes range $_code = str_replace(' ', '', $code); $_code = explode('-', $_code); if (sizeof($_code) != 2) { throw new LoaderException("Invalid range in {$code}"); } list($start, $end) = $_code; $size = $end - $start + 1; // +1 because indexes are from 0 to 9 and we count from 1 to 9 // TODO: start and end should be in [0, 9], start < end, when range is [0,9] do nothing, just set props to current level if ($start > 9 || $end > 9) { throw new LoaderException("Invalid range in {$code} (start and end should be less then 10)"); } if ($size < 0) { throw new LoaderException("Invalid range in {$code} (start is equal or greater than end)"); } if ($size == 10) { // optimization: do not fill whole range with same data, set props on current level and expand data on the same level $out = $country; } else { $expanded = array_fill($start, $size, $country); $out = $this->joinSubZones($out, $expanded); } } else { // numeric code if (strlen($code) > 1) { // greater than 9, so contain more than one digit $pos = strlen($code) - 1; $expanded = array($code[$pos] => $country); for ($pos--; $pos >= 0; $pos--) { $expanded = array($code[$pos] => $expanded); } $sub = $expanded; } else { $sub = array($code => $country); } $out = $this->joinSubZones($out, $sub); } } ksort($out); if (!empty($out_props)) { ksort($out_props); $out['~'] = $out_props; } return $out; }