Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
30 / 30
100.00% covered (success)
100.00%
4 / 4
CRAP
100.00% covered (success)
100.00%
1 / 1
OpenDocumentWriter
100.00% covered (success)
100.00%
30 / 30
100.00% covered (success)
100.00%
4 / 4
5
100.00% covered (success)
100.00%
1 / 1
 __construct
n/a
0 / 0
n/a
0 / 0
1
 escape
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 write
100.00% covered (success)
100.00%
15 / 15
100.00% covered (success)
100.00%
1 / 1
1
 manifest
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
1
 meta
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2namespace Apie\Faker\Support;
3
4use ZipStream\CompressionMethod;
5use ZipStream\ZipStream;
6
7/**
8 * Creates an OpenDocument package (odt, ods, ...) from a content.xml.
9 */
10final class OpenDocumentWriter
11{
12    public const XML = '<?xml version="1.0" encoding="UTF-8"?>';
13    public const NAMESPACES = 'xmlns:office="urn:oasis:names:tc:opendocument:xmlns:office:1.0"'
14        . ' xmlns:style="urn:oasis:names:tc:opendocument:xmlns:style:1.0"'
15        . ' xmlns:text="urn:oasis:names:tc:opendocument:xmlns:text:1.0"'
16        . ' xmlns:table="urn:oasis:names:tc:opendocument:xmlns:table:1.0"'
17        . ' xmlns:fo="urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0"'
18        . ' xmlns:number="urn:oasis:names:tc:opendocument:xmlns:datastyle:1.0"';
19
20    /**
21     * @codeCoverageIgnore
22     */
23    private function __construct()
24    {
25    }
26
27    public static function escape(string $value): string
28    {
29        return htmlspecialchars($value, ENT_XML1 | ENT_QUOTES | ENT_SUBSTITUTE);
30    }
31
32    /** @return resource */
33    public static function write(string $mimeType, string $originalFilename, string $title, string $contentXml): mixed
34    {
35        $resource = tmpfile();
36        $zipstream = new ZipStream(
37            outputStream: $resource,
38            sendHttpHeaders: false,
39            outputName: $originalFilename,
40            contentType: $mimeType,
41            defaultEnableZeroHeader: false,
42            enableZip64: false,
43        );
44        // the mimetype file must be the first entry and can not be compressed.
45        $zipstream->addFile('mimetype', $mimeType, compressionMethod: CompressionMethod::STORE);
46        $zipstream->addFile('META-INF/manifest.xml', self::manifest($mimeType));
47        $zipstream->addFile('meta.xml', self::meta($title));
48        $zipstream->addFile('content.xml', $contentXml);
49        $zipstream->finish();
50
51        return $resource;
52    }
53
54    private static function manifest(string $mimeType): string
55    {
56        return self::XML
57            . '<manifest:manifest xmlns:manifest="urn:oasis:names:tc:opendocument:xmlns:manifest:1.0" manifest:version="1.2">'
58            . '<manifest:file-entry manifest:full-path="/" manifest:version="1.2" manifest:media-type="' . $mimeType . '"/>'
59            . '<manifest:file-entry manifest:full-path="content.xml" manifest:media-type="text/xml"/>'
60            . '<manifest:file-entry manifest:full-path="meta.xml" manifest:media-type="text/xml"/>'
61            . '</manifest:manifest>';
62    }
63
64    private static function meta(string $title): string
65    {
66        return self::XML
67            . '<office:document-meta xmlns:office="urn:oasis:names:tc:opendocument:xmlns:office:1.0"'
68            . ' xmlns:meta="urn:oasis:names:tc:opendocument:xmlns:meta:1.0"'
69            . ' xmlns:dc="http://purl.org/dc/elements/1.1/" office:version="1.2"><office:meta>'
70            . '<dc:title>' . self::escape($title) . '</dc:title>'
71            . '<meta:initial-creator>Apie faker</meta:initial-creator>'
72            . '<meta:creation-date>' . gmdate('Y-m-d\TH:i:s') . '</meta:creation-date>'
73            . '</office:meta></office:document-meta>';
74    }
75}