Part-DB.Part-DB-server/src/Services/LabelSystem/LabelGenerator.php

121 lines
4.2 KiB
PHP
Raw Normal View History

2020-04-14 11:07:07 +02:00
<?php
2022-11-29 21:21:26 +01:00
/*
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
*
* Copyright (C) 2019 - 2022 Jan Böhmer (https://github.com/jbtronics)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
2020-05-10 21:39:31 +02:00
declare(strict_types=1);
2020-04-14 11:07:07 +02:00
/**
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
*
2022-11-29 22:28:53 +01:00
* Copyright (C) 2019 - 2022 Jan Böhmer (https://github.com/jbtronics)
2020-04-14 11:07:07 +02:00
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
namespace App\Services\LabelSystem;
use App\Entity\LabelSystem\LabelOptions;
use App\Entity\Parts\Part;
2020-04-16 19:56:30 +02:00
use App\Entity\Parts\PartLot;
use App\Entity\Parts\Storelocation;
2020-04-14 11:07:07 +02:00
use Dompdf\Dompdf;
2022-08-14 19:32:53 +02:00
use InvalidArgumentException;
2020-04-14 11:07:07 +02:00
2020-05-09 18:17:23 +02:00
final class LabelGenerator
2020-04-14 11:07:07 +02:00
{
2020-04-16 19:56:30 +02:00
public const CLASS_SUPPORT_MAPPING = [
2020-04-14 11:07:07 +02:00
'part' => Part::class,
2020-04-16 19:56:30 +02:00
'part_lot' => PartLot::class,
'storelocation' => Storelocation::class,
2020-04-14 11:07:07 +02:00
];
public const MM_TO_POINTS_FACTOR = 2.83465;
2022-09-18 22:59:31 +02:00
private LabelHTMLGenerator $labelHTMLGenerator;
2020-04-14 11:07:07 +02:00
public function __construct(LabelHTMLGenerator $labelHTMLGenerator)
{
$this->labelHTMLGenerator = $labelHTMLGenerator;
}
/**
2020-05-10 21:39:31 +02:00
* @param object|object[] $elements An element or an array of elements for which labels should be generated
*/
public function generateLabel(LabelOptions $options, $elements): string
2020-04-14 11:07:07 +02:00
{
2020-08-21 21:36:22 +02:00
if (!is_array($elements) && !is_object($elements)) {
2022-08-14 19:32:53 +02:00
throw new InvalidArgumentException('$element must be an object or an array of objects!');
2020-04-14 11:07:07 +02:00
}
2020-08-21 21:36:22 +02:00
if (!is_array($elements)) {
$elements = [$elements];
}
2020-04-14 11:07:07 +02:00
foreach ($elements as $element) {
2020-08-21 21:36:22 +02:00
if (!$this->supports($options, $element)) {
2022-08-14 19:32:53 +02:00
throw new InvalidArgumentException('The given options are not compatible with the given element!');
}
}
$dompdf = new Dompdf();
$dompdf->setPaper($this->mmToPointsArray($options->getWidth(), $options->getHeight()));
$dompdf->loadHtml($this->labelHTMLGenerator->getLabelHTML($options, $elements));
2020-04-14 11:07:07 +02:00
$dompdf->render();
2020-05-10 21:39:31 +02:00
2020-04-14 11:07:07 +02:00
return $dompdf->output();
}
/**
* Check if the given LabelOptions can be used with $element.
*/
2020-08-21 22:43:37 +02:00
public function supports(LabelOptions $options, object $element): bool
2020-04-14 11:07:07 +02:00
{
$supported_type = $options->getSupportedElement();
2020-08-21 21:36:22 +02:00
if (!isset(static::CLASS_SUPPORT_MAPPING[$supported_type])) {
2022-08-14 19:32:53 +02:00
throw new InvalidArgumentException('Supported type name of the Label options not known!');
2020-04-14 11:07:07 +02:00
}
return is_a($element, static::CLASS_SUPPORT_MAPPING[$supported_type]);
}
/**
2020-05-10 21:39:31 +02:00
* Converts width and height given in mm to an size array, that can be used by DOMPDF for page size.
*
* @param float $width The width of the paper
* @param float $height The height of the paper
*
* @return float[]
*/
2020-04-14 11:07:07 +02:00
public function mmToPointsArray(float $width, float $height): array
{
return [0.0, 0.0, $width * self::MM_TO_POINTS_FACTOR, $height * self::MM_TO_POINTS_FACTOR];
2020-04-14 11:07:07 +02:00
}
2020-05-10 21:39:31 +02:00
}