Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
47 / 47 |
|
100.00% |
5 / 5 |
CRAP | |
100.00% |
1 / 1 |
| OdsFileFaker | |
100.00% |
47 / 47 |
|
100.00% |
5 / 5 |
13 | |
100.00% |
1 / 1 |
| createOriginalFilename | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| createMimeType | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| isSupported | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| createResource | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
1 | |||
| createContent | |
100.00% |
38 / 38 |
|
100.00% |
1 / 1 |
9 | |||
| 1 | <?php |
| 2 | namespace Apie\Faker\FileFakers; |
| 3 | |
| 4 | use Apie\Faker\Interfaces\ApieFileFaker; |
| 5 | use Apie\Faker\Support\OpenDocumentWriter; |
| 6 | use Apie\Faker\Tabular\RandomRecords; |
| 7 | use DateTimeImmutable; |
| 8 | use Faker\Generator; |
| 9 | |
| 10 | /** |
| 11 | * Generates an OpenDocument spreadsheet with a styled header row and typed cells (text, numbers and dates). |
| 12 | */ |
| 13 | final class OdsFileFaker implements ApieFileFaker |
| 14 | { |
| 15 | private const HEADER_COLORS = ['#1F4E78', '#375623', '#C00000', '#7030A0', '#BF8F00', '#404040']; |
| 16 | private const FONTS = ['Liberation Sans', 'Liberation Serif', 'DejaVu Sans', 'Courier New']; |
| 17 | |
| 18 | public function createOriginalFilename(Generator $faker): string |
| 19 | { |
| 20 | return $faker->word() . '.ods'; |
| 21 | } |
| 22 | |
| 23 | public function createMimeType(): string |
| 24 | { |
| 25 | return 'application/vnd.oasis.opendocument.spreadsheet'; |
| 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 | return OpenDocumentWriter::write( |
| 37 | $mimeType, |
| 38 | $originalFilename, |
| 39 | ucfirst($faker->words(3, true)), |
| 40 | $this->createContent($faker) |
| 41 | ); |
| 42 | } |
| 43 | |
| 44 | private function createContent(Generator $faker): string |
| 45 | { |
| 46 | $records = RandomRecords::create($faker); |
| 47 | $e = OpenDocumentWriter::escape(...); |
| 48 | $header = $faker->randomElement(self::HEADER_COLORS); |
| 49 | $font = $faker->randomElement(self::FONTS); |
| 50 | $zebra = $faker->boolean(50); |
| 51 | |
| 52 | $rows = '<table:table-row>'; |
| 53 | foreach ($records->getColumnNames() as $name) { |
| 54 | $rows .= '<table:table-cell table:style-name="Header" office:value-type="string"><text:p>' . $e($name) . '</text:p></table:table-cell>'; |
| 55 | } |
| 56 | $rows .= '</table:table-row>'; |
| 57 | foreach ($records as $index => $record) { |
| 58 | $suffix = $zebra && $index % 2 === 1 ? 'Zebra' : ''; |
| 59 | $rows .= '<table:table-row>'; |
| 60 | foreach ($record as $value) { |
| 61 | $rows .= match (true) { |
| 62 | $value instanceof DateTimeImmutable => '<table:table-cell table:style-name="Date' . $suffix . '" office:value-type="date" office:date-value="' |
| 63 | . $value->format('Y-m-d') . '"><text:p>' . $value->format('Y-m-d') . '</text:p></table:table-cell>', |
| 64 | is_string($value) => '<table:table-cell table:style-name="Cell' . $suffix . '" office:value-type="string"><text:p>' . $e($value) . '</text:p></table:table-cell>', |
| 65 | default => '<table:table-cell table:style-name="Cell' . $suffix . '" office:value-type="float" office:value="' . $value . '"><text:p>' . $value . '</text:p></table:table-cell>', |
| 66 | }; |
| 67 | } |
| 68 | $rows .= '</table:table-row>'; |
| 69 | } |
| 70 | |
| 71 | $cellProperties = fn (string $background) => '<style:table-cell-properties fo:background-color="' . $background . '" fo:border="0.5pt solid #bfbfbf"/>' |
| 72 | . '<style:text-properties fo:font-family="' . $font . '"/>'; |
| 73 | |
| 74 | return OpenDocumentWriter::XML |
| 75 | . '<office:document-content ' . OpenDocumentWriter::NAMESPACES . ' office:version="1.2">' |
| 76 | . '<office:automatic-styles>' |
| 77 | . '<style:style style:name="Col" style:family="table-column"><style:table-column-properties style:column-width="4cm"/></style:style>' |
| 78 | . '<number:date-style style:name="NDate"><number:year number:style="long"/><number:text>-</number:text>' |
| 79 | . '<number:month number:style="long"/><number:text>-</number:text><number:day number:style="long"/></number:date-style>' |
| 80 | . '<style:style style:name="Header" style:family="table-cell"><style:table-cell-properties fo:background-color="' . $header . '" fo:border="0.5pt solid #bfbfbf"/>' |
| 81 | . '<style:text-properties fo:font-family="' . $font . '" fo:font-weight="bold" fo:color="#ffffff"/></style:style>' |
| 82 | . '<style:style style:name="Cell" style:family="table-cell">' . $cellProperties('transparent') . '</style:style>' |
| 83 | . '<style:style style:name="CellZebra" style:family="table-cell">' . $cellProperties('#f2f2f2') . '</style:style>' |
| 84 | . '<style:style style:name="Date" style:family="table-cell" style:data-style-name="NDate">' . $cellProperties('transparent') . '</style:style>' |
| 85 | . '<style:style style:name="DateZebra" style:family="table-cell" style:data-style-name="NDate">' . $cellProperties('#f2f2f2') . '</style:style>' |
| 86 | . '</office:automatic-styles>' |
| 87 | . '<office:body><office:spreadsheet><table:table table:name="Sheet1">' |
| 88 | . '<table:table-column table:style-name="Col" table:number-columns-repeated="' . count($records->getColumnNames()) . '"/>' |
| 89 | . $rows |
| 90 | . '</table:table></office:spreadsheet></office:body>' |
| 91 | . '</office:document-content>'; |
| 92 | } |
| 93 | } |