[TASK] Refactor SkillGroupingViewHelper

This commit is contained in:
Markus Klein 2023-07-21 11:06:53 +02:00
parent b6e99f84ed
commit a4f0ba6d23

View file

@ -62,11 +62,7 @@ class SkillGroupingViewHelper extends AbstractViewHelper
$skillSet = $arguments['skillSet']; $skillSet = $arguments['skillSet'];
$as = $arguments['as']; $as = $arguments['as'];
if ($skillSet) { $groups = self::group($skillSet ? $skillSet->getSkills() : $skills);
$groups = self::group($skillSet->getSkills());
} else {
$groups = self::group($skills);
}
$templateVariableContainer = $renderingContext->getVariableProvider(); $templateVariableContainer = $renderingContext->getVariableProvider();
$output = ''; $output = '';
@ -84,33 +80,20 @@ class SkillGroupingViewHelper extends AbstractViewHelper
*/ */
protected static function group(array $skills): array protected static function group(array $skills): array
{ {
$domainTagsWithId = [];
foreach ($skills as $skill) {
if (!isset($skill->toArray()['domainTag'])) {
$domainTagsWithId[] = [
'uid' => 0,
'title' => 'none'
];
continue;
}
$domainTagsWithId[] = $skill->toArray()['domainTag'];
}
//remove items with duplicate title
$domainTagsWithId = array_map('unserialize', array_unique(array_map('serialize', $domainTagsWithId)));
$result = []; $result = [];
foreach ($domainTagsWithId as $domainTag) { foreach ($skills as $skill) {
$result[] = [ $skillAsArray = $skill->toArray();
'title' => $domainTag['title'], $domainTag = !empty($skillAsArray['domainTag']) ? $skillAsArray['domainTag'] : ['uid' => 0, 'title' => ''];
if (!isset($result[$domainTag['uid']])) {
$result[$domainTag['uid']] = [
'uid' => $domainTag['uid'], 'uid' => $domainTag['uid'],
'skills' => array_filter($skills, function ($skill) use ($domainTag) { 'title' => $domainTag['title'],
if (!isset($skill->toArray()['domainTag'])) { 'skills' => [],
return $domainTag['title'] === 'none';
}
return $skill->toArray()['domainTag']['title'] === $domainTag['title'];
})
]; ];
} }
$result[$domainTag['uid']]['skills'][] = $skill;
}
return $result; return $result;
} }
} }