configAltNamesHelper = $configAltNamesHelper; $this->engine = $engine; $this->translator = $translator; } /** * @param Person $person */ public function renderBox($person, array $options): string { $params = [ 'addAltNames' => $options['addAltNames'] ?? $this->configAltNamesHelper->hasAltNames(), 'addLink' => $options['addLink'] ?? false, 'addEntity' => $options['addEntity'] ?? false, 'addId' => $options['addId'] ?? false, 'addInfo' => $options['addInfo'] ?? false, 'addAge' => $options['addAge'] ?? false, 'addCenter' => $options['addCenter'] ?? false, 'address_multiline' => $options['address_multiline'] ?? false, 'hLevel' => $options['hLevel'] ?? 3, 'customButtons' => $options['customButtons'] ?? [], 'customArea' => $options['customArea'] ?? [], 'addDeath' => $options['addDeath'] ?? true, 'addAgeBadge' => $options['addAgeBadge'] ?? false, ]; return $this->getDefaultOpeningBox('person') . $this->engine->render('@ChillPerson/Entity/person.html.twig', [ 'person' => $person, 'render' => $options['render'] ?? 'raw', 'options' => $params, ]) . $this->getDefaultClosingBox(); } /** * @param Person $person */ public function renderString($person, array $options): string { $options = array_merge(['addAge' => true], $options); if (null !== $person->getAge() && $person->getDeathDate() === null && $options['addAge']) { return $person->getFirstName() . ' ' . $person->getLastName() . $this->addAltNames($person, false) . ' (' . $this->translator->trans('years_old', ['age' => $person->getAge()]) . ')'; } return $person->getFirstName() . ' ' . $person->getLastName() . $this->addAltNames($person, false); } public function supports($entity, array $options): bool { return $entity instanceof Person; } protected function addAltNames(Person $person, bool $addSpan): string { $str = ''; if ($this->configAltNamesHelper->hasAltNames()) { $altNames = $this->configAltNamesHelper->getChoices(); $isFirst = true; foreach ($person->getAltNames()->getIterator() as $altName) { /** @var \Chill\PersonBundle\Entity\PersonAltName $altName */ if (array_key_exists($altName->getKey(), $altNames)) { if ($isFirst) { $str .= ' ('; $isFirst = false; } else { $str .= ' '; } if ($addSpan) { $str .= ''; } $str .= $altName->getLabel(); if ($addSpan) { $str .= ''; } } } if (!$isFirst) { $str .= ')'; } } return $str; } }