1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-11 13:42:49 +01:00
invoiceninja/tests/Integration/Einvoice/FACT1Test.php

198 lines
5.0 KiB
PHP
Raw Normal View History

2024-05-18 15:04:53 +02:00
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2021. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://www.elastic.co/licensing/elastic-license
*/
namespace Tests\Integration\Einvoice;
use Tests\TestCase;
2024-05-19 23:50:39 +02:00
use Sabre\Xml\Reader;
use Sabre\Xml\Service;
2024-05-18 15:04:53 +02:00
use Invoiceninja\Einvoice\Models\FACT1\Invoice;
/**
* @test
*/
class FACT1Test extends TestCase
{
2024-05-19 23:50:39 +02:00
public array $set = [];
2024-05-18 15:04:53 +02:00
protected function setUp(): void
{
parent::setUp();
}
2024-05-19 23:50:39 +02:00
// public function testValidationFact1()
// {
2024-05-18 15:04:53 +02:00
2024-05-19 23:50:39 +02:00
// $files = [
// 'tests/Integration/Einvoice/samples/fact1.xml',
// ];
2024-05-18 15:04:53 +02:00
2024-05-19 23:50:39 +02:00
// foreach($files as $f) {
2024-05-18 15:04:53 +02:00
2024-05-19 23:50:39 +02:00
// $xml = file_get_contents($f);
2024-05-18 15:04:53 +02:00
2024-05-19 23:50:39 +02:00
// // Remove namespaces and prefixes
// $xmlWithoutNamespacesAndPrefixes = $this->removeNamespacesAndPrefixes($xml);
2024-05-18 15:04:53 +02:00
2024-05-19 23:50:39 +02:00
// // Output the result
// nlog($xmlWithoutNamespacesAndPrefixes);
2024-05-18 15:04:53 +02:00
2024-05-19 23:50:39 +02:00
// }
2024-05-18 15:04:53 +02:00
2024-05-19 23:50:39 +02:00
public function removeNamespacesFromArray($data)
{
if (is_array($data)) {
foreach ($data as &$item) {
if (isset($item['name'])) {
// Remove the namespace from the name
$item['name'] = preg_replace('/^\{\}(.+)/', '$1', $item['name']);
}
if (isset($item['value']) && is_array($item['value'])) {
// Recursively process child elements
$item['value'] = $this->removeNamespacesFromArray($item['value']);
}
if (isset($item['attributes'])) {
unset($item['attributes']);
// Process attributes if needed
// $item['attributes'] = array_map(function ($attr) {
// return preg_replace('/^\{\}(.+)/', '$1', $attr);
// }, $item['attributes']);
}
2024-05-18 15:04:53 +02:00
}
2024-05-19 23:50:39 +02:00
}
return $data;
}
function convertToKeyValue($data)
{
$result = [];
foreach ($data as $item) {
// Remove namespace prefix if present
$name = preg_replace('/^\{\}(.+)/', '$1', $item['name']);
$result[$name] = $item['value'];
}
return $result;
}
2024-05-18 15:04:53 +02:00
2024-05-19 23:50:39 +02:00
public function keyValueDeserializer(Reader $reader)
{
$values = [];
$reader->read();
$reader->next();
foreach ($reader->parseGetElements() as $element) {
// Strip the namespace prefix
echo "merp".PHP_EOL;
$name = preg_replace('/^\{\}.*/', '', $element['name']);
$values[$name] = $element['value'];
}
return $values;
}
public function testFactToArray()
{
$xml = file_get_contents('tests/Integration/Einvoice/samples/fact1_no_prefixes.xml');
$service = new Service();
// $service->elementMap = [
// '{}' => 'Sabre\Xml\Deserializer\keyValue',
// ];
2024-05-18 15:04:53 +02:00
2024-05-19 23:50:39 +02:00
// $service->elementMap = [
// '{}*' => function (Reader $reader) use ($service) {
// return $this->keyValueDeserializer($reader);
// }
// ];
$result = $this->removeNamespacesFromArray($service->parse($xml));
// Convert parsed XML to key-value array
if (isset($result['value']) && is_array($result['value'])) {
$keyValueArray = $this->convertToKeyValue($result['value']);
} else {
$keyValueArray = [];
2024-05-18 15:04:53 +02:00
}
2024-05-19 23:50:39 +02:00
// Output the result
nlog($keyValueArray);
// nlog($cleanedArray);
nlog($service->parse($xml));
}
// Output the result
// ($xmlWithoutNamespaces);
// $reader = new Reader();
// $service = new Service();
// $service->elementMap = [
// '*' => 'Sabre\Xml\Deserializer\keyValue',
// ];
// nlog($service->parse($xmlstring));
// $payload ='';
// // $reader->xml($xmlstring);
// // $payload = $reader->parse();
// // nlog($payload);
// $validation_array = false;
// try {
// $rules = Invoice::getValidationRules($payload);
// nlog($rules);
// $this->assertIsArray($rules);
// $payload = Invoice::from($payload)->toArray();
// nlog($payload);
// $this->assertIsArray($payload);
// $validation_array = Invoice::validate($payload);
// $this->assertIsArray($validation_array);
// } catch(\Illuminate\Validation\ValidationException $e) {
// nlog($e->errors());
// }
// $this->assertIsArray($validation_array);
// }
private function extractName($name): string
{
$pattern = '/\{[^{}]*\}([^{}]*)/';
if (preg_match($pattern, $name, $matches)) {
$extracted = $matches[1];
return $extracted;
}
2024-05-18 15:04:53 +02:00
2024-05-19 23:50:39 +02:00
return $name;
2024-05-18 15:04:53 +02:00
}
}