From 875243a55d5f5426e22a8c76d588f8059e2a10c9 Mon Sep 17 00:00:00 2001 From: HorstOeko Date: Thu, 16 May 2024 06:15:09 +0200 Subject: [PATCH] ZugferdMapper -> Completed JSON Loading and Saving --- src/ZugferdMapper.php | 48 +++++++++++++++++++++++++++++++++- tests/testcases/MapperTest.php | 43 +++++++++++++++++++++++++++++- 2 files changed, 89 insertions(+), 2 deletions(-) diff --git a/src/ZugferdMapper.php b/src/ZugferdMapper.php index 2f79e98..b994055 100644 --- a/src/ZugferdMapper.php +++ b/src/ZugferdMapper.php @@ -65,7 +65,8 @@ class ZugferdMapper protected const KEY_TOCODE = "tocode"; /** - * Load mappings from JSON string + * Load mappings from JSON string. This performs a validation before loading + * the json to internal mapping storage * * @param string $json * @return boolean @@ -77,6 +78,51 @@ public static function loadFromJson(string $json): bool if (!is_array($jsonDecoded)) { return false; } + + foreach (static::getSupportedMappingAreas() as $meppingArea) { + if (!isset($jsonDecoded[$meppingArea])) { + return false; + } + if (!is_array($jsonDecoded[$meppingArea])) { + return false; + } + foreach ($jsonDecoded[$meppingArea] as $mappingItem) { + if (!isset($mappingItem[static::KEY_DIRECTION])) { + return false; + } + if (!isset($mappingItem[static::KEY_FROMCODE])) { + return false; + } + if (!isset($mappingItem[static::KEY_TOCODE])) { + return false; + } + } + } + + static::$mappings = $jsonDecoded; + + return true; + } + + /** + * Load mappings from JSON file + * + * @param string $filename + * @return boolean + */ + public static function loadFromJsonFile(string $filename): bool + { + if (!file_exists($filename)) { + return false; + } + + $json = file_get_contents($filename); + + if ($json === false) { + return false; + } + + return static::loadFromJson($json); } /** diff --git a/tests/testcases/MapperTest.php b/tests/testcases/MapperTest.php index 48ad888..4f20f20 100644 --- a/tests/testcases/MapperTest.php +++ b/tests/testcases/MapperTest.php @@ -184,6 +184,7 @@ public function testSaveToJsonFile(): void { $filename = getcwd() . "/mappings.json"; + ZugferdMapper::clearAllMappings(); ZugferdMapper::addCurrencyMappingIncoming('EUR', 'EUR'); ZugferdMapper::addCurrencyMappingIncoming('DEM', 'DM'); ZugferdMapper::addCurrencyMappingOutgoing('EUR', 'EUR'); @@ -191,8 +192,48 @@ public function testSaveToJsonFile(): void ZugferdMapper::addUnitCodeMappingIncoming('C62', 'STK'); ZugferdMapper::addUnitCodeMappingOutgoing('STK', 'C62'); - ZugferdMapper::saveToJsonFile($filename); + $this->assertTrue(ZugferdMapper::saveToJsonFile($filename)); + $this->assertTrue(file_exists($filename)); + + @unlink($filename); + } + + public function testLoadFromJsonFile(): void + { + $filename = getcwd() . "/mappings.json"; + + ZugferdMapper::clearAllMappings(); + ZugferdMapper::addCurrencyMappingIncoming('EUR', 'EUR'); + ZugferdMapper::addCurrencyMappingIncoming('DEM', 'DM'); + ZugferdMapper::addCurrencyMappingOutgoing('EUR', 'EUR'); + ZugferdMapper::addCurrencyMappingOutgoing('DM', 'DEM'); + ZugferdMapper::addUnitCodeMappingIncoming('C62', 'STK'); + ZugferdMapper::addUnitCodeMappingOutgoing('STK', 'C62'); + $this->assertTrue(ZugferdMapper::saveToJsonFile($filename)); $this->assertTrue(file_exists($filename)); + + ZugferdMapper::clearAllMappings(); + + $this->assertNotEquals('DM', ZugferdMapper::getMapping(ZugferdMapper::DIRECTION_INCOMING, ZugferdMapper::MAPPING_AREA_CURRENCY, 'DEM')); + $this->assertNotEquals('DEM', ZugferdMapper::getMapping(ZugferdMapper::DIRECTION_OUTGOING, ZugferdMapper::MAPPING_AREA_CURRENCY, 'DM')); + $this->assertNotEquals('STK', ZugferdMapper::getMapping(ZugferdMapper::DIRECTION_INCOMING, ZugferdMapper::MAPPING_AREA_UNITCODE, 'C62')); + $this->assertNotEquals('C62', ZugferdMapper::getMapping(ZugferdMapper::DIRECTION_OUTGOING, ZugferdMapper::MAPPING_AREA_UNITCODE, 'STK')); + + $this->assertTrue(ZugferdMapper::loadFromJsonFile($filename)); + + $this->assertEquals('EUR', ZugferdMapper::getMapping(ZugferdMapper::DIRECTION_INCOMING, ZugferdMapper::MAPPING_AREA_CURRENCY, 'EUR')); + $this->assertEquals('DM', ZugferdMapper::getMapping(ZugferdMapper::DIRECTION_INCOMING, ZugferdMapper::MAPPING_AREA_CURRENCY, 'DEM')); + $this->assertEquals('GBP', ZugferdMapper::getMapping(ZugferdMapper::DIRECTION_INCOMING, ZugferdMapper::MAPPING_AREA_CURRENCY, 'GBP')); + $this->assertEquals('EUR', ZugferdMapper::getMapping(ZugferdMapper::DIRECTION_OUTGOING, ZugferdMapper::MAPPING_AREA_CURRENCY, 'EUR')); + $this->assertEquals('DEM', ZugferdMapper::getMapping(ZugferdMapper::DIRECTION_OUTGOING, ZugferdMapper::MAPPING_AREA_CURRENCY, 'DM')); + $this->assertEquals('GBP', ZugferdMapper::getMapping(ZugferdMapper::DIRECTION_OUTGOING, ZugferdMapper::MAPPING_AREA_CURRENCY, 'GBP')); + + $this->assertEquals('STK', ZugferdMapper::getMapping(ZugferdMapper::DIRECTION_INCOMING, ZugferdMapper::MAPPING_AREA_UNITCODE, 'C62')); + $this->assertEquals('XPP', ZugferdMapper::getMapping(ZugferdMapper::DIRECTION_INCOMING, ZugferdMapper::MAPPING_AREA_UNITCODE, 'XPP')); + $this->assertEquals('C62', ZugferdMapper::getMapping(ZugferdMapper::DIRECTION_OUTGOING, ZugferdMapper::MAPPING_AREA_UNITCODE, 'STK')); + $this->assertEquals('XPP', ZugferdMapper::getMapping(ZugferdMapper::DIRECTION_OUTGOING, ZugferdMapper::MAPPING_AREA_UNITCODE, 'XPP')); + + @unlink($filename); } }