* {domainGroup.title}
* * {skill.name}
*
* */ class SkillGroupingViewHelper extends AbstractViewHelper { use CompileWithRenderStatic; protected $escapeOutput = false; public function initializeArguments(): void { $this->registerArgument('skills', 'array', 'An array of skills to group', false, []); $this->registerArgument('skillSet', SkillSet::class, 'The skills of this skill set will be grouped'); $this->registerArgument('as', 'string', 'The name of the iteration variable', true); } public static function renderStatic( array $arguments, Closure $renderChildrenClosure, RenderingContextInterface $renderingContext ) { /** @var Skill[] $skills */ $skills = $arguments['skills']; /** @var SkillSet|null $skillSet */ $skillSet = $arguments['skillSet']; $as = $arguments['as']; if ($skillSet) { $groups = self::group($skillSet->getSkills()); } else { $groups = self::group($skills); } $templateVariableContainer = $renderingContext->getVariableProvider(); $output = ''; foreach ($groups as $group) { $templateVariableContainer->add($as, $group); $output .= $renderChildrenClosure(); $templateVariableContainer->remove($as); } return $output; } /** * @param Skill[] $skills * @return 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 = []; foreach ($domainTagsWithId as $domainTag) { $result[] = [ 'title' => $domainTag['title'], 'uid' => $domainTag['uid'], 'skills' => array_filter($skills, function ($skill) use ($domainTag) { if (!isset($skill->toArray()['domainTag'])) { return $domainTag['title'] === 'none'; } return $skill->toArray()['domainTag']['title'] === $domainTag['title']; }) ]; } return $result; } }