Skip to content

Commit

Permalink
add denormalization
Browse files Browse the repository at this point in the history
  • Loading branch information
turbo124 committed Aug 28, 2024
1 parent d4f8031 commit b3225f3
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 22 deletions.
20 changes: 14 additions & 6 deletions src/EInvoice.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,7 @@ public function validate(mixed $object): array
public function validateRequest(array $payload, string $class): array
{

$serializer = $this->getSerializer();

// Blank string is converted to null, which then makes the "string" type invalid - even for an optional property.
// by denormalizing, we avoid this scenario, as the prop is dropped, but the validator will still enforce if required!
// ie, we need this to get an object - of some form - into the validator!!!!
$payload = $serializer->denormalize(json_encode($payload), $class, null, [AbstractObjectNormalizer::SKIP_NULL_VALUES => true]);
$payload = $this->denormalize($payload, $class);

// $payload = $serializer->deserialize(json_encode($payload), $class, 'json');

Expand All @@ -99,6 +94,19 @@ public function validateRequest(array $payload, string $class): array

}

public function denormalize(array $payload $class): mixed
{

$serializer = $this->getSerializer();

// Blank string is converted to null, which then makes the "string" type invalid - even for an optional property.
// by denormalizing, we avoid this scenario, as the prop is dropped, but the validator will still enforce if required!
// ie, we need this to get an object - of some form - into the validator!!!!
$payload = $serializer->denormalize(json_encode($payload), $class, null, [AbstractObjectNormalizer::SKIP_NULL_VALUES => true]);]

return $payload;

}

/**
* Decodes a document into an object
Expand Down
45 changes: 29 additions & 16 deletions tests/Data/PeppolDataTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,19 @@ public function testFactSerializer()
// $this->assertEquals(234234234, $invoice->AccountingSupplierParty->Party->PartyIdentification[0]->ID);
}

public function testDecode()
{
$json = '{"ID":"ROCKER","Note":"NOTE","PaymentMeans":[{"ID":{"value":"afdasfasdfasdfas"},"PayeeFinancialAccount":{"Name":"PFA-NAME","ID":{"value":"DE89370400440532013000"},"AliasName":"PFA-Alias","AccountTypeCode":{"value":"CHECKING"},"AccountFormatCode":{"value":"IBAN"},"CurrencyCode":{"value":"EUR"},"FinancialInstitutionBranch":{"ID":{"value":"DEUTDEMMXXX"},"Name":"Deutsche Bank"}}}]}';

$e = new EInvoice();
$result = $e->decode('Peppol', $json, 'json');

echo print_r($result);


}


public function testXSDValidation()
{

Expand Down Expand Up @@ -233,30 +246,30 @@ private function getPath($string): ?string
return null;
}

public function testPeppolValidation()
{
// public function testPeppolValidation()
// {

$f = "src/Standards/Peppol/example.xml";
$xslt = "src/Standards/Peppol/peppol.xslt";
// $f = "src/Standards/Peppol/example.xml";
// $xslt = "src/Standards/Peppol/peppol.xslt";

$e = new EInvoice();
$result = $e->decode('Peppol', file_get_contents($f), 'xml');;
// $e = new EInvoice();
// $result = $e->decode('Peppol', file_get_contents($f), 'xml');;

$convert = $e->encode($result, 'xml');
// $convert = $e->encode($result, 'xml');

$this->assertNotNull($convert);
// $this->assertNotNull($convert);

$xmlFile = $f;
$saxonProc = new \Saxon\SaxonProcessor();
// $xmlFile = $f;
// $saxonProc = new \Saxon\SaxonProcessor();

$proc = $saxonProc->newXslt30Processor();
// $proc = $saxonProc->newXslt30Processor();

// $proc->transformFileToFile($f, $xslt, $outputFile); //output to file
// // $proc->transformFileToFile($f, $xslt, $outputFile); //output to file

// $result = $proc->transformFileToValue($f, $xslt); //output to saxon object
// // $result = $proc->transformFileToValue($f, $xslt); //output to saxon object

$executable = $proc->compileFromFile($xslt);
$result = $executable->transformFileToString($xmlFile); //output to strings
// $executable = $proc->compileFromFile($xslt);
// $result = $executable->transformFileToString($xmlFile); //output to strings

}
// }
}

0 comments on commit b3225f3

Please sign in to comment.