Skip to content

Commit

Permalink
Change use of deprecated methods
Browse files Browse the repository at this point in the history
correct imports
  • Loading branch information
tansautn committed Oct 14, 2024
1 parent 80280e1 commit 9568d60
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 10 deletions.
42 changes: 35 additions & 7 deletions src/Traits/ExcelExportable.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,14 @@
namespace Zuko\Flex2Cell\Traits;


use PhpOffice\PhpSpreadsheet\Cell\Coordinate;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;

/**
* Class ExcelExportable
*
* @package App\Traits\ExcelExport
* @package Zuko\Flex2Cell\Traits
*/
trait ExcelExportable
{
Expand All @@ -56,6 +57,10 @@ trait ExcelExportable
* @var bool
*/
protected $skipHeader = false;
/**
* @var array
*/
protected $columnLetters = [];

/**
* @param $data
Expand Down Expand Up @@ -234,10 +239,10 @@ protected function writeHeaders($sheet)
{
$columnIndex = 1;
foreach ($this->headers as $header) {
if (!in_array($header, $this->hiddens)) {
$sheet->setCellValueByColumnAndRow($columnIndex, 1, $this->getHeader($header));
if (!in_array($header, $this->hiddens, true)) {
$sheet->setCellValue([$columnIndex, 1], $this->getHeader($header));
if (isset($this->subHeaders[$header])) {
$sheet->setCellValueByColumnAndRow($columnIndex, 2, $this->getSubHeader($header));
$sheet->setCellValue([$columnIndex, 2], $this->getSubHeader($header));
}
$columnIndex++;
}
Expand All @@ -257,10 +262,10 @@ protected function writeRow($sheet, $row, $rowIndex)
{
$columnIndex = 1;
foreach ($this->mapping as $key => $header) {
if (!in_array($header, $this->hiddens)) {
if (!in_array($header, $this->hiddens, true)) {
$value = $this->getValue($row, $key);
$value = $this->formatValue($header, $value);
$sheet->setCellValueByColumnAndRow($columnIndex++, $rowIndex, $value);
$sheet->setCellValue([$columnIndex++, $rowIndex], $value);
}
}
}
Expand Down Expand Up @@ -343,6 +348,17 @@ protected function getValue($row, $key)
*
* @return mixed The formatted value.
*/
protected function formatValue($mappingKey, $value)
{
return $this->parentFormat($mappingKey, $value);
}

/**
* Get the column letter for a mapping key.
*
* @param string $mappingKey
* @return string
*/
protected function getColumnLetter($mappingKey)
{
if (!isset($this->columnLetters[$mappingKey])) {
Expand All @@ -352,11 +368,23 @@ protected function getColumnLetter($mappingKey)
return $this->columnLetters[$mappingKey];
}

/**
* Get the mapping key from a header.
*
* @param string $header The header
* @return string The mapping key if found, null otherwise
*/
protected function getMappingKeyFromHeader($header)
{
return array_search($header, $this->mapping);
return array_search($header, $this->mapping, true);
}

/**
* Get a header from a mapping key.
*
* @param string $mappingKey The mapping key
* @return string The header if found, null otherwise
*/
protected function getHeaderFromMappingKey($mappingKey)
{
return $this->mapping[$mappingKey] ?? null;
Expand Down
28 changes: 26 additions & 2 deletions src/Traits/HasExportAttributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,24 @@
/**
* Class HasExportAttributes
*
* @package App\Traits\ExcelExport
* @package Zuko\Flex2Cell\Traits
*/
trait HasExportAttributes
{
protected $formatters = [];

/**
* Set the formatters to be used for formatting values when exporting.
*
* $formatters is an associative array, where the keys are the column names
* and the values are either a callable or an object that implements
* FormatterInterface. If the value is a string, it will be treated as a class
* name and an instance of that class will be created.
*
* @param array|callable[]|\Zuko\Flex2Cell\Contracts\FormatterInterface[] $formatters An associative array of formatters.
*
* @return static
*/
public function setFormatters(array $formatters)
{
foreach ($formatters as $key => $formatter) {
Expand All @@ -52,7 +64,19 @@ public function setFormatters(array $formatters)
}
return $this;
}

/**
* Format a value for export.
*
* This method is called once for each value that is exported.
* The default implementation simply returns the value as is,
* but you can override this method in your class to change the
* behavior.
*
* @param string $mappingKey The key of the mapped column that is being exported.
* @param mixed $value The value that is being exported.
*
* @return mixed The formatted value.
*/
protected function formatValue($mappingKey, $value)
{
if (isset($this->formatters[$mappingKey])) {
Expand Down
2 changes: 1 addition & 1 deletion src/Traits/HasExportMerging.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
/**
* Class HasExportMerging
*
* @package App\Traits\ExcelExport
* @package Zuko\Flex2Cell\Traits
*/
trait HasExportMerging
{
Expand Down

0 comments on commit 9568d60

Please sign in to comment.