Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
57 / 57
100.00% covered (success)
100.00%
6 / 6
CRAP
100.00% covered (success)
100.00%
1 / 1
OdtFileFaker
100.00% covered (success)
100.00%
57 / 57
100.00% covered (success)
100.00%
6 / 6
15
100.00% covered (success)
100.00%
1 / 1
 createOriginalFilename
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 createMimeType
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 isSupported
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 createResource
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 createContent
100.00% covered (success)
100.00%
38 / 38
100.00% covered (success)
100.00%
1 / 1
6
 createTable
100.00% covered (success)
100.00%
14 / 14
100.00% covered (success)
100.00%
1 / 1
5
1<?php
2namespace Apie\Faker\FileFakers;
3
4use Apie\Faker\Interfaces\ApieFileFaker;
5use Apie\Faker\Support\OpenDocumentWriter;
6use Apie\Faker\Tabular\RandomRecords;
7use DateTimeImmutable;
8use Faker\Generator;
9
10/**
11 * Generates an OpenDocument text file with a title, headings, paragraphs, a list and a table in a random style.
12 */
13final class OdtFileFaker implements ApieFileFaker
14{
15    private const FONTS = ['Liberation Serif', 'Liberation Sans', 'DejaVu Sans', 'Courier New'];
16    private const COLORS = ['#1F4E78', '#375623', '#C00000', '#7030A0', '#BF8F00', '#404040'];
17
18    public function createOriginalFilename(Generator $faker): string
19    {
20        return $faker->word() . '.odt';
21    }
22
23    public function createMimeType(): string
24    {
25        return 'application/vnd.oasis.opendocument.text';
26    }
27
28    public static function isSupported(): bool
29    {
30        return true;
31    }
32
33    /** @return resource */
34    public function createResource(Generator $faker, string $originalFilename, string $mimeType): mixed
35    {
36        $title = ucfirst($faker->words($faker->numberBetween(2, 5), true));
37
38        return OpenDocumentWriter::write($mimeType, $originalFilename, $title, $this->createContent($faker, $title));
39    }
40
41    private function createContent(Generator $faker, string $title): string
42    {
43        $color = $faker->randomElement(self::COLORS);
44        $font = $faker->randomElement(self::FONTS);
45        $e = OpenDocumentWriter::escape(...);
46        $body = '<text:p text:style-name="Title">' . $e($title) . '</text:p>';
47        $sections = $faker->numberBetween(2, 4);
48        $tableSection = $faker->numberBetween(0, $sections - 1);
49        for ($i = 0; $i < $sections; $i++) {
50            $body .= '<text:h text:style-name="Heading" text:outline-level="1">' . $e(ucfirst($faker->words(3, true))) . '</text:h>';
51            for ($j = $faker->numberBetween(1, 3); $j > 0; $j--) {
52                $body .= '<text:p text:style-name="Body">' . $e($faker->paragraph(5)) . '</text:p>';
53            }
54            if ($faker->boolean(50)) {
55                $body .= '<text:list>';
56                for ($j = $faker->numberBetween(3, 6); $j > 0; $j--) {
57                    $body .= '<text:list-item><text:p text:style-name="Body">' . $e($faker->sentence(6)) . '</text:p></text:list-item>';
58                }
59                $body .= '</text:list>';
60            }
61            if ($i === $tableSection) {
62                $body .= $this->createTable($faker, $e);
63            }
64        }
65
66        return OpenDocumentWriter::XML
67            . '<office:document-content ' . OpenDocumentWriter::NAMESPACES . ' office:version="1.2">'
68            . '<office:automatic-styles>'
69            . '<style:style style:name="Title" style:family="paragraph">'
70            . '<style:paragraph-properties fo:text-align="center" fo:margin-bottom="0.5cm"/>'
71            . '<style:text-properties fo:font-family="' . $font . '" fo:font-size="26pt" fo:font-weight="bold" fo:color="' . $color . '"/></style:style>'
72            . '<style:style style:name="Heading" style:family="paragraph">'
73            . '<style:paragraph-properties fo:margin-top="0.4cm" fo:margin-bottom="0.2cm"/>'
74            . '<style:text-properties fo:font-family="' . $font . '" fo:font-size="16pt" fo:font-weight="bold" fo:color="' . $color . '"/></style:style>'
75            . '<style:style style:name="Body" style:family="paragraph">'
76            . '<style:paragraph-properties fo:margin-bottom="0.25cm" fo:text-align="justify"/>'
77            . '<style:text-properties fo:font-family="' . $font . '" fo:font-size="11pt"/></style:style>'
78            . '<style:style style:name="Table" style:family="table"><style:table-properties style:width="17cm" table:align="margins"/></style:style>'
79            . '<style:style style:name="TableHeader" style:family="table-cell">'
80            . '<style:table-cell-properties fo:background-color="' . $color . '" fo:padding="0.1cm" fo:border="0.5pt solid #999999"/>'
81            . '<style:text-properties fo:font-weight="bold" fo:color="#ffffff"/></style:style>'
82            . '<style:style style:name="TableCell" style:family="table-cell">'
83            . '<style:table-cell-properties fo:padding="0.1cm" fo:border="0.5pt solid #999999"/></style:style>'
84            . '</office:automatic-styles>'
85            . '<office:body><office:text>' . $body . '</office:text></office:body>'
86            . '</office:document-content>';
87    }
88
89    private function createTable(Generator $faker, \Closure $e): string
90    {
91        $records = RandomRecords::create($faker, 3, 10);
92        $columns = $records->getColumnNames();
93        $table = '<table:table table:name="Table1" table:style-name="Table">'
94            . '<table:table-column table:number-columns-repeated="' . count($columns) . '"/><table:table-row>';
95        foreach ($columns as $name) {
96            $table .= '<table:table-cell table:style-name="TableHeader" office:value-type="string"><text:p>' . $e($name) . '</text:p></table:table-cell>';
97        }
98        $table .= '</table:table-row>';
99        foreach ($records as $record) {
100            $table .= '<table:table-row>';
101            foreach ($record as $value) {
102                $text = $value instanceof DateTimeImmutable ? $value->format('Y-m-d') : (string) $value;
103                $table .= '<table:table-cell table:style-name="TableCell" office:value-type="string"><text:p>' . $e($text) . '</text:p></table:table-cell>';
104            }
105            $table .= '</table:table-row>';
106        }
107
108        return $table . '</table:table>';
109    }
110}