Skip to content

Commit

Permalink
ZugferdMapper -> Load/Save from/to JSON
Browse files Browse the repository at this point in the history
  • Loading branch information
HorstOeko committed May 16, 2024
1 parent d9cf693 commit 85353d7
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
36 changes: 36 additions & 0 deletions src/ZugferdMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,42 @@ class ZugferdMapper
*/
protected const KEY_TOCODE = "tocode";

/**
* Load mappings from JSON string
*
* @param string $json
* @return boolean
*/
public static function loadFromJson(string $json): bool
{
$jsonDecoded = json_decode($json, true);

if (!is_array($jsonDecoded)) {
return false;
}
}

/**
* Saves mappings to JSON string
*
* @return string
*/
public static function saveToJson(): string
{
return json_encode(static::$mappings, JSON_PRETTY_PRINT | JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_AMP);
}

/**
* Saves mappings to a JSON file
*
* @param string $filename
* @return boolean
*/
public static function saveToJsonFile(string $filename): bool
{
return file_put_contents($filename, static::saveToJson()) !== false;
}

/**
* Add a mapping to the internal mapping list
*
Expand Down
16 changes: 16 additions & 0 deletions tests/testcases/MapperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -179,4 +179,20 @@ function () {
}
);
}

public function testSaveToJsonFile(): void
{
$filename = getcwd() . "/mappings.json";

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(file_exists($filename));
}
}

0 comments on commit 85353d7

Please sign in to comment.