Use enums for LabelOptions

This commit is contained in:
Jan Böhmer 2023-06-12 23:39:30 +02:00
parent 485b35fbd4
commit 71cd4057a7
23 changed files with 329 additions and 157 deletions

View file

@ -0,0 +1,64 @@
<?php
/*
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
*
* Copyright (C) 2019 - 2023 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\Entity\LabelSystem;
enum BarcodeType: string
{
case NONE = 'none';
case QR = 'qr';
case CODE39 = 'code39';
case DATAMATRIX = 'datamatrix';
case CODE93 = 'code93';
case CODE128 = 'code128';
/**
* Returns true if the barcode is none. (Useful for twig templates)
* @return bool
*/
public function isNone(): bool
{
return $this === self::NONE;
}
/**
* Returns true if the barcode is a 1D barcode (Code39, etc.).
* @return bool
*/
public function is1D(): bool
{
return match ($this) {
self::CODE39, self::CODE93, self::CODE128 => true,
default => false,
};
}
/**
* Returns true if the barcode is a 2D barcode (QR code, datamatrix).
* @return bool
*/
public function is2D(): bool
{
return match ($this) {
self::QR, self::DATAMATRIX => true,
default => false,
};
}
}

View file

@ -48,12 +48,6 @@ use Symfony\Component\Validator\Constraints as Assert;
#[ORM\Embeddable]
class LabelOptions
{
final public const BARCODE_TYPES = ['none', /*'ean8',*/ 'qr', 'code39', 'datamatrix', 'code93', 'code128'];
final public const SUPPORTED_ELEMENTS = ['part', 'part_lot', 'storelocation'];
final public const PICTURE_TYPES = ['none', 'element_picture', 'main_attachment'];
final public const LINES_MODES = ['html', 'twig'];
/**
* @var float The page size of the label in mm
*/
@ -69,25 +63,19 @@ class LabelOptions
protected float $height = 30.0;
/**
* @var string The type of the barcode that should be used in the label (e.g. 'qr')
* @var BarcodeType The type of the barcode that should be used in the label (e.g. 'qr')
*/
#[Assert\Choice(choices: LabelOptions::BARCODE_TYPES)]
#[ORM\Column(type: Types::STRING)]
protected string $barcode_type = 'none';
#[ORM\Column(type: Types::STRING, enumType: BarcodeType::class)]
protected BarcodeType $barcode_type = BarcodeType::NONE;
/**
* @var string What image should be shown along the
* @var LabelPictureType What image should be shown along the label
*/
#[Assert\Choice(choices: LabelOptions::PICTURE_TYPES)]
#[ORM\Column(type: Types::STRING)]
protected string $picture_type = 'none';
#[ORM\Column(type: Types::STRING, enumType: LabelPictureType::class)]
protected LabelPictureType $picture_type = LabelPictureType::NONE;
/**
* @var string
*/
#[Assert\Choice(choices: LabelOptions::SUPPORTED_ELEMENTS)]
#[ORM\Column(type: Types::STRING)]
protected string $supported_element = 'part';
#[ORM\Column(type: Types::STRING, enumType: LabelSupportedElement::class)]
protected LabelSupportedElement $supported_element = LabelSupportedElement::PART;
/**
* @var string any additional CSS for the label
@ -95,11 +83,10 @@ class LabelOptions
#[ORM\Column(type: Types::TEXT)]
protected string $additional_css = '';
/** @var string The mode that will be used to interpret the lines
/** @var LabelProcessMode The mode that will be used to interpret the lines
*/
#[Assert\Choice(choices: LabelOptions::LINES_MODES)]
#[ORM\Column(type: Types::STRING)]
protected string $lines_mode = 'html';
#[ORM\Column(type: Types::STRING, enumType: LabelProcessMode::class, name: 'lines_mode')]
protected LabelProcessMode $process_mode = LabelProcessMode::PLACEHOLDER;
/**
* @var string
@ -131,36 +118,36 @@ class LabelOptions
return $this;
}
public function getBarcodeType(): string
public function getBarcodeType(): BarcodeType
{
return $this->barcode_type;
}
public function setBarcodeType(string $barcode_type): self
public function setBarcodeType(BarcodeType $barcode_type): self
{
$this->barcode_type = $barcode_type;
return $this;
}
public function getPictureType(): string
public function getPictureType(): LabelPictureType
{
return $this->picture_type;
}
public function setPictureType(string $picture_type): self
public function setPictureType(LabelPictureType $picture_type): self
{
$this->picture_type = $picture_type;
return $this;
}
public function getSupportedElement(): string
public function getSupportedElement(): LabelSupportedElement
{
return $this->supported_element;
}
public function setSupportedElement(string $supported_element): self
public function setSupportedElement(LabelSupportedElement $supported_element): self
{
$this->supported_element = $supported_element;
@ -194,14 +181,14 @@ class LabelOptions
return $this;
}
public function getLinesMode(): string
public function getProcessMode(): LabelProcessMode
{
return $this->lines_mode;
return $this->process_mode;
}
public function setLinesMode(string $lines_mode): self
public function setProcessMode(LabelProcessMode $process_mode): self
{
$this->lines_mode = $lines_mode;
$this->process_mode = $process_mode;
return $this;
}

View file

@ -0,0 +1,37 @@
<?php
/*
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
*
* Copyright (C) 2019 - 2023 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\Entity\LabelSystem;
enum LabelPictureType: string
{
/**
* Show no picture on the label
*/
case NONE = 'none';
/**
* Show the preview picture of the element on the label
*/
case ELEMENT_PICTURE = 'element_picture';
/**
* Show the main attachment of the element on the label
*/
case MAIN_ATTACHMENT = 'main_attachment';
}

View file

@ -0,0 +1,29 @@
<?php
/*
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
*
* Copyright (C) 2019 - 2023 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\Entity\LabelSystem;
enum LabelProcessMode: string
{
/** Use placeholders like [[PLACEHOLDER]] which gets replaced with content */
case PLACEHOLDER = 'html';
/** Interpret the given lines as twig template */
case TWIG = 'twig';
}

View file

@ -0,0 +1,45 @@
<?php
/*
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
*
* Copyright (C) 2019 - 2023 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\Entity\LabelSystem;
use App\Entity\Parts\Part;
use App\Entity\Parts\PartLot;
use App\Entity\Parts\Storelocation;
enum LabelSupportedElement: string
{
case PART = 'part';
case PART_LOT = 'part_lot';
case STORELOCATION = 'storelocation';
/**
* Returns the entity class for the given element type
* @return string
*/
public function getEntityClass(): string
{
return match ($this) {
self::PART => Part::class,
self::PART_LOT => PartLot::class,
self::STORELOCATION => Storelocation::class,
};
}
}