Part-DB.Part-DB-server/src/Services/LabelSystem/PlaceholderProviders/PartProvider.php

112 lines
3.5 KiB
PHP
Raw Normal View History

<?php
2020-05-10 21:39:31 +02:00
declare(strict_types=1);
/**
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
*
* Copyright (C) 2019 - 2020 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/>.
*/
namespace App\Services\LabelSystem\PlaceholderProviders;
use App\Entity\Parts\Part;
use App\Services\SIFormatter;
use Symfony\Contracts\Translation\TranslatorInterface;
2020-05-09 18:17:23 +02:00
final class PartProvider implements PlaceholderProviderInterface
{
2020-05-09 18:17:23 +02:00
private $siFormatter;
private $translator;
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) {
return null;
}
2020-05-10 21:39:31 +02:00
if ('[[CATEGORY]]' === $placeholder) {
return $part->getCategory() ? $part->getCategory()->getName() : '';
}
2020-05-10 21:39:31 +02:00
if ('[[CATEGORY_FULL]]' === $placeholder) {
return $part->getCategory() ? $part->getCategory()->getFullPath() : '';
}
2020-05-10 21:39:31 +02:00
if ('[[MANUFACTURER]]' === $placeholder) {
return $part->getManufacturer() ? $part->getManufacturer()->getName() : '';
}
2020-05-10 21:39:31 +02:00
if ('[[MANUFACTURER_FULL]]' === $placeholder) {
return $part->getManufacturer() ? $part->getManufacturer()->getFullPath() : '';
}
2020-05-10 21:39:31 +02:00
if ('[[FOOTPRINT]]' === $placeholder) {
return $part->getFootprint() ? $part->getFootprint()->getName() : '';
}
2020-05-10 21:39:31 +02:00
if ('[[FOOTPRINT_FULL]]' === $placeholder) {
return $part->getFootprint() ? $part->getFootprint()->getFullPath() : '';
}
2020-05-10 21:39:31 +02:00
if ('[[MASS]]' === $placeholder) {
return $part->getMass() ? $this->siFormatter->format($part->getMass(), 'g', 1) : '';
}
2020-05-10 21:39:31 +02:00
if ('[[MPN]]' === $placeholder) {
return $part->getManufacturerProductNumber();
}
2020-05-10 21:39:31 +02:00
if ('[[TAGS]]' === $placeholder) {
return $part->getTags();
}
2020-05-10 21:39:31 +02:00
if ('[[M_STATUS]]' === $placeholder) {
if ('' === $part->getManufacturingStatus()) {
return '';
}
2020-05-10 21:39:31 +02:00
return $this->translator->trans('m_status.'.$part->getManufacturingStatus());
}
$parsedown = new \Parsedown();
2020-05-10 21:39:31 +02:00
if ('[[DESCRIPTION]]' === $placeholder) {
return $parsedown->line($part->getDescription());
}
2020-05-10 21:39:31 +02:00
if ('[[DESCRIPTION_T]]' === $placeholder) {
return strip_tags($parsedown->line($part->getDescription()));
}
2020-05-10 21:39:31 +02:00
if ('[[COMMENT]]' === $placeholder) {
return $parsedown->line($part->getComment());
}
2020-05-10 21:39:31 +02:00
if ('[[COMMENT_T]]' === $placeholder) {
return strip_tags($parsedown->line($part->getComment()));
}
return null;
}
2020-05-10 21:39:31 +02:00
}