2020-04-14 17:21:30 +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 17:21:30 +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 17:21:30 +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\PlaceholderProviders;
|
|
|
|
|
|
|
|
use App\Entity\Parts\Part;
|
|
|
|
use App\Services\SIFormatter;
|
2022-08-14 19:32:53 +02:00
|
|
|
use Parsedown;
|
2020-04-14 17:21:30 +02:00
|
|
|
use Symfony\Contracts\Translation\TranslatorInterface;
|
|
|
|
|
2020-05-09 18:17:23 +02:00
|
|
|
final class PartProvider implements PlaceholderProviderInterface
|
2020-04-14 17:21:30 +02:00
|
|
|
{
|
2022-09-18 22:59:31 +02:00
|
|
|
private SIFormatter $siFormatter;
|
|
|
|
private TranslatorInterface $translator;
|
2020-04-14 17:21:30 +02:00
|
|
|
|
|
|
|
public function __construct(SIFormatter $SIFormatter, TranslatorInterface $translator)
|
|
|
|
{
|
|
|
|
$this->siFormatter = $SIFormatter;
|
|
|
|
$this->translator = $translator;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function replace(string $placeholder, object $part, array $options = []): ?string
|
|
|
|
{
|
2020-08-21 21:36:22 +02:00
|
|
|
if (!$part instanceof Part) {
|
2020-04-14 17:21:30 +02:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2020-05-10 21:39:31 +02:00
|
|
|
if ('[[CATEGORY]]' === $placeholder) {
|
2020-04-14 17:21:30 +02:00
|
|
|
return $part->getCategory() ? $part->getCategory()->getName() : '';
|
|
|
|
}
|
|
|
|
|
2020-05-10 21:39:31 +02:00
|
|
|
if ('[[CATEGORY_FULL]]' === $placeholder) {
|
2020-04-14 17:21:30 +02:00
|
|
|
return $part->getCategory() ? $part->getCategory()->getFullPath() : '';
|
|
|
|
}
|
|
|
|
|
2020-05-10 21:39:31 +02:00
|
|
|
if ('[[MANUFACTURER]]' === $placeholder) {
|
2020-04-14 17:21:30 +02:00
|
|
|
return $part->getManufacturer() ? $part->getManufacturer()->getName() : '';
|
|
|
|
}
|
|
|
|
|
2020-05-10 21:39:31 +02:00
|
|
|
if ('[[MANUFACTURER_FULL]]' === $placeholder) {
|
2020-04-14 17:21:30 +02:00
|
|
|
return $part->getManufacturer() ? $part->getManufacturer()->getFullPath() : '';
|
|
|
|
}
|
|
|
|
|
2020-05-10 21:39:31 +02:00
|
|
|
if ('[[FOOTPRINT]]' === $placeholder) {
|
2020-04-14 17:21:30 +02:00
|
|
|
return $part->getFootprint() ? $part->getFootprint()->getName() : '';
|
|
|
|
}
|
|
|
|
|
2020-05-10 21:39:31 +02:00
|
|
|
if ('[[FOOTPRINT_FULL]]' === $placeholder) {
|
2020-04-14 17:21:30 +02:00
|
|
|
return $part->getFootprint() ? $part->getFootprint()->getFullPath() : '';
|
|
|
|
}
|
|
|
|
|
2020-05-10 21:39:31 +02:00
|
|
|
if ('[[MASS]]' === $placeholder) {
|
2020-04-14 17:21:30 +02:00
|
|
|
return $part->getMass() ? $this->siFormatter->format($part->getMass(), 'g', 1) : '';
|
|
|
|
}
|
|
|
|
|
2020-05-10 21:39:31 +02:00
|
|
|
if ('[[MPN]]' === $placeholder) {
|
2020-04-14 17:21:30 +02:00
|
|
|
return $part->getManufacturerProductNumber();
|
|
|
|
}
|
|
|
|
|
2020-05-10 21:39:31 +02:00
|
|
|
if ('[[TAGS]]' === $placeholder) {
|
2020-04-14 17:21:30 +02:00
|
|
|
return $part->getTags();
|
|
|
|
}
|
|
|
|
|
2020-05-10 21:39:31 +02:00
|
|
|
if ('[[M_STATUS]]' === $placeholder) {
|
|
|
|
if ('' === $part->getManufacturingStatus()) {
|
2020-04-14 17:21:30 +02:00
|
|
|
return '';
|
|
|
|
}
|
2020-05-10 21:39:31 +02:00
|
|
|
|
|
|
|
return $this->translator->trans('m_status.'.$part->getManufacturingStatus());
|
2020-04-14 17:21:30 +02:00
|
|
|
}
|
|
|
|
|
2022-08-14 19:32:53 +02:00
|
|
|
$parsedown = new Parsedown();
|
2020-04-14 17:21:30 +02:00
|
|
|
|
2020-05-10 21:39:31 +02:00
|
|
|
if ('[[DESCRIPTION]]' === $placeholder) {
|
2020-04-14 17:21:30 +02:00
|
|
|
return $parsedown->line($part->getDescription());
|
|
|
|
}
|
|
|
|
|
2020-05-10 21:39:31 +02:00
|
|
|
if ('[[DESCRIPTION_T]]' === $placeholder) {
|
2020-04-14 17:21:30 +02:00
|
|
|
return strip_tags($parsedown->line($part->getDescription()));
|
|
|
|
}
|
|
|
|
|
2020-05-10 21:39:31 +02:00
|
|
|
if ('[[COMMENT]]' === $placeholder) {
|
2020-04-14 17:21:30 +02:00
|
|
|
return $parsedown->line($part->getComment());
|
|
|
|
}
|
|
|
|
|
2020-05-10 21:39:31 +02:00
|
|
|
if ('[[COMMENT_T]]' === $placeholder) {
|
2020-04-14 17:21:30 +02:00
|
|
|
return strip_tags($parsedown->line($part->getComment()));
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
2020-05-10 21:39:31 +02:00
|
|
|
}
|