diff --git a/autoloader/index/0.php b/autoloader/index/0.php index 3f6e1230..04a3adcf 100644 --- a/autoloader/index/0.php +++ b/autoloader/index/0.php @@ -247,6 +247,6 @@ 'malkusch\bav\undefinedattributeagencyexception' => '../classes/bank/exception/UndefinedAttributeAgencyException.php', 'malkusch\bav\undefinedfileparsercontextexception' => '../classes/dataBackend/file/fileParser/exception/UndefinedFileParserContextException.php', 'malkusch\bav\missingattributesdatabackendioexception' => '../classes/dataBackend/exception/MissingAttributesDataBackendIOException.php', - 'malkusch\bav\fileparserioexception' => '../classes/dataBackend/file/fileParser/exception/FileParserIOException', - 'malkusch\bav\parseexception' => '../classes/dataBackend/file/fileParser/exception/ParseException', + 'malkusch\bav\fileparserioexception' => '../classes/dataBackend/file/fileParser/exception/FileParserIOException.php', + 'malkusch\bav\parseexception' => '../classes/dataBackend/file/fileParser/exception/ParseException.php', ); diff --git a/classes/dataBackend/file/FileDataBackend.php b/classes/dataBackend/file/FileDataBackend.php index 28f1e0a0..0cbfcab9 100644 --- a/classes/dataBackend/file/FileDataBackend.php +++ b/classes/dataBackend/file/FileDataBackend.php @@ -2,8 +2,11 @@ namespace malkusch\bav; +use LogicException; +use malkusch\index\FileExistsIOException; use \malkusch\index\FixedSizeIndex; use \malkusch\index\IndexException; +use malkusch\index\IOIndexException; /** * It uses the huge file from the Bundesbank and uses a binary search to find a row. @@ -49,9 +52,11 @@ public function __construct($file = null) $this->parser = new FileParser($file); $this->fileUtil = new FileUtil(); } - + /** * @return FixedSizeIndex + * @throws FileExistsIOException + * @throws IOIndexException */ private function getIndex() { @@ -127,6 +132,18 @@ private function sortFile($file) $this->fileUtil->safeRename($temp, $file); } + /** + * Since 2019-03 the file is encoded in UTF-8. Convert the encoding to ISO-8859-1 so the file parser works with fixed line lengths. + * + * @param $file + * @throws EncodingException + * @throws UnsupportedEncodingException + */ + private function reEncodeFile($file) { + $contents = file_get_contents($file); + file_put_contents($file, utf8_decode($contents)); + } + /** * @see DataBackend::uninstall() * @throws DataBackendIOException @@ -140,8 +157,16 @@ public function uninstall() } /** - * @see DataBackend::install() * @throws DataBackendIOException + * @throws DownloaderException + * @throws EncodingException + * @throws FileException + * @throws FileParserIOException + * @throws FileParserNotExistsException + * @throws FileValidatorException + * @throws URIPickerException + * @throws UnsupportedEncodingException + * @see DataBackend::install() */ public function install() { @@ -151,10 +176,16 @@ public function install() /** * This method works only if your PHP is compiled with cURL. * - * @see DataBackend::update() * @throws DataBackendIOException - * @throws FileException * @throws DownloaderException + * @throws EncodingException + * @throws FileException + * @throws FileParserIOException + * @throws FileParserNotExistsException + * @throws FileValidatorException + * @throws URIPickerException + * @throws UnsupportedEncodingException + * @see DataBackend::update() */ public function update() { @@ -188,6 +219,9 @@ public function update() // download file $file = $downloader->downloadFile($url); + // convert encoding from UTF-8 to ISO-8859-15 (as needed by file parser) + $this->reEncodeFile($file); + // Validate file format. $validator = new FileValidator(); $validator->validate($file); @@ -205,9 +239,10 @@ public function update() } /** - * @throws DataBackendIOException - * @throws DataBackendException * @return Bank[] + * @throws DataBackendException + * @throws DataBackendIOException + * @throws EncodingException * @see DataBackend::getAllBanks() */ public function getAllBanks() @@ -235,11 +270,11 @@ public function getAllBanks() } /** - * @throws DataBackendIOException - * @throws BankNotFoundException * @param String $bankID - * @see DataBackend::getNewBank() * @return Bank + * @throws EncodingException + * @throws DataBackendIOException + * @throws BankNotFoundException*@see DataBackend::getNewBank() */ public function getNewBank($bankID) { @@ -266,10 +301,14 @@ public function getNewBank($bankID) } /** - * @see DataBackend::getMainAgency() + * @param Bank $bank + * @return Agency * @throws DataBackendException + * @throws DataBackendIOException + * @throws EncodingException + * @throws FileParserNotExistsException * @throws NoMainAgencyException - * @return Agency + * @see DataBackend::getMainAgency() */ public function getMainAgency(Bank $bank) { @@ -286,7 +325,7 @@ public function getMainAgency(Bank $bank) throw new NoMainAgencyException($bank); } catch (UndefinedFileParserContextException $e) { - throw new \LogicException("Start and end should be defined."); + throw new LogicException("Start and end should be defined."); } catch (FileParserIOException $e) { throw new DataBackendIOException("Parser Exception at bank {$bank->getBankID()}"); @@ -298,10 +337,13 @@ public function getMainAgency(Bank $bank) } /** - * @see DataBackend::getAgenciesForBank() - * @throws DataBackendIOException - * @throws DataBackendException + * @param Bank $bank * @return Agency[] + * @throws DataBackendException + * @throws DataBackendIOException + * @throws EncodingException + * @throws FileParserNotExistsException + * @see DataBackend::getAgenciesForBank() */ public function getAgenciesForBank(Bank $bank) { @@ -318,7 +360,7 @@ public function getAgenciesForBank(Bank $bank) return $agencies; } catch (UndefinedFileParserContextException $e) { - throw new \LogicException("Start and end should be defined."); + throw new LogicException("Start and end should be defined."); } catch (FileParserIOException $e) { throw new DataBackendIOException(); @@ -330,12 +372,16 @@ public function getAgenciesForBank(Bank $bank) } /** + * @param $bankID * @return FileParserContext + * @throws EncodingException + * @throws FileParserIOException + * @throws FileParserNotExistsException */ private function defineContextInterval($bankID) { if (! isset($this->contextCache[$bankID])) { - throw new \LogicException("The contextCache object should exist!"); + throw new LogicException("The contextCache object should exist!"); } $context = $this->contextCache[$bankID]; @@ -400,9 +446,13 @@ public function isInstalled() /** * Returns bank agencies for a given BIC. * - * @todo This method is inefficient. Add index based implementation. * @param string $bic BIC * @return Agency[] + * @throws DataBackendException + * @throws DataBackendIOException + * @throws EncodingException + * @throws UndefinedAttributeAgencyException + * @todo This method is inefficient. Add index based implementation. */ public function getBICAgencies($bic) { diff --git a/classes/dataBackend/file/download/Downloader.php b/classes/dataBackend/file/download/Downloader.php index 283c5e27..055979aa 100644 --- a/classes/dataBackend/file/download/Downloader.php +++ b/classes/dataBackend/file/download/Downloader.php @@ -30,6 +30,7 @@ public function __construct() } curl_setopt($this->handle, CURLOPT_RETURNTRANSFER, true); + curl_setopt($this->handle, CURLOPT_FOLLOWLOCATION, true); } /** diff --git a/classes/dataBackend/file/fileParser/FileParser.php b/classes/dataBackend/file/fileParser/FileParser.php index 03054f5d..bd3e925d 100644 --- a/classes/dataBackend/file/fileParser/FileParser.php +++ b/classes/dataBackend/file/fileParser/FileParser.php @@ -134,7 +134,6 @@ private function init() } - $dummyLine = fgets($this->fp); if (! $dummyLine) { throw new FileParserIOException(); @@ -193,10 +192,11 @@ public function seekLine($line, $offset = 0) } /** - * @throws FileParserIOException - * @throws FileParserNotExistsException * @param int $line * @return string + * @throws EncodingException + * @throws FileParserIOException + * @throws FileParserNotExistsException */ public function readLine($line) { @@ -205,10 +205,11 @@ public function readLine($line) } /** - * @throws FileParserIOException - * @throws FileParserNotExistsException * @param int $line * @return string + * @throws EncodingException + * @throws FileParserIOException + * @throws FileParserNotExistsException */ public function getBankID($line) { @@ -249,9 +250,10 @@ public function __destruct() } /** - * @throws ParseException + * @param DataBackend $dataBackend * @param string $line * @return Bank + * @throws ParseException */ public function getBank(DataBackend $dataBackend, $line) { @@ -265,9 +267,10 @@ public function getBank(DataBackend $dataBackend, $line) } /** - * @throws ParseException + * @param Bank $bank * @param string $line * @return Agency + * @throws ParseException */ public function getAgency(Bank $bank, $line) { diff --git a/classes/dataBackend/file/fileParser/exception/FileParserIOException b/classes/dataBackend/file/fileParser/exception/FileParserIOException.php similarity index 100% rename from classes/dataBackend/file/fileParser/exception/FileParserIOException rename to classes/dataBackend/file/fileParser/exception/FileParserIOException.php diff --git a/classes/dataBackend/file/fileParser/exception/ParseException b/classes/dataBackend/file/fileParser/exception/ParseException.php similarity index 100% rename from classes/dataBackend/file/fileParser/exception/ParseException rename to classes/dataBackend/file/fileParser/exception/ParseException.php