Repository navigation
Expand file tree
/
Copy pathJSON.php
More file actions
88 lines (70 loc) · 2.58 KB
/
Copy pathJSON.php
File metadata and controls
88 lines (70 loc) · 2.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
<?php
/**
* Bit&Black IDML-JSON Converter.
*
* @author Tobias Köngeter
* @copyright Copyright © Bit&Black
* @link https://www.bitandblack.com
* @license MIT
*/
namespace BitAndBlack\IdmlJsonConverter\File;
use BitAndBlack\IdmlJsonConverter\Converter\ArrayToDomNodeConverter;
use BitAndBlack\IdmlJsonConverter\Exception\FailedExtractingContentExpection;
use BitAndBlack\IdmlJsonConverter\Exception\UnknownFileException;
use BitAndBlack\IdmlJsonConverter\Utils\RootElementNameFromFileName;
use BitAndBlack\IdmlJsonConverter\ValueModifier\ValueNormalizer;
use DOMException;
use ZipStream\Exception\OverflowException;
use ZipStream\ZipStream;
readonly class JSON
{
private ?string $response;
/**
* @param array<string, string> $content
* @throws DOMException
* @throws FailedExtractingContentExpection
* @throws UnknownFileException
*/
public function __construct(array $content)
{
$outputStream = fopen('php://memory', 'wb+');
if (false === $outputStream) {
throw FailedExtractingContentExpection::phpMemory();
}
$zipStream = new ZipStream(
outputStream: $outputStream,
sendHttpHeaders: false,
);
foreach ($content as $fileName => $fileContent) {
$valueNormalizer = new ValueNormalizer($fileContent);
$fileContent = $valueNormalizer->getStringified();
if (str_ends_with($fileName, '.xml')) {
$rootElementName = new RootElementNameFromFileName($fileName);
$arrayToDomNodeConverter = new ArrayToDomNodeConverter($fileContent, $rootElementName);
$fileContent = $arrayToDomNodeConverter->getString();
$fileContent = preg_replace_callback(
'/<\?ACE\s(.*?)\?>/s',
static fn ($matches) => '<?ACE ' . htmlentities((string) $matches[1], ENT_QUOTES | ENT_XML1, 'UTF-8') . '?>',
$fileContent
);
}
$zipStream->addFile($fileName, $fileContent);
}
try {
$zipStream->finish();
} catch (OverflowException $exception) {
throw FailedExtractingContentExpection::fileTooLarge($exception);
}
rewind($outputStream);
$response = stream_get_contents($outputStream);
fclose($outputStream);
if (!$response) {
throw FailedExtractingContentExpection::cannotReadFromStream();
}
$this->response = $response;
}
public function getIDML(): ?string
{
return $this->response;
}
}