Skip to content

Commit c9bcdbf

Browse files
committed
Refactors relation handling with helper methods
Introduces factory methods to streamline creation of relation objects from raw values and document arrays, improving readability and maintainability. Reduces code duplication and clarifies relation mapping logic.
1 parent dd66dfe commit c9bcdbf

File tree

2 files changed

+25
-9
lines changed

2 files changed

+25
-9
lines changed

src/AbraFlexi/RO.php

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1127,20 +1127,21 @@ public function fixRecordTypes(array $record, $evidence = null)
11271127
$record[$column][$a] = $b;
11281128
}
11291129
} else {
1130-
$record[$column] = new Relation(
1131-
$value,
1132-
$column,
1133-
str_replace('ext:', '', $value),
1134-
$value,
1135-
);
1130+
$relation = Relation::fromExtId($value, $column);
1131+
$record[$column][$relation->target] = $relation;
11361132
}
11371133
} else { // ExtIDs
11381134
if (\count($record[$column]) === 1) {
1139-
$record[$column] = new Relation(\array_key_exists('kod', $value) ? $value['kod'] : $value['id'], $value['typDoklK'], $value['id'], $value['typDoklK@showAs']);
1135+
if (\is_array($value)) {
1136+
$record[$column] = Relation::fromTypDokl($value);
1137+
} else {
1138+
$relation = Relation::fromExtId($value, $column);
1139+
$record[$column][$relation->target] = $relation;
1140+
}
11401141
} else {
11411142
foreach ($record[$column] as $relPos => $rawRelation) {
1142-
[,$ext,$extId] = explode(':', $rawRelation);
1143-
$record[$column][$ext] = new Relation($rawRelation, $ext, $extId, $column.' '.$ext.':'.$extId);
1143+
$relation = Relation::fromExtId($rawRelation, $column);
1144+
$record[$column][$relation->target] = $relation;
11441145
unset($record[$column][$relPos]);
11451146
}
11461147
}

src/AbraFlexi/Relation.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,4 +102,19 @@ public function offsetUnset(mixed $offset): void
102102
{
103103
unset($this->data[$offset]);
104104
}
105+
106+
/**
107+
* Create Relation object from ExtID.
108+
*/
109+
public static function fromExtId(string $extIdRaw, string $column): self
110+
{
111+
[,$ext,$extId] = explode(':', $extIdRaw);
112+
113+
return new self($extId, $ext, $extId, trim($column.' '.$ext).':'.$extId);
114+
}
115+
116+
public static function fromTypDokl(array $typDokl): self
117+
{
118+
return new self(\array_key_exists('kod', $typDokl) ? $typDokl['kod'] : $typDokl['id'], $typDokl['typDoklK'], $typDokl['id'], $typDokl['typDoklK@showAs']);
119+
}
105120
}

0 commit comments

Comments
 (0)