Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
18 / 18 |
|
100.00% |
4 / 4 |
CRAP | |
100.00% |
1 / 1 |
WordDocumentFaker | |
100.00% |
18 / 18 |
|
100.00% |
4 / 4 |
5 | |
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% |
15 / 15 |
|
100.00% |
1 / 1 |
2 |
1 | <?php |
2 | namespace Apie\Faker\FileFakers; |
3 | |
4 | use Apie\Faker\Interfaces\ApieFileFaker; |
5 | use Faker\Generator; |
6 | use Symfony\Component\Finder\Finder; |
7 | use ZipStream\ZipStream; |
8 | |
9 | final class WordDocumentFaker implements ApieFileFaker |
10 | { |
11 | public function createOriginalFilename(Generator $faker): string |
12 | { |
13 | return $faker->word() . '.docx'; |
14 | } |
15 | public function createMimeType(): string |
16 | { |
17 | return 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'; |
18 | } |
19 | public static function isSupported(): bool |
20 | { |
21 | return true; |
22 | } |
23 | |
24 | /** @return resource */ |
25 | public function createResource(Generator $faker, string $originalFilename, string $mimeType): mixed |
26 | { |
27 | $resource = tmpfile(); |
28 | $zipstream = new ZipStream( |
29 | outputStream: $resource, |
30 | sendHttpHeaders: false, |
31 | outputName: $originalFilename, |
32 | contentType: $mimeType, |
33 | defaultEnableZeroHeader: true, |
34 | ); |
35 | foreach (Finder::create()->files()->ignoreDotFiles(false)->in(__DIR__ . '/../../fixtures/docx') as $file) { |
36 | $zipstream->addFileFromPath($file->getRelativePathname(), $file->getRealPath()); |
37 | } |
38 | $documentXML = file_get_contents(__DIR__ . '/../../fixtures/document.xml'); |
39 | $documentXML = str_replace('LORUM IPSUM', htmlentities($faker->text(1024)), $documentXML); |
40 | $zipstream->addFile('word/document.xml', $documentXML); |
41 | //$imageFile = $faker->fakeClass(BackgroundFileFaker::class); |
42 | //$zipstream->addFileFromStream('word/media/image1.jpg', $imageFile->createResource($faker, 'image1.jpg', 'image/jpeg')); |
43 | // TODO replace image randomly |
44 | $zipstream->finish(); |
45 | return $resource; |
46 | } |
47 | } |