Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
95.00% |
19 / 20 |
|
66.67% |
2 / 3 |
CRAP | |
0.00% |
0 / 1 |
| EntityExport | |
95.00% |
19 / 20 |
|
66.67% |
2 / 3 |
7 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| streamFromEntityList | |
94.44% |
17 / 18 |
|
0.00% |
0 / 1 |
5.00 | |||
| getSupportedExtensions | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | namespace Apie\Export; |
| 3 | |
| 4 | use Apie\Core\Context\ApieContext; |
| 5 | use Apie\Core\Datalayers\Lists\EntityListInterface; |
| 6 | use Apie\Core\PropertyAccess; |
| 7 | use Apie\Export\Lists\FileExtensionList; |
| 8 | use Apie\HtmlBuilders\Columns\ColumnSelector; |
| 9 | use Apie\Serializer\Serializer; |
| 10 | use Psr\Http\Message\StreamInterface; |
| 11 | |
| 12 | class EntityExport |
| 13 | { |
| 14 | public function __construct( |
| 15 | private readonly ColumnSelector $columnSelector, |
| 16 | private readonly ExportInterface $exporter, |
| 17 | private readonly Serializer $serializer |
| 18 | ) { |
| 19 | } |
| 20 | |
| 21 | public function streamFromEntityList(\ReflectionClass $resourceName, EntityListInterface $entityList, ApieContext $apieContext, string $outputFilename = 'export.xlsx'): StreamInterface |
| 22 | { |
| 23 | $columns = $this->columnSelector->getColumns($resourceName, $apieContext); |
| 24 | $generator = function () use ($entityList, $columns, $apieContext) { |
| 25 | $first = true; |
| 26 | foreach ($entityList as $entity) { |
| 27 | $data = []; |
| 28 | foreach ($columns as $column) { |
| 29 | $data[$column] = $this->serializer->normalize( |
| 30 | PropertyAccess::getPropertyValue($entity, [$column], $apieContext, false), |
| 31 | $apieContext |
| 32 | ); |
| 33 | if (is_array($data[$column])) { |
| 34 | $data[$column] = implode(', ', $data[$column]); |
| 35 | } |
| 36 | } |
| 37 | if ($first) { |
| 38 | yield $columns; |
| 39 | $first = false; |
| 40 | } |
| 41 | yield array_values($data); |
| 42 | } |
| 43 | }; |
| 44 | return $this->exporter->streamFromSheets([$resourceName->getShortName() => $generator()], $outputFilename); |
| 45 | } |
| 46 | |
| 47 | public function getSupportedExtensions(): FileExtensionList |
| 48 | { |
| 49 | return $this->exporter->getSupportedExtensions(); |
| 50 | } |
| 51 | } |