Part-DB.Part-DB-server/src/Entity/Parameters/AbstractParameter.php

413 lines
11 KiB
PHP
Raw Normal View History

<?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-03-15 13:56:31 +01:00
declare(strict_types=1);
/**
* 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)
*
* 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\Parameters;
2023-06-11 14:55:06 +02:00
use App\Repository\ParameterRepository;
use Doctrine\DBAL\Types\Types;
use App\Entity\Base\AbstractDBElement;
use App\Entity\Base\AbstractNamedDBElement;
use Doctrine\ORM\Mapping as ORM;
use InvalidArgumentException;
use LogicException;
2023-03-12 01:12:35 +01:00
use Symfony\Component\Serializer\Annotation\Groups;
use Symfony\Component\Validator\Constraints as Assert;
2022-08-14 19:32:53 +02:00
use function sprintf;
2023-06-11 14:55:06 +02:00
#[ORM\Entity(repositoryClass: ParameterRepository::class)]
#[ORM\InheritanceType('SINGLE_TABLE')]
#[ORM\DiscriminatorColumn(name: 'type', type: 'smallint')]
#[ORM\DiscriminatorMap([0 => 'CategoryParameter', 1 => 'CurrencyParameter', 2 => 'ProjectParameter', 3 => 'FootprintParameter', 4 => 'GroupParameter', 5 => 'ManufacturerParameter', 6 => 'MeasurementUnitParameter', 7 => 'PartParameter', 8 => 'StorelocationParameter', 9 => 'SupplierParameter', 10 => 'AttachmentTypeParameter'])]
#[ORM\Table('parameters')]
#[ORM\Index(name: 'parameter_name_idx', columns: ['name'])]
#[ORM\Index(name: 'parameter_group_idx', columns: ['param_group'])]
#[ORM\Index(name: 'parameter_type_element_idx', columns: ['type', 'element_id'])]
abstract class AbstractParameter extends AbstractNamedDBElement
{
/**
* @var string The class of the element that can be passed to this attachment. Must be overridden in subclasses.
*/
protected const ALLOWED_ELEMENT_CLASS = '';
/**
* @var string The mathematical symbol for this specification. Can be rendered pretty later. Should be short
*/
2023-05-28 01:21:05 +02:00
#[Assert\Length(max: 20)]
#[Groups(['full'])]
2023-06-11 14:55:06 +02:00
#[ORM\Column(type: Types::STRING)]
2022-09-18 22:59:31 +02:00
protected string $symbol = '';
/**
2020-08-21 21:36:22 +02:00
* @var float|null the guaranteed minimum value of this property
*/
2023-05-28 01:21:05 +02:00
#[Assert\Type(['float', null])]
#[Assert\LessThanOrEqual(propertyPath: 'value_typical', message: 'parameters.validator.min_lesser_typical')]
#[Assert\LessThan(propertyPath: 'value_max', message: 'parameters.validator.min_lesser_max')]
#[Groups(['full'])]
2023-06-11 14:55:06 +02:00
#[ORM\Column(type: Types::FLOAT, nullable: true)]
2022-09-18 22:59:31 +02:00
protected ?float $value_min = null;
/**
2020-08-21 21:36:22 +02:00
* @var float|null the typical value of this property
*/
2023-05-28 01:21:05 +02:00
#[Assert\Type([null, 'float'])]
#[Groups(['full'])]
2023-06-11 14:55:06 +02:00
#[ORM\Column(type: Types::FLOAT, nullable: true)]
2022-09-18 22:59:31 +02:00
protected ?float $value_typical = null;
/**
2020-08-21 21:36:22 +02:00
* @var float|null the maximum value of this property
*/
2023-05-28 01:21:05 +02:00
#[Assert\Type(['float', null])]
#[Assert\GreaterThanOrEqual(propertyPath: 'value_typical', message: 'parameters.validator.max_greater_typical')]
#[Groups(['full'])]
2023-06-11 14:55:06 +02:00
#[ORM\Column(type: Types::FLOAT, nullable: true)]
2022-09-18 22:59:31 +02:00
protected ?float $value_max = null;
/**
* @var string The unit in which the value values are given (e.g. V)
*/
2023-05-28 01:21:05 +02:00
#[Groups(['full'])]
2023-06-11 14:55:06 +02:00
#[ORM\Column(type: Types::STRING)]
2022-09-18 22:59:31 +02:00
protected string $unit = '';
/**
2020-08-21 21:36:22 +02:00
* @var string a text value for the given property
*/
2023-05-28 01:21:05 +02:00
#[Groups(['full'])]
2023-06-11 14:55:06 +02:00
#[ORM\Column(type: Types::STRING)]
2022-09-18 22:59:31 +02:00
protected string $value_text = '';
/**
2020-08-21 21:36:22 +02:00
* @var string the group this parameter belongs to
*/
2023-05-28 01:21:05 +02:00
#[Groups(['full'])]
2023-06-11 14:55:06 +02:00
#[ORM\Column(type: Types::STRING, name: 'param_group')]
2022-09-18 22:59:31 +02:00
protected string $group = '';
/**
2023-04-15 23:14:53 +02:00
* Mapping is done in subclasses.
2020-03-15 13:56:31 +01:00
*
2020-08-21 21:36:22 +02:00
* @var AbstractDBElement|null the element to which this parameter belongs to
*/
2023-04-15 22:25:03 +02:00
protected ?AbstractDBElement $element = null;
public function __construct()
{
if ('' === static::ALLOWED_ELEMENT_CLASS) {
throw new LogicException('An *Attachment class must override the ALLOWED_ELEMENT_CLASS const!');
}
}
public function updateTimestamps(): void
{
parent::updateTimestamps();
if ($this->element instanceof AbstractNamedDBElement) {
$this->element->updateTimestamps();
}
}
/**
* Returns the element this parameter belongs to.
*/
public function getElement(): ?AbstractDBElement
{
return $this->element;
}
/**
* Return a formatted string version of the values of the string.
2020-03-15 13:56:31 +01:00
* Based on the set values it can return something like this: 34 V (12 V ... 50 V) [Text].
*/
public function getFormattedValue(): string
{
//If we just only have text value, return early
2020-03-15 13:56:31 +01:00
if (null === $this->value_typical && null === $this->value_min && null === $this->value_max) {
return $this->value_text;
}
$str = '';
$bracket_opened = false;
if ($this->value_typical) {
$str .= $this->getValueTypicalWithUnit();
if ($this->value_min || $this->value_max) {
$bracket_opened = true;
$str .= ' (';
}
}
if ($this->value_max && $this->value_min) {
2020-03-15 13:56:31 +01:00
$str .= $this->getValueMinWithUnit().' ... '.$this->getValueMaxWithUnit();
} elseif ($this->value_max) {
2020-03-15 13:56:31 +01:00
$str .= 'max. '.$this->getValueMaxWithUnit();
} elseif ($this->value_min) {
2020-03-15 13:56:31 +01:00
$str .= 'min. '.$this->getValueMinWithUnit();
}
//Add closing bracket
if ($bracket_opened) {
$str .= ')';
}
if ($this->value_text !== '' && $this->value_text !== '0') {
2020-03-15 13:56:31 +01:00
$str .= ' ['.$this->value_text.']';
}
return $str;
}
/**
* Sets the element to which this parameter belongs to.
2020-03-15 13:56:31 +01:00
*
* @return $this
*/
public function setElement(AbstractDBElement $element): self
{
2020-08-21 21:36:22 +02:00
if (!is_a($element, static::ALLOWED_ELEMENT_CLASS)) {
throw new InvalidArgumentException(sprintf('The element associated with a %s must be a %s!', static::class, static::ALLOWED_ELEMENT_CLASS));
}
$this->element = $element;
2020-03-15 13:56:31 +01:00
return $this;
}
/**
* Sets the name of the specification. This value is required.
2020-03-15 13:56:31 +01:00
*
* @return $this
*/
public function setName(string $name): AbstractNamedDBElement
{
$this->name = $name;
2020-03-15 13:56:31 +01:00
return $this;
}
/**
2020-03-29 22:16:06 +02:00
* Returns the name of the group this parameter is associated to (e.g. Technical Parameters).
*/
public function getGroup(): string
{
return $this->group;
}
/**
* Sets the name of the group this parameter is associated to.
2020-03-29 22:16:06 +02:00
*
* @return $this
*/
public function setGroup(string $group): self
{
$this->group = $group;
2020-03-29 22:16:06 +02:00
return $this;
}
/**
2020-03-15 13:56:31 +01:00
* Returns the mathematical symbol for this specification (e.g. "V_CB").
*/
public function getSymbol(): string
{
return $this->symbol;
}
/**
2020-03-15 13:56:31 +01:00
* Sets the mathematical symbol for this specification (e.g. "V_CB").
*
* @return $this
*/
public function setSymbol(string $symbol): self
{
$this->symbol = $symbol;
2020-03-15 13:56:31 +01:00
return $this;
}
/**
* Returns The guaranteed minimum value of this property.
*/
public function getValueMin(): ?float
{
return $this->value_min;
}
/**
* Sets the minimum value of this property.
2020-03-15 13:56:31 +01:00
*
* @return $this
*/
public function setValueMin(?float $value_min): self
{
$this->value_min = $value_min;
2020-03-15 13:56:31 +01:00
return $this;
}
/**
* Returns the typical value of this property.
*/
public function getValueTypical(): ?float
{
return $this->value_typical;
}
/**
2020-03-15 13:56:31 +01:00
* Return a formatted version with the minimum value with the unit of this parameter.
*/
public function getValueTypicalWithUnit(): string
{
return $this->formatWithUnit($this->value_typical);
}
/**
2020-03-15 13:56:31 +01:00
* Return a formatted version with the maximum value with the unit of this parameter.
*/
public function getValueMaxWithUnit(): string
{
return $this->formatWithUnit($this->value_max);
}
/**
2020-03-15 13:56:31 +01:00
* Return a formatted version with the typical value with the unit of this parameter.
*/
public function getValueMinWithUnit(): string
{
return $this->formatWithUnit($this->value_min);
}
/**
2020-03-15 13:56:31 +01:00
* Sets the typical value of this property.
*
*
* @return $this
*/
public function setValueTypical(?float $value_typical): self
{
$this->value_typical = $value_typical;
2020-03-15 13:56:31 +01:00
return $this;
}
/**
2020-03-15 13:56:31 +01:00
* Returns the guaranteed maximum value.
*/
public function getValueMax(): ?float
{
return $this->value_max;
}
/**
2020-03-15 13:56:31 +01:00
* Sets the guaranteed maximum value.
*
* @return $this
*/
public function setValueMax(?float $value_max): self
{
$this->value_max = $value_max;
2020-03-15 13:56:31 +01:00
return $this;
}
/**
2020-03-15 13:56:31 +01:00
* Returns the unit used by the value (e.g. "V").
*/
public function getUnit(): string
{
return $this->unit;
}
/**
* Sets the unit used by the value.
2020-03-15 13:56:31 +01:00
*
* @return $this
*/
public function setUnit(string $unit): self
{
$this->unit = $unit;
2020-03-15 13:56:31 +01:00
return $this;
}
/**
* Returns the text value.
*/
public function getValueText(): string
{
return $this->value_text;
}
/**
* Sets the text value.
2020-03-15 13:56:31 +01:00
*
* @return $this
*/
public function setValueText(string $value_text): self
{
$this->value_text = $value_text;
2020-03-15 13:56:31 +01:00
return $this;
}
2020-03-15 13:56:31 +01:00
/**
* Return a string representation and (if possible) with its unit.
*/
protected function formatWithUnit(float $value, string $format = '%g'): string
{
2022-08-14 19:32:53 +02:00
$str = sprintf($format, $value);
if ($this->unit !== '') {
2020-03-15 13:56:31 +01:00
return $str.' '.$this->unit;
}
return $str;
}
/**
* Returns the class of the element that is allowed to be associated with this attachment.
* @return string
*/
public function getElementClass(): string
{
return static::ALLOWED_ELEMENT_CLASS;
}
2020-03-15 13:56:31 +01:00
}