Skip to content

Commit

Permalink
refactor(Service): use constants instead of hard-coded strings
Browse files Browse the repository at this point in the history
Signed-off-by: Arthur Schiwon <[email protected]>
  • Loading branch information
blizzz authored and backportbot[bot] committed Feb 19, 2025
1 parent 18de832 commit 2b4268c
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 26 deletions.
7 changes: 7 additions & 0 deletions lib/Db/Column.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,13 @@ class Column extends Entity implements JsonSerializable {
public const TYPE_DATETIME = 'datetime';
public const TYPE_USERGROUP = 'usergroup';

public const SUBTYPE_DATETIME_DATE = 'date';
public const SUBTYPE_DATETIME_TIME = 'time';

public const SUBTYPE_SELECTION_CHECK = 'check';

public const SUBTYPE_TEXT_LINE = 'line';

protected ?string $title = null;
protected ?int $tableId = null;
protected ?string $createdBy = null;
Expand Down
74 changes: 48 additions & 26 deletions lib/Service/ImportService.php
Original file line number Diff line number Diff line change
Expand Up @@ -163,20 +163,38 @@ private function getPreviewData(Worksheet $worksheet): array {
$colIndex = $cellIterator->getCurrentColumnIndex() - 1;
$column = $this->columns[$colIndex];

if (($column && $column->getType() === 'datetime') || (is_array($columns[$colIndex]) && $columns[$colIndex]['type'] === 'datetime')) {
if (isset($columns[$colIndex]['subtype']) && $columns[$colIndex]['subtype'] === 'date') {
if (
($column && $column->getType() === Column::TYPE_DATETIME)
|| (is_array($columns[$colIndex])
&& $columns[$colIndex]['type'] === Column::TYPE_DATETIME)
) {
if (isset($columns[$colIndex]['subtype'])
&& $columns[$colIndex]['subtype'] === Column::SUBTYPE_DATETIME_DATE
) {
$format = 'Y-m-d';
} elseif (isset($columns[$colIndex]['subtype']) && $columns[$colIndex]['subtype'] === 'time') {
} elseif (isset($columns[$colIndex]['subtype'])
&& $columns[$colIndex]['subtype'] === Column::SUBTYPE_DATETIME_TIME
) {
$format = 'H:i';
} else {
$format = 'Y-m-d H:i';
}
$value = $this->parseAndFormatDateTimeString($value, $format);
} elseif (($column && $column->getType() === 'number' && $column->getNumberSuffix() === '%')
|| (is_array($columns[$colIndex]) && $columns[$colIndex]['type'] === 'number' && $columns[$colIndex]['numberSuffix'] === '%')) {
} elseif (
($column && $column->getType() === Column::TYPE_NUMBER
&& $column->getNumberSuffix() === '%')
|| (is_array($columns[$colIndex])
&& $columns[$colIndex]['type'] === Column::TYPE_NUMBER
&& $columns[$colIndex]['numberSuffix'] === '%')
) {
$value = $value * 100;
} elseif (($column && $column->getType() === 'selection' && $column->getSubtype() === 'check')
|| (is_array($columns[$colIndex]) && $columns[$colIndex]['type'] === 'selection' && $columns[$colIndex]['subtype'] === 'check')) {
} elseif (
($column && $column->getType() === Column::TYPE_SELECTION
&& $column->getSubtype() === Column::SUBTYPE_SELECTION_CHECK)
|| (is_array($columns[$colIndex])
&& $columns[$colIndex]['type'] === Column::TYPE_SELECTION
&& $columns[$colIndex]['subtype'] === Column::SUBTYPE_SELECTION_CHECK)
) {
$value = $cell->getFormattedValue() === 'TRUE' ? 'true' : 'false';
}

Expand Down Expand Up @@ -379,18 +397,22 @@ private function createRow(Row $row): void {
$value = $cell->getValue();
$hasData = $hasData || !empty($value);

if ($column->getType() === 'datetime') {
if ($column->getSubtype() === 'date') {
if ($column->getType() === Column::TYPE_DATETIME) {
if ($column->getSubtype() === Column::SUBTYPE_DATETIME_DATE) {
$format = 'Y-m-d';
} elseif ($column->getSubtype() === 'time') {
} elseif ($column->getSubtype() === Column::SUBTYPE_DATETIME_TIME) {
$format = 'H:i';
} else {
$format = 'Y-m-d H:i';
}
$value = $this->parseAndFormatDateTimeString($value, $format);
} elseif ($column->getType() === 'number' && $column->getNumberSuffix() === '%') {
} elseif ($column->getType() === Column::TYPE_NUMBER
&& $column->getNumberSuffix() === '%'
) {
$value = $value * 100;
} elseif ($column->getType() === 'selection' && $column->getSubtype() === 'check') {
} elseif ($column->getType() === Column::TYPE_SELECTION
&& $column->getSubtype() === Column::SUBTYPE_SELECTION_CHECK
) {
$value = $cell->getFormattedValue() === 'TRUE' ? 'true' : 'false';
}

Expand Down Expand Up @@ -537,8 +559,8 @@ private function parseColumnDataType(Cell $cell): array {
$value = $cell->getValue();
$formattedValue = $cell->getFormattedValue();
$dataType = [
'type' => 'text',
'subtype' => 'line',
'type' => Column::TYPE_TEXT,
'subtype' => Column::SUBTYPE_TEXT_LINE,
];

if (!is_numeric($formattedValue)
Expand All @@ -552,66 +574,66 @@ private function parseColumnDataType(Cell $cell): array {
$containsTime = $dateAnalysis['hour'] !== false || $dateAnalysis['minute'] !== false || $dateAnalysis['second'] !== false;

if ($containsDate && !$containsTime) {
$subType = 'date';
$subType = Column::SUBTYPE_DATETIME_DATE;
} elseif (!$containsDate && $containsTime) {
$subType = 'time';
$subType = Column::SUBTYPE_DATETIME_TIME;
} else {
$subType = '';
}

$dataType = [
'type' => 'datetime',
'type' => Column::TYPE_DATETIME,
'subtype' => $subType,
];
} elseif ($originDataType === DataType::TYPE_NUMERIC) {
if (str_contains($formattedValue, '%')) {
$dataType = [
'type' => 'number',
'type' => Column::TYPE_NUMBER,
'number_decimals' => 2,
'number_suffix' => '%',
];
} elseif (str_contains($formattedValue, '')) {
$dataType = [
'type' => 'number',
'type' => Column::TYPE_NUMBER,
'number_decimals' => 2,
'number_suffix' => '',
];
} elseif (str_contains($formattedValue, 'EUR')) {
$dataType = [
'type' => 'number',
'type' => Column::TYPE_NUMBER,
'number_decimals' => 2,
'number_suffix' => 'EUR',
];
} elseif (str_contains($formattedValue, '$')) {
$dataType = [
'type' => 'number',
'type' => Column::TYPE_NUMBER,
'number_decimals' => 2,
'number_prefix' => '$',
];
} elseif (str_contains($formattedValue, 'USD')) {
$dataType = [
'type' => 'number',
'type' => Column::TYPE_NUMBER,
'number_decimals' => 2,
'number_suffix' => 'USD',
];
} elseif (is_float($value)) {
$decimals = strlen(substr(strrchr((string)$value, "."), 1));
$dataType = [
'type' => 'number',
'type' => Column::TYPE_NUMBER,
'number_decimals' => $decimals,
];
} else {
$dataType = [
'type' => 'number',
'type' => Column::TYPE_NUMBER,
];
}
} elseif ($originDataType === DataType::TYPE_BOOL
|| ($originDataType === DataType::TYPE_FORMULA
&& in_array($formattedValue, ['FALSE', 'TRUE'], true))
) {
$dataType = [
'type' => 'selection',
'subtype' => 'check',
'type' => Column::TYPE_SELECTION,
'subtype' => Column::SUBTYPE_SELECTION_CHECK,
'selection_default' => 'false',
];
}
Expand Down

0 comments on commit 2b4268c

Please sign in to comment.