Skip to content

Commit

Permalink
feature: dont auto detect datetime
Browse files Browse the repository at this point in the history
  • Loading branch information
krissss committed Jan 14, 2025
1 parent 57cdc1b commit 3324552
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 18 deletions.
2 changes: 2 additions & 0 deletions src/DataExporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
*/
class DataExporter
{
public static $defaultDateTimeFormat = 'Y-m-d H:i:s'; // 默认的时间展示格式

/**
* @var ContainerContract
*/
Expand Down
6 changes: 4 additions & 2 deletions src/DataExporter/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Illuminate\Contracts\Support\Arrayable;
use InvalidArgumentException;
use Iterator;
use Kriss\DataExporter\DataExporter;
use Kriss\DataExporter\Source\ExcelSheetSourceIterator;
use Kriss\DataExporter\Source\GeneratorChainSourceIterator;
use Kriss\DataExporter\Traits\ObjectEventsSupportTrait;
Expand Down Expand Up @@ -146,8 +147,9 @@ private function prepareData(WriterInterface $writer, array $data)
if ($value instanceof TypedExpression) {
return $value->getRawValue();
}
if (is_object($value)) {
return (string) $value;
// 日期转字符串
if ($value instanceof \DateTimeInterface) {
return $value->format(DataExporter::$defaultDateTimeFormat);
}

return $value;
Expand Down
15 changes: 7 additions & 8 deletions src/Writer/Expression/TypedExpression.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Kriss\DataExporter\Writer\Expression;

use Box\Spout\Common\Entity\Cell as SpoutDataType;
use Kriss\DataExporter\DataExporter;
use PhpOffice\PhpSpreadsheet\Cell\DataType as SpreadsheetDataType;

class TypedExpression
Expand All @@ -20,6 +21,10 @@ class TypedExpression

public function __construct($expression, int $type)
{
if ($type === self::TYPE_DATE && $expression instanceof \DateTimeInterface) {
$expression = $expression->format(DATE_ATOM); // 按照 ISO 格式转化时间
}

$this->expression = $expression;
$this->type = $type;
}
Expand All @@ -37,13 +42,6 @@ public function getValue()
if ($this->type === self::TYPE_BOOLEAN) {
return (bool)$this->expression;
}
if ($this->type === self::TYPE_DATE) {
if ($this->expression instanceof \DateTimeInterface) {
return $this->expression->format(DATE_ATOM);
}

return $this->expression;
}
if ($this->type === self::TYPE_EMPTY) {
return null;
}
Expand Down Expand Up @@ -101,8 +99,9 @@ public static function fromValue($value): self
/*if (is_string($value) && isset($value[0]) && $value[0] === '=') {
return new self($value, self::TYPE_FORMULA);
}*/
// 将日期按照字符串解析,这样可以正常展示
if ($value instanceof \DateTimeInterface) {
return new self($value, self::TYPE_DATE);
return new self($value->format(DataExporter::$defaultDateTimeFormat), self::TYPE_STRING);
}

return new self($value, self::TYPE_STRING);
Expand Down
8 changes: 7 additions & 1 deletion src/Writer/XlsxSpoutWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@ private function parseData($value): array
{
$typed = TypedExpression::fromValue($value);

return [$typed->getSpoutType(), $typed->getRawValue()];
$type = $typed->getSpoutType();
$value = $typed->getValue();
if ($type === Cell::TYPE_DATE) {
$value = new \DateTime($value);
}

return [$type, $value];
}
}
48 changes: 41 additions & 7 deletions tests/Feature/WriterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,15 +114,25 @@
'bool_false' => false,
'bool_true' => true,
'null' => null,
'date' => '2021-01-01',
'datetime' => '2021-01-01 12:00:00',
'time' => '12:00:00',
'date_carbon' => Carbon::now(),
// 时间
'date' => new TypedExpression('2021-01-01', TypedExpression::TYPE_DATE),
'datetime' => new TypedExpression('2021-01-01 12:52:18', TypedExpression::TYPE_DATE),
'time' => new TypedExpression('12:52:18', TypedExpression::TYPE_DATE),
'date_carbon' => new TypedExpression(Carbon::now(), TypedExpression::TYPE_DATE),
'date_datetime_obj' => new TypedExpression(new DateTime(), TypedExpression::TYPE_DATE),
'date_string' => '2021-01-01', // 不自动识别
'datetime_string' => '2021-01-01 12:52:18', // 不自动识别
'time_string' => '12:12:52', // 不自动识别
'carbon_string' => Carbon::now(), // 会处理成时间字符串
'datetime_obj_string' => new DateTime(), // 会处理成时间字符串
// 超链接
'hyperlink' => new HyperLinkExpression('https://www.baidu.com', '百度'),
'hyperlink2' => new HyperLinkExpression('https://www.baidu.com?name=a"b', '百"度'),
'formula' => new TypedExpression('=SUM(G2:H2)', TypedExpression::TYPE_FORMULA), // 手动指定公式
'formula_string' => '=SUM(G2:H2)', // 不自动识别公式
'formula_string2' => '=(WX000', // 不自动识别公式
'hyperlink_string' => 'https://www.baidu.com', // 不自动识别
// 公式
'formula' => new TypedExpression('=SUM(G2:H2)', TypedExpression::TYPE_FORMULA),
'formula_string' => '=SUM(G2:H2)', // 不自动识别
'formula_string2' => '=(WX000', // 不自动识别
],
];

Expand All @@ -133,3 +143,27 @@
// check by person
expect(true)->toBeTrue();
});

it('change default datetime format', function () {
$defaultDateTimeFormat = DataExporter::$defaultDateTimeFormat;

// 修改默认的时间格式
DataExporter::$defaultDateTimeFormat = 'Y/m/d H-i-s';

$source = [
[
'carbon_string' => Carbon::now(), // 会处理成时间字符串
'datetime_obj_string' => new DateTime(), // 会处理成时间字符串
],
];

DataExporter::xlsx($source)->saveAs($this->filename . '-xlsx');
DataExporter::xlsxSpreadsheet($source)->saveAs($this->filename . '-xlsx-spreadsheet');
DataExporter::xlsxSpout($source)->saveAs($this->filename . '-xlsx-spout');

// check by person
expect(true)->toBeTrue();

// 重置回来,防止影响其他测试用例
DataExporter::$defaultDateTimeFormat = $defaultDateTimeFormat;
});

0 comments on commit 3324552

Please sign in to comment.