Skip to content

Commit 49ff88b

Browse files
AIlkivenjeck
authored andcommitted
chore: split value and ID parsing into separate methods
Signed-off-by: ailkiv <a.ilkiv.ye@gmail.com>
1 parent a684487 commit 49ff88b

4 files changed

Lines changed: 66 additions & 26 deletions

File tree

lib/Service/ColumnTypes/IColumnTypeBusiness.php

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,29 @@ interface IColumnTypeBusiness {
2525
public function parseValue($value, ?Column $column): string;
2626

2727
/**
28-
* tests if the given string can be parsed to a value of the column type
28+
* tests if the given value can be parsed to a value of the column type
2929
*
3030
* @param mixed $value
3131
* @param Column|null $column
3232
* @return bool
3333
*/
3434
public function canBeParsed($value, ?Column $column): bool;
35+
36+
/**
37+
* tests if the given string can be parsed to a value/id of the column type
38+
*
39+
* @param mixed $value
40+
* @param Column|null $column
41+
* @return bool
42+
*/
43+
public function canBeParsedDisplayValue($value, ?Column $column): bool;
44+
45+
/**
46+
* parses the given string to a value/id of the column type
47+
*
48+
* @param mixed $value
49+
* @param Column|null $column
50+
* @return string
51+
*/
52+
public function parseDisplayValue($value, ?Column $column): string;
3553
}

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
@@ -366,12 +366,12 @@ private function parseValueByColumnType(string $value, Column $column): string {
366366
$businessClassName .= ucfirst($column->getType()) . ucfirst($column->getSubtype()) . 'Business';
367367
/** @var IColumnTypeBusiness $columnBusiness */
368368
$columnBusiness = Server::get($businessClassName);
369-
if (!$columnBusiness->canBeParsed($value, $column)) {
369+
if (!$columnBusiness->canBeParsedDisplayValue($value, $column)) {
370370
$this->logger->warning('Value ' . $value . ' could not be parsed for column ' . $column->getTitle());
371371
$this->countParsingErrors++;
372372
return '';
373373
}
374-
return $columnBusiness->parseValue($value, $column);
374+
return $columnBusiness->parseDisplayValue($value, $column);
375375
} catch (NotFoundExceptionInterface|ContainerExceptionInterface $e) {
376376
$this->logger->debug('Column type business class not found', ['exception' => $e]);
377377
}

0 commit comments

Comments
 (0)