Skip to content

Commit

Permalink
ZugferdMapper -> Completed JSON Loading and Saving
Browse files Browse the repository at this point in the history
  • Loading branch information
HorstOeko committed May 16, 2024
1 parent 85353d7 commit 875243a
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 2 deletions.
48 changes: 47 additions & 1 deletion src/ZugferdMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
}

/**
Expand Down
43 changes: 42 additions & 1 deletion tests/testcases/MapperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -184,15 +184,56 @@ public function testSaveToJsonFile(): 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');

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);
}
}

0 comments on commit 875243a

Please sign in to comment.