. */ namespace App\Services\LabelSystem; use App\Entity\Contracts\NamedElementInterface; use App\Entity\LabelSystem\LabelOptions; use App\Services\ElementTypeNameGenerator; use Twig\Environment; class LabelHTMLGenerator { protected $twig; protected $elementTypeNameGenerator; protected $replacer; public function __construct(ElementTypeNameGenerator $elementTypeNameGenerator, LabelTextReplacer $replacer, Environment $twig) { $this->twig = $twig; $this->elementTypeNameGenerator = $elementTypeNameGenerator; $this->replacer = $replacer; } public function getLabelHTML(LabelOptions $options, array $elements): string { if (empty($elements)) { throw new \InvalidArgumentException('$elements must not be empty'); } $twig_elements = []; foreach ($elements as $element) { $twig_elements[] = [ 'element' => $element, 'lines' => $this->replacer->replace($options->getLines(), $element) ]; } return $this->twig->render('LabelSystem/labels/base_label.html.twig', [ 'meta_title' => $this->getPDFTitle($options, $elements[0]), 'elements' => $twig_elements, ]); } protected function getPDFTitle(LabelOptions $options, object $element) { if ($element instanceof NamedElementInterface) { return $this->elementTypeNameGenerator->getTypeNameCombination($element, false); } return 'Part-DB label'; } }