Skip to content

Commit d9ae0b9

Browse files
committed
chore: split value and ID parsing into separate methods
Signed-off-by: ailkiv <a.ilkiv.ye@gmail.com>
1 parent 6e8b98b commit d9ae0b9

File tree

3 files changed

+47
-25
lines changed

3 files changed

+47
-25
lines changed

lib/Service/ColumnTypes/SelectionBusiness.php

Lines changed: 37 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,24 @@ public function parseValue($value, ?Column $column = null): string {
2323
}
2424

2525
$intValue = (int)$value;
26-
if ((string)$intValue === (string)$value) {
27-
// if it seems to be an option ID
28-
foreach ($column->getSelectionOptionsArray() as $option) {
29-
if ($option['id'] === $intValue && $option['label'] !== $value) {
30-
return json_encode($option['id']);
31-
}
26+
foreach ($column->getSelectionOptionsArray() as $option) {
27+
if ($option['id'] === $intValue) {
28+
return json_encode($option['id']);
3229
}
33-
} else {
34-
foreach ($column->getSelectionOptionsArray() as $option) {
35-
if ($option['label'] === $value) {
36-
return json_encode($option['id']);
37-
}
30+
}
31+
32+
return '';
33+
}
34+
35+
public function parseDisplayValue($value, ?Column $column = null): string {
36+
if (!$column) {
37+
$this->logger->warning('No column given, but expected on ' . __FUNCTION__ . ' within ' . __CLASS__, ['exception' => new \Exception()]);
38+
return '';
39+
}
40+
41+
foreach ($column->getSelectionOptionsArray() as $option) {
42+
if ($option['label'] === $value) {
43+
return json_encode($option['id']);
3844
}
3945
}
4046

@@ -56,22 +62,30 @@ public function canBeParsed($value, ?Column $column = null): bool {
5662
}
5763

5864
$intValue = (int)$value;
59-
if ((string)$intValue === (string)$value) {
60-
// if it seems to be an option ID
61-
foreach ($column->getSelectionOptionsArray() as $option) {
62-
if ($option['id'] === $intValue && $option['label'] !== $value) {
63-
return true;
64-
}
65-
}
66-
} else {
67-
foreach ($column->getSelectionOptionsArray() as $option) {
68-
if ($option['label'] === $value) {
69-
return true;
70-
}
65+
foreach ($column->getSelectionOptionsArray() as $option) {
66+
if ($option['id'] === $intValue) {
67+
return true;
7168
}
7269
}
7370

7471
return false;
7572
}
7673

74+
public function canBeParsedDisplayValue($value, ?Column $column = null): bool {
75+
if (!$column) {
76+
$this->logger->warning('No column given, but expected on ' . __FUNCTION__ . ' within ' . __CLASS__, ['exception' => new \Exception()]);
77+
return false;
78+
}
79+
if ($value === null) {
80+
return true;
81+
}
82+
83+
foreach ($column->getSelectionOptionsArray() as $option) {
84+
if ($option['label'] === $value) {
85+
return true;
86+
}
87+
}
88+
89+
return false;
90+
}
7791
}

lib/Service/ColumnTypes/SuperBusiness.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ public function parseValue($value, ?Column $column = null): string {
2828
return json_encode($value);
2929
}
3030

31+
public function parseDisplayValue($value, ?Column $column = null): string {
32+
return $this->parseValue($value, $column);
33+
}
34+
3135
/**
3236
* @param mixed $value
3337
* @param Column|null $column
@@ -37,6 +41,10 @@ public function canBeParsed($value, ?Column $column = null): bool {
3741
return true;
3842
}
3943

44+
public function canBeParsedDisplayValue($value, ?Column $column = null): bool {
45+
return $this->canBeParsed($value, $column);
46+
}
47+
4048
protected function isValidDate(string $dateString, string $format): bool {
4149
try {
4250
$dateTime = new DateTime($dateString);

lib/Service/ImportService.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -347,12 +347,12 @@ private function parseValueByColumnType(string $value, Column $column): string {
347347
$businessClassName .= ucfirst($column->getType()) . ucfirst($column->getSubtype()) . 'Business';
348348
/** @var IColumnTypeBusiness $columnBusiness */
349349
$columnBusiness = Server::get($businessClassName);
350-
if (!$columnBusiness->canBeParsed($value, $column)) {
350+
if (!$columnBusiness->canBeParsedDisplayValue($value, $column)) {
351351
$this->logger->warning('Value ' . $value . ' could not be parsed for column ' . $column->getTitle());
352352
$this->countParsingErrors++;
353353
return '';
354354
}
355-
return $columnBusiness->parseValue($value, $column);
355+
return $columnBusiness->parseDisplayValue($value, $column);
356356
} catch (NotFoundExceptionInterface|ContainerExceptionInterface $e) {
357357
$this->logger->debug('Column type business class not found', ['exception' => $e]);
358358
}

0 commit comments

Comments
 (0)