Skip to content

Commit

Permalink
WIP #7
Browse files Browse the repository at this point in the history
TODO:
- test that JSON files converted via PHP validate against the JSON schema
- test that JSON files converted via PHP can be reconverted back to XML without loss of information
- protect against large XML files
- protect against malicous XML files
  • Loading branch information
Paolo Greppi committed Jul 3, 2018
1 parent 33df0b2 commit 4d4e150
Show file tree
Hide file tree
Showing 5 changed files with 123 additions and 3 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
node_modules
vendor
update.sh
.directory
12 changes: 9 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ Poiché per lo sviluppo di API ed SDK è più comodo manipolare i dati in format
## Prerequisiti

```
sudo apt install make nodejs yarnpkg python libxml2-utils
sudo apt install make nodejs yarnpkg php-cli libxml2-utils composer
yarnpkg install
composer install
```

## Procedura
Expand Down Expand Up @@ -70,6 +71,11 @@ ottenendo però uno schema non utilizzabile.
Successivamente i files XML di esempio sono stati convertiti a JSON per mezzo della libreria [node-xml2json](https://github.com/buglabs/node-xml2json) (script [`bin/convert_samples.sh`](bin/convert_samples.sh)).
La liberia xml2json [non è supportata nel browser](https://github.com/buglabs/node-xml2json/issues/97) quindi per i client la conversione va fatta server side con lo script PHP [`www/xml2json.php`](www/xml2json.php), ad esempio:
```
curl -X POST -F 'xml=@samples/IT01234567890_FPA01.xml' http://localhost:8000/xml2json.php
```
Si è quindi generato uno schema a partire dai files fattura JSON di esempio, per mezzo del servizio on-line [jsonschema.net](https://www.jsonschema.net/), che dopo semplificazione (`grep -v '$id'`) e aggiustamento manuale aggiunta di campi `title` e `description` desunti dalle SPECIFICHE TECNICHE OPERATIVE DEL FORMATO DELLA FATTURA DEL SISTEMA DI INTERSCAMBIO ha dato origine allo schema [`fatturaPA_1.2_schema.json`](fatturaPA_1.2_schema.json).
Tutti i files di esempio JSON sono validati dallo schema (script [`bin/validate_samples_json.sh`](bin/validate_samples_json.sh)).
Expand All @@ -90,9 +96,9 @@ Con lo schema così ottenuto e [JSON Editor](https://github.com/json-editor/json
Per lanciare il demo:
```
make
python -m SimpleHTTPServer
php -S localhost:8000 -t www
```
quindi visitare http://localhost:8000/www/index.html
quindi visitare http://localhost:8000/index.html
## Legalese
Expand Down
5 changes: 5 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"require": {
"squizlabs/php_codesniffer": "*"
}
}
69 changes: 69 additions & 0 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

39 changes: 39 additions & 0 deletions www/xml2json.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php
// sample usage:
// curl -X POST -F 'xml=@samples/IT01234567890_FPA01.xml' http://localhost:8000/xml2json.php

function normalize(&$array, $arrayNotation)
{
foreach ($array as $key => $value) {
normalize($array[$key], $arrayNotation);
if (in_array($key, $arrayNotation) && is_array($value)) {
$array[$key] = [$array[$key]];
}
}
}

$arrayNotation = [
"FatturaElettronicaBody",
"Causale",
"DatiOrdineAcquisto",
"RiferimentoNumeroLinea",
"DatiContratto",
"DatiConvenzione",
"DatiRicezione",
"DettaglioLinee",
"DatiRiepilogo",
"DatiPagamento"
];

$xml = simplexml_load_file($_FILES['xml']['tmp_name']);
// convert back and forth to transform SimpleXMLElement Objects to associative arrays
$json = json_encode($xml);
$array = json_decode($json, true);
// recusively traverse array and convert selected scalar values to arrays
normalize($array, $arrayNotation);
// convert @attributes to keys
foreach ($array['@attributes'] as $key => $value) {
$array[$key] = $value;
}
unset($array['@attributes']);
echo json_encode(array("FatturaElettronica" => $array), JSON_PRETTY_PRINT);

0 comments on commit 4d4e150

Please sign in to comment.