apply more cs rules for php-cs

This commit is contained in:
2023-10-17 13:27:03 +02:00
parent 0b0cbed9db
commit bc2041cbdd
1485 changed files with 8169 additions and 9620 deletions

View File

@@ -13,25 +13,18 @@ namespace Chill\MainBundle\Export\Formatter;
use Chill\MainBundle\Export\ExportManager;
use Chill\MainBundle\Export\FormatterInterface;
use LogicException;
use RuntimeException;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\FormType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Contracts\Translation\TranslatorInterface;
use function array_key_exists;
use function array_slice;
use function call_user_func;
use function count;
use function is_array;
/**
* Command to get the report with curl:
* curl --user "center a_social:password" "http://localhost:8000/fr/exports/generate/count_person?export[filters][person_gender_filter][enabled]=&export[filters][person_nationality_filter][enabled]=&export[filters][person_nationality_filter][form][nationalities]=&export[aggregators][person_nationality_aggregator][order]=1&export[aggregators][person_nationality_aggregator][form][group_by_level]=country&export[submit]=&export[_token]=RHpjHl389GrK-bd6iY5NsEqrD5UKOTHH40QKE9J1edU" --globoff.
*
* @deprecated this formatter is not used any more.
* @deprecated this formatter is not used any more
*/
class CSVFormatter implements FormatterInterface
{
@@ -65,13 +58,11 @@ class CSVFormatter implements FormatterInterface
/**
* @uses appendAggregatorForm
*
* @param mixed $exportAlias
*/
public function buildForm(FormBuilderInterface $builder, $exportAlias, array $aggregatorAliases)
{
$aggregators = $this->exportManager->getAggregators($aggregatorAliases);
$nb = count($aggregatorAliases);
$nb = \count($aggregatorAliases);
foreach ($aggregators as $alias => $aggregator) {
$builderAggregator = $builder->create($alias, FormType::class, [
@@ -101,7 +92,7 @@ class CSVFormatter implements FormatterInterface
continue;
}
if (is_array($statement)) {
if (\is_array($statement)) {
$descriptions[] = $this->translator->trans(
$statement[0],
$statement[1],
@@ -142,7 +133,7 @@ class CSVFormatter implements FormatterInterface
$response = new Response();
$response->setStatusCode(200);
$response->headers->set('Content-Type', 'text/csv; charset=utf-8');
//$response->headers->set('Content-Disposition','attachment; filename="export.csv"');
// $response->headers->set('Content-Disposition','attachment; filename="export.csv"');
$response->setContent($this->generateContent());
@@ -172,10 +163,8 @@ class CSVFormatter implements FormatterInterface
// gather data in an array
foreach ($keys as $key) {
$values = array_map(static function ($row) use ($key, $alias) {
if (!array_key_exists($key, $row)) {
throw new LogicException("the key '" . $key . "' is declared by "
. "the aggregator with alias '" . $alias . "' but is not "
. 'present in results');
if (!\array_key_exists($key, $row)) {
throw new \LogicException("the key '".$key."' is declared by the aggregator with alias '".$alias."' but is not ".'present in results');
}
return $row[$key];
@@ -199,10 +188,8 @@ class CSVFormatter implements FormatterInterface
foreach ($keys as $key) {
$values = array_map(static function ($row) use ($key, $export) {
if (!array_key_exists($key, $row)) {
throw new LogicException("the key '" . $key . "' is declared by "
. "the export with title '" . $export->getTitle() . "' but is not "
. 'present in results');
if (!\array_key_exists($key, $row)) {
throw new \LogicException("the key '".$key."' is declared by the export with title '".$export->getTitle()."' but is not ".'present in results');
}
return $row[$key];
@@ -220,9 +207,9 @@ class CSVFormatter implements FormatterInterface
protected function generateContent()
{
$line = null;
$rowKeysNb = count($this->getRowHeaders());
$columnKeysNb = count($this->getColumnHeaders());
$resultsKeysNb = count($this->export->getQueryKeys($this->exportData));
$rowKeysNb = \count($this->getRowHeaders());
$columnKeysNb = \count($this->getColumnHeaders());
$resultsKeysNb = \count($this->export->getQueryKeys($this->exportData));
$results = $this->getOrderedResults();
/** @var string[] $columnHeaders the column headers associations */
$columnHeaders = [];
@@ -233,9 +220,9 @@ class CSVFormatter implements FormatterInterface
// create a file pointer connected to the output stream
$output = fopen('php://output', 'wb');
//title
// title
fputcsv($output, [$this->translator->trans($this->export->getTitle())]);
//blank line
// blank line
fputcsv($output, ['']);
// add filtering description
@@ -247,32 +234,32 @@ class CSVFormatter implements FormatterInterface
// iterate on result to : 1. populate row headers, 2. populate column headers, 3. add result
foreach ($results as $row) {
$rowHeaders = array_slice($row, 0, $rowKeysNb);
$rowHeaders = \array_slice($row, 0, $rowKeysNb);
//first line : we create line and adding them row headers
// first line : we create line and adding them row headers
if (!isset($line)) {
$line = array_slice($row, 0, $rowKeysNb);
$line = \array_slice($row, 0, $rowKeysNb);
}
// do we have to create a new line ? if the rows are equals, continue on the line, else create a next line
if (array_slice($line, 0, $rowKeysNb) !== $rowHeaders) {
if (\array_slice($line, 0, $rowKeysNb) !== $rowHeaders) {
$contentData[] = $line;
$line = array_slice($row, 0, $rowKeysNb);
$line = \array_slice($row, 0, $rowKeysNb);
}
// add the column headers
/** @var string[] $columns the column for this row */
$columns = array_slice($row, $rowKeysNb, $columnKeysNb);
$columns = \array_slice($row, $rowKeysNb, $columnKeysNb);
$columnPosition = $this->findColumnPosition($columnHeaders, $columns);
//fill with blank at the position given by the columnPosition + nbRowHeaders
// fill with blank at the position given by the columnPosition + nbRowHeaders
for ($i = 0; $i < $columnPosition; ++$i) {
if (!isset($line[$rowKeysNb + $i])) {
$line[$rowKeysNb + $i] = '';
}
}
$resultData = array_slice($row, $resultsKeysNb * -1);
$resultData = \array_slice($row, $resultsKeysNb * -1);
foreach ($resultData as $data) {
$line[] = $data;
@@ -282,7 +269,7 @@ class CSVFormatter implements FormatterInterface
// we add the last line
$contentData[] = $line;
//column title headers
// column title headers
for ($i = 0; $i < $columnKeysNb; ++$i) {
$line = array_fill(0, $rowKeysNb, '');
@@ -293,22 +280,22 @@ class CSVFormatter implements FormatterInterface
$content[] = $line;
}
//row title headers
// row title headers
$headerLine = [];
foreach ($this->getRowHeaders() as $headerKey) {
$headerLine[] = array_key_exists('_header', $this->labels[$headerKey]) ?
$headerLine[] = \array_key_exists('_header', $this->labels[$headerKey]) ?
$this->labels[$headerKey]['_header'] : '';
}
foreach ($this->export->getQueryKeys($this->exportData) as $key) {
$headerLine[] = array_key_exists('_header', $this->labels[$key]) ?
$headerLine[] = \array_key_exists('_header', $this->labels[$key]) ?
$this->labels[$key]['_header'] : '';
}
fputcsv($output, $headerLine);
unset($headerLine); //free memory
unset($headerLine); // free memory
//generate CSV
// generate CSV
foreach ($content as $line) {
fputcsv($output, $line);
}
@@ -390,7 +377,7 @@ class CSVFormatter implements FormatterInterface
++$i;
}
//we didn't find it, adding the column
// we didn't find it, adding the column
$columnHeaders[] = $columnToFind;
return $i++;
@@ -410,12 +397,12 @@ class CSVFormatter implements FormatterInterface
$line = [];
foreach ($headers as $key) {
$line[] = call_user_func($labels[$key], $row[$key]);
$line[] = \call_user_func($labels[$key], $row[$key]);
}
//append result
// append result
foreach ($resultsKeys as $key) {
$line[] = call_user_func($labels[$key], $row[$key]);
$line[] = \call_user_func($labels[$key], $row[$key]);
}
$r[] = $line;
@@ -429,19 +416,17 @@ class CSVFormatter implements FormatterInterface
/**
* @param string $position may be 'c' (column) or 'r' (row)
*
* @throws RuntimeException
*
* @return string[]
*
* @throws \RuntimeException
*/
private function getPositionnalHeaders($position)
{
$headers = [];
foreach ($this->formatterData as $alias => $data) {
if (!array_key_exists($alias, $this->aggregatorsData)) {
throw new RuntimeException('the formatter wants to use the '
. "aggregator with alias {$alias}, but the export do not "
. 'contains data about it');
if (!\array_key_exists($alias, $this->aggregatorsData)) {
throw new \RuntimeException('the formatter wants to use the '."aggregator with alias {$alias}, but the export do not ".'contains data about it');
}
$aggregator = $this->aggregators[$alias];

View File

@@ -13,18 +13,11 @@ namespace Chill\MainBundle\Export\Formatter;
use Chill\MainBundle\Export\ExportManager;
use Chill\MainBundle\Export\FormatterInterface;
use LogicException;
use OutOfBoundsException;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Contracts\Translation\TranslatorInterface;
use function array_key_exists;
use function array_keys;
use function array_map;
use function count;
use function implode;
// command to get the report with curl : curl --user "center a_social:password" "http://localhost:8000/fr/exports/generate/count_person?export[filters][person_gender_filter][enabled]=&export[filters][person_nationality_filter][enabled]=&export[filters][person_nationality_filter][form][nationalities]=&export[aggregators][person_nationality_aggregator][order]=1&export[aggregators][person_nationality_aggregator][form][group_by_level]=country&export[submit]=&export[_token]=RHpjHl389GrK-bd6iY5NsEqrD5UKOTHH40QKE9J1edU" --globoff
@@ -101,11 +94,11 @@ class CSVListFormatter implements FormatterInterface
/**
* Generate a response from the data collected on differents ExportElementInterface.
*
* @param mixed[] $result The result, as given by the ExportInterface
* @param mixed[] $formatterData collected from the current form
* @param string $exportAlias the id of the current export
* @param array $filtersData an array containing the filters data. The key are the filters id, and the value are the data
* @param array $aggregatorsData an array containing the aggregators data. The key are the filters id, and the value are the data
* @param mixed[] $result The result, as given by the ExportInterface
* @param mixed[] $formatterData collected from the current form
* @param string $exportAlias the id of the current export
* @param array $filtersData an array containing the filters data. The key are the filters id, and the value are the data
* @param array $aggregatorsData an array containing the aggregators data. The key are the filters id, and the value are the data
*
* @return \Symfony\Component\HttpFoundation\Response The response to be shown
*/
@@ -150,7 +143,7 @@ class CSVListFormatter implements FormatterInterface
$response = new Response();
$response->setStatusCode(200);
$response->headers->set('Content-Type', 'text/csv; charset=utf-8');
//$response->headers->set('Content-Disposition','attachment; filename="export.csv"');
// $response->headers->set('Content-Disposition','attachment; filename="export.csv"');
$response->setContent($csvContent);
@@ -168,9 +161,9 @@ class CSVListFormatter implements FormatterInterface
* @param string $key
* @param string $value
*
* @throws LogicException if the label is not found
*
* @return string
*
* @throws \LogicException if the label is not found
*/
protected function getLabel($key, $value)
{
@@ -178,15 +171,8 @@ class CSVListFormatter implements FormatterInterface
$this->prepareCacheLabels();
}
if (!array_key_exists($key, $this->labelsCache)) {
throw new OutOfBoundsException(sprintf(
'The key "%s" '
. 'is not present in the list of keys handled by '
. 'this query. Check your `getKeys` and `getLabels` '
. 'methods. Available keys are %s.',
$key,
implode(', ', array_keys($this->labelsCache))
));
if (!\array_key_exists($key, $this->labelsCache)) {
throw new \OutOfBoundsException(sprintf('The key "%s" is not present in the list of keys handled by this query. Check your `getKeys` and `getLabels` methods. Available keys are %s.', $key, \implode(', ', \array_keys($this->labelsCache))));
}
return $this->labelsCache[$key]($value);
@@ -203,7 +189,7 @@ class CSVListFormatter implements FormatterInterface
foreach ($keys as $key) {
// get an array with all values for this key if possible
$values = array_map(static fn ($v) => $v[$key], $this->result);
$values = \array_map(static fn ($v) => $v[$key], $this->result);
// store the label in the labelsCache property
$this->labelsCache[$key] = $export->getLabels($key, $values, $this->exportData);
}
@@ -218,7 +204,7 @@ class CSVListFormatter implements FormatterInterface
{
$keys = $this->exportManager->getExport($this->exportAlias)->getQueryKeys($this->exportData);
// we want to keep the order of the first row. So we will iterate on the first row of the results
$first_row = count($this->result) > 0 ? $this->result[0] : [];
$first_row = \count($this->result) > 0 ? $this->result[0] : [];
$header_line = [];
if (true === $this->formatterData['numerotation']) {
@@ -231,7 +217,7 @@ class CSVListFormatter implements FormatterInterface
);
}
if (count($header_line) > 0) {
if (\count($header_line) > 0) {
fputcsv($output, $header_line);
}
}

View File

@@ -13,15 +13,11 @@ namespace Chill\MainBundle\Export\Formatter;
use Chill\MainBundle\Export\ExportManager;
use Chill\MainBundle\Export\FormatterInterface;
use LogicException;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Contracts\Translation\TranslatorInterface;
use function array_map;
use function count;
/**
* Create a CSV List for the export where the header are printed on the
* first column, and the result goes from left to right.
@@ -97,11 +93,11 @@ class CSVPivotedListFormatter implements FormatterInterface
/**
* Generate a response from the data collected on differents ExportElementInterface.
*
* @param mixed[] $result The result, as given by the ExportInterface
* @param mixed[] $formatterData collected from the current form
* @param string $exportAlias the id of the current export
* @param array $filtersData an array containing the filters data. The key are the filters id, and the value are the data
* @param array $aggregatorsData an array containing the aggregators data. The key are the filters id, and the value are the data
* @param mixed[] $result The result, as given by the ExportInterface
* @param mixed[] $formatterData collected from the current form
* @param string $exportAlias the id of the current export
* @param array $filtersData an array containing the filters data. The key are the filters id, and the value are the data
* @param array $aggregatorsData an array containing the aggregators data. The key are the filters id, and the value are the data
*
* @return \Symfony\Component\HttpFoundation\Response The response to be shown
*/
@@ -139,7 +135,7 @@ class CSVPivotedListFormatter implements FormatterInterface
++$i;
}
//adding the lines to the csv output
// adding the lines to the csv output
foreach ($lines as $line) {
fputcsv($output, $line);
}
@@ -168,9 +164,9 @@ class CSVPivotedListFormatter implements FormatterInterface
* @param string $key
* @param string $value
*
* @throws LogicException if the label is not found
*
* @return string
*
* @throws \LogicException if the label is not found
*/
protected function getLabel($key, $value)
{
@@ -192,7 +188,7 @@ class CSVPivotedListFormatter implements FormatterInterface
foreach ($keys as $key) {
// get an array with all values for this key if possible
$values = array_map(static fn ($v) => $v[$key], $this->result);
$values = \array_map(static fn ($v) => $v[$key], $this->result);
// store the label in the labelsCache property
$this->labelsCache[$key] = $export->getLabels($key, $values, $this->exportData);
}
@@ -207,7 +203,7 @@ class CSVPivotedListFormatter implements FormatterInterface
{
$keys = $this->exportManager->getExport($this->exportAlias)->getQueryKeys($this->exportData);
// we want to keep the order of the first row. So we will iterate on the first row of the results
$first_row = count($this->result) > 0 ? $this->result[0] : [];
$first_row = \count($this->result) > 0 ? $this->result[0] : [];
$header_line = [];
if (true === $this->formatterData['numerotation']) {

View File

@@ -13,7 +13,6 @@ namespace Chill\MainBundle\Export\Formatter;
use Chill\MainBundle\Export\ExportManager;
use Chill\MainBundle\Export\FormatterInterface;
use LogicException;
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\FormType;
@@ -21,19 +20,6 @@ use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Contracts\Translation\TranslatorInterface;
use function array_map;
use function array_merge;
use function array_multisort;
use function array_unique;
use function call_user_func;
use function count;
use function fopen;
use function is_array;
use function stream_get_contents;
use function sys_get_temp_dir;
use function tempnam;
use function unlink;
class SpreadSheetFormatter implements FormatterInterface
{
/**
@@ -60,7 +46,7 @@ class SpreadSheetFormatter implements FormatterInterface
*
* @var type
*/
//protected $aggregators;
// protected $aggregators;
/**
* array containing value of export form.
@@ -104,7 +90,7 @@ class SpreadSheetFormatter implements FormatterInterface
*
* @var array
*/
//protected $labels;
// protected $labels;
/**
* temporary file to store spreadsheet.
@@ -157,7 +143,7 @@ class SpreadSheetFormatter implements FormatterInterface
// ordering aggregators
$aggregators = $this->exportManager->getAggregators($aggregatorAliases);
$nb = count($aggregatorAliases);
$nb = \count($aggregatorAliases);
foreach ($aggregators as $alias => $aggregator) {
$builderAggregator = $builder->create($alias, FormType::class, [
@@ -212,15 +198,15 @@ class SpreadSheetFormatter implements FormatterInterface
$this->getContentType($this->formatterData['format'])
);
$this->tempfile = tempnam(sys_get_temp_dir(), '');
$this->tempfile = \tempnam(\sys_get_temp_dir(), '');
$this->generateContent();
$f = fopen($this->tempfile, 'rb');
$response->setContent(stream_get_contents($f));
$f = \fopen($this->tempfile, 'rb');
$response->setContent(\stream_get_contents($f));
fclose($f);
// remove the temp file from disk
unlink($this->tempfile);
\unlink($this->tempfile);
return $response;
}
@@ -238,11 +224,11 @@ class SpreadSheetFormatter implements FormatterInterface
$worksheet->fromArray(
$sortedResults,
null,
'A' . $line,
'A'.$line,
true
);
return $line + count($sortedResults) + 1;
return $line + \count($sortedResults) + 1;
}
/**
@@ -260,7 +246,7 @@ class SpreadSheetFormatter implements FormatterInterface
$filter = $this->exportManager->getFilter($alias);
$description = $filter->describeAction($data, 'string');
if (is_array($description)) {
if (\is_array($description)) {
$description = $this->translator
->trans(
$description[0],
@@ -268,7 +254,7 @@ class SpreadSheetFormatter implements FormatterInterface
);
}
$worksheet->setCellValue('A' . $line, $description);
$worksheet->setCellValue('A'.$line, $description);
++$line;
}
@@ -303,7 +289,7 @@ class SpreadSheetFormatter implements FormatterInterface
$worksheet->fromArray(
$displayables,
null,
'A' . $line
'A'.$line
);
return $line + 1;
@@ -359,7 +345,7 @@ class SpreadSheetFormatter implements FormatterInterface
'ods' => \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, 'Ods'),
'xlsx' => \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, 'Xlsx'),
'csv' => \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, 'Csv'),
default => throw new LogicException(),
default => throw new \LogicException(),
};
$writer->save($this->tempfile);
@@ -383,7 +369,7 @@ class SpreadSheetFormatter implements FormatterInterface
$aggregator = $this->exportManager->getAggregator($alias);
$aggregatorsKeys = $aggregator->getQueryKeys($data);
// append the keys from aggregator to the $keys existing array
$keys = array_merge($keys, $aggregatorsKeys);
$keys = \array_merge($keys, $aggregatorsKeys);
// append the key with the alias, which will be use later for sorting
foreach ($aggregatorsKeys as $key) {
$aggregatorKeyAssociation[$key] = $alias;
@@ -420,7 +406,7 @@ class SpreadSheetFormatter implements FormatterInterface
case 'xlsx':
return 'application/vnd.openxmlformats-officedocument.'
. 'spreadsheetml.sheet';
.'spreadsheetml.sheet';
}
}
@@ -439,7 +425,7 @@ class SpreadSheetFormatter implements FormatterInterface
$value ??= '';
return call_user_func($this->cacheDisplayableResult[$key], $value);
return \call_user_func($this->cacheDisplayableResult[$key], $value);
}
protected function getTitle()
@@ -447,7 +433,7 @@ class SpreadSheetFormatter implements FormatterInterface
$title = $this->translator->trans($this->export->getTitle());
if (30 < strlen($title)) {
return substr($title, 0, 30) . '…';
return substr($title, 0, 30).'…';
}
return $title;
@@ -499,7 +485,7 @@ class SpreadSheetFormatter implements FormatterInterface
$this->cacheDisplayableResult[$key] = $element->getLabels($key, ['_header'], $data);
} else {
$this->cacheDisplayableResult[$key] =
$element->getLabels($key, array_unique($allValues[$key]), $data);
$element->getLabels($key, \array_unique($allValues[$key]), $data);
}
}
@@ -548,9 +534,9 @@ class SpreadSheetFormatter implements FormatterInterface
$exportKeys = $this->export->getQueryKeys($this->exportData);
$aggregatorKeys = $this->getAggregatorKeysSorted();
$globalKeys = array_merge($aggregatorKeys, $exportKeys);
$globalKeys = \array_merge($aggregatorKeys, $exportKeys);
$sortedResult = array_map(function ($row) use ($globalKeys) {
$sortedResult = \array_map(function ($row) use ($globalKeys) {
$newRow = [];
foreach ($globalKeys as $key) {
@@ -560,7 +546,7 @@ class SpreadSheetFormatter implements FormatterInterface
return $newRow;
}, $this->result);
array_multisort($sortedResult);
\array_multisort($sortedResult);
return [$sortedResult, $exportKeys, $aggregatorKeys, $globalKeys];
}

View File

@@ -13,29 +13,15 @@ namespace Chill\MainBundle\Export\Formatter;
use Chill\MainBundle\Export\ExportManager;
use Chill\MainBundle\Export\FormatterInterface;
use DateTimeInterface;
use LogicException;
use OutOfBoundsException;
use PhpOffice\PhpSpreadsheet\Shared\Date;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Style\NumberFormat;
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
use RuntimeException;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Contracts\Translation\TranslatorInterface;
use function array_key_exists;
use function array_keys;
use function array_map;
use function count;
use function fopen;
use function implode;
use function stream_get_contents;
use function sys_get_temp_dir;
use function tempnam;
use function unlink;
// command to get the report with curl : curl --user "center a_social:password" "http://localhost:8000/fr/exports/generate/count_person?export[filters][person_gender_filter][enabled]=&export[filters][person_nationality_filter][enabled]=&export[filters][person_nationality_filter][form][nationalities]=&export[aggregators][person_nationality_aggregator][order]=1&export[aggregators][person_nationality_aggregator][form][group_by_level]=country&export[submit]=&export[_token]=RHpjHl389GrK-bd6iY5NsEqrD5UKOTHH40QKE9J1edU" --globoff
@@ -120,11 +106,11 @@ class SpreadsheetListFormatter implements FormatterInterface
/**
* Generate a response from the data collected on differents ExportElementInterface.
*
* @param mixed[] $result The result, as given by the ExportInterface
* @param mixed[] $formatterData collected from the current form
* @param string $exportAlias the id of the current export
* @param array $filtersData an array containing the filters data. The key are the filters id, and the value are the data
* @param array $aggregatorsData an array containing the aggregators data. The key are the filters id, and the value are the data
* @param mixed[] $result The result, as given by the ExportInterface
* @param mixed[] $formatterData collected from the current form
* @param string $exportAlias the id of the current export
* @param array $filtersData an array containing the filters data. The key are the filters id, and the value are the data
* @param array $aggregatorsData an array containing the aggregators data. The key are the filters id, and the value are the data
*
* @return \Symfony\Component\HttpFoundation\Response The response to be shown
*/
@@ -150,20 +136,20 @@ class SpreadsheetListFormatter implements FormatterInterface
foreach ($result as $row) {
if (true === $this->formatterData['numerotation']) {
$worksheet->setCellValue('A' . ($i + 1), (string) $i);
$worksheet->setCellValue('A'.($i + 1), (string) $i);
}
$a = $this->formatterData['numerotation'] ? 'B' : 'A';
foreach ($row as $key => $value) {
$row = $a . ($i + 1);
$row = $a.($i + 1);
$formattedValue = $this->getLabel($key, $value);
if ($formattedValue instanceof DateTimeInterface) {
if ($formattedValue instanceof \DateTimeInterface) {
$worksheet->setCellValue($row, Date::PHPToExcel($formattedValue));
if ($formattedValue->format('His') === '000000') {
if ('000000' === $formattedValue->format('His')) {
$worksheet->getStyle($row)
->getNumberFormat()
->setFormatCode(NumberFormat::FORMAT_DATE_DDMMYYYY);
@@ -191,7 +177,7 @@ class SpreadsheetListFormatter implements FormatterInterface
case 'xlsx':
$writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, 'Xlsx');
$contentType = 'application/vnd.openxmlformats-officedocument.'
. 'spreadsheetml.sheet';
.'spreadsheetml.sheet';
break;
@@ -204,22 +190,21 @@ class SpreadsheetListFormatter implements FormatterInterface
default:
// this should not happen
// throw an exception to ensure that the error is catched
throw new OutOfBoundsException('The format ' . $this->formatterData['format'] .
' is not supported');
throw new \OutOfBoundsException('The format '.$this->formatterData['format'].' is not supported');
}
$response = new Response();
$response->headers->set('content-type', $contentType);
$tempfile = tempnam(sys_get_temp_dir(), '');
$tempfile = \tempnam(\sys_get_temp_dir(), '');
$writer->save($tempfile);
$f = fopen($tempfile, 'rb');
$response->setContent(stream_get_contents($f));
$f = \fopen($tempfile, 'rb');
$response->setContent(\stream_get_contents($f));
fclose($f);
// remove the temp file from disk
unlink($tempfile);
\unlink($tempfile);
return $response;
}
@@ -235,9 +220,9 @@ class SpreadsheetListFormatter implements FormatterInterface
* @param string $key
* @param string $value
*
* @throws LogicException if the label is not found
*
* @return string
*
* @throws \LogicException if the label is not found
*/
protected function getLabel($key, $value)
{
@@ -245,15 +230,8 @@ class SpreadsheetListFormatter implements FormatterInterface
$this->prepareCacheLabels();
}
if (!array_key_exists($key, $this->labelsCache)) {
throw new OutOfBoundsException(sprintf(
'The key "%s" '
. 'is not present in the list of keys handled by '
. 'this query. Check your `getKeys` and `getLabels` '
. 'methods. Available keys are %s.',
$key,
implode(', ', array_keys($this->labelsCache))
));
if (!\array_key_exists($key, $this->labelsCache)) {
throw new \OutOfBoundsException(sprintf('The key "%s" is not present in the list of keys handled by this query. Check your `getKeys` and `getLabels` methods. Available keys are %s.', $key, \implode(', ', \array_keys($this->labelsCache))));
}
return $this->labelsCache[$key]($value);
@@ -270,9 +248,9 @@ class SpreadsheetListFormatter implements FormatterInterface
foreach ($keys as $key) {
// get an array with all values for this key if possible
$values = array_map(static function ($v) use ($key) {
if (!array_key_exists($key, $v)) {
throw new RuntimeException(sprintf('This key does not exists: %s. Available keys are %s', $key, implode(', ', array_keys($v))));
$values = \array_map(static function ($v) use ($key) {
if (!\array_key_exists($key, $v)) {
throw new \RuntimeException(sprintf('This key does not exists: %s. Available keys are %s', $key, \implode(', ', \array_keys($v))));
}
return $v[$key];
@@ -289,7 +267,7 @@ class SpreadsheetListFormatter implements FormatterInterface
{
$keys = $this->exportManager->getExport($this->exportAlias)->getQueryKeys($this->exportData);
// we want to keep the order of the first row. So we will iterate on the first row of the results
$first_row = count($this->result) > 0 ? $this->result[0] : [];
$first_row = \count($this->result) > 0 ? $this->result[0] : [];
$header_line = [];
if (true === $this->formatterData['numerotation']) {
@@ -302,7 +280,7 @@ class SpreadsheetListFormatter implements FormatterInterface
);
}
if (count($header_line) > 0) {
if (\count($header_line) > 0) {
$worksheet->fromArray($header_line, null, 'A1');
}
}