Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 38 |
|
0.00% |
0 / 6 |
CRAP | |
0.00% |
0 / 1 |
| TaskAttributeValue | |
0.00% |
0 / 38 |
|
0.00% |
0 / 6 |
420 | |
0.00% |
0 / 1 |
| setL11n | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
20 | |||
| getL11n | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
6 | |||
| setValue | |
0.00% |
0 / 10 |
|
0.00% |
0 / 1 |
56 | |||
| getValue | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
30 | |||
| toArray | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
2 | |||
| jsonSerialize | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * Jingga |
| 4 | * |
| 5 | * PHP Version 8.1 |
| 6 | * |
| 7 | * @package Modules\Tasks\Models |
| 8 | * @copyright Dennis Eichhorn |
| 9 | * @license OMS License 2.0 |
| 10 | * @version 1.0.0 |
| 11 | * @link https://jingga.app |
| 12 | */ |
| 13 | declare(strict_types=1); |
| 14 | |
| 15 | namespace Modules\Tasks\Models; |
| 16 | |
| 17 | use phpOMS\Localization\BaseStringL11n; |
| 18 | use phpOMS\Localization\ISO639x1Enum; |
| 19 | |
| 20 | /** |
| 21 | * Task attribute value class. |
| 22 | * |
| 23 | * The relation with the type/task is defined in the TaskAttribute class. |
| 24 | * |
| 25 | * @package Modules\Tasks\Models |
| 26 | * @license OMS License 2.0 |
| 27 | * @link https://jingga.app |
| 28 | * @since 1.0.0 |
| 29 | */ |
| 30 | class TaskAttributeValue implements \JsonSerializable |
| 31 | { |
| 32 | /** |
| 33 | * Id |
| 34 | * |
| 35 | * @var int |
| 36 | * @since 1.0.0 |
| 37 | */ |
| 38 | public int $id = 0; |
| 39 | |
| 40 | /** |
| 41 | * Depending attribute type |
| 42 | * |
| 43 | * @var null|int |
| 44 | * @since 1.0.0 |
| 45 | */ |
| 46 | public ?int $dependingAttributeType = null; |
| 47 | |
| 48 | /** |
| 49 | * Depending attribute value |
| 50 | * |
| 51 | * @var null|int |
| 52 | * @since 1.0.0 |
| 53 | */ |
| 54 | public ?int $dependingAttributeValue = null; |
| 55 | |
| 56 | /** |
| 57 | * Int value |
| 58 | * |
| 59 | * @var null|int |
| 60 | * @since 1.0.0 |
| 61 | */ |
| 62 | public ?int $valueInt = null; |
| 63 | |
| 64 | /** |
| 65 | * String value |
| 66 | * |
| 67 | * @var null|string |
| 68 | * @since 1.0.0 |
| 69 | */ |
| 70 | public ?string $valueStr = null; |
| 71 | |
| 72 | /** |
| 73 | * Decimal value |
| 74 | * |
| 75 | * @var null|float |
| 76 | * @since 1.0.0 |
| 77 | */ |
| 78 | public ?float $valueDec = null; |
| 79 | |
| 80 | /** |
| 81 | * DateTime value |
| 82 | * |
| 83 | * @var null|\DateTimeInterface |
| 84 | * @since 1.0.0 |
| 85 | */ |
| 86 | public ?\DateTimeInterface $valueDat = null; |
| 87 | |
| 88 | /** |
| 89 | * Is a default value which can be selected |
| 90 | * |
| 91 | * @var bool |
| 92 | * @since 1.0.0 |
| 93 | */ |
| 94 | public bool $isDefault = false; |
| 95 | |
| 96 | /** |
| 97 | * Unit of the value |
| 98 | * |
| 99 | * @var string |
| 100 | * @since 1.0.0 |
| 101 | */ |
| 102 | public string $unit = ''; |
| 103 | |
| 104 | /** |
| 105 | * Localization |
| 106 | * |
| 107 | * @var null|BaseStringL11n |
| 108 | */ |
| 109 | public ?BaseStringL11n $l11n = null; |
| 110 | |
| 111 | /** |
| 112 | * Set l11n |
| 113 | * |
| 114 | * @param string|BaseStringL11n $l11n Tag article l11n |
| 115 | * @param string $lang Language |
| 116 | * |
| 117 | * @return void |
| 118 | * |
| 119 | * @since 1.0.0 |
| 120 | */ |
| 121 | public function setL11n(string | BaseStringL11n $l11n, string $lang = ISO639x1Enum::_EN) : void |
| 122 | { |
| 123 | if ($l11n instanceof BaseStringL11n) { |
| 124 | $this->l11n = $l11n; |
| 125 | } elseif (isset($this->l11n) && $this->l11n instanceof BaseStringL11n) { |
| 126 | $this->l11n->content = $l11n; |
| 127 | $this->l11n->setLanguage($lang); |
| 128 | } else { |
| 129 | $this->l11n = new BaseStringL11n(); |
| 130 | $this->l11n->content = $l11n; |
| 131 | $this->l11n->ref = $this->id; |
| 132 | $this->l11n->setLanguage($lang); |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * Get localization |
| 138 | * |
| 139 | * @return null|string |
| 140 | * |
| 141 | * @since 1.0.0 |
| 142 | */ |
| 143 | public function getL11n() : ?string |
| 144 | { |
| 145 | return $this->l11n instanceof BaseStringL11n ? $this->l11n->content : $this->l11n; |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * Set value |
| 150 | * |
| 151 | * @param null|int|string|float $value Value |
| 152 | * |
| 153 | * @return void |
| 154 | * |
| 155 | * @since 1.0.0 |
| 156 | */ |
| 157 | public function setValue(mixed $value, int $datatype) : void |
| 158 | { |
| 159 | if ($datatype === AttributeValueType::_STRING) { |
| 160 | $this->valueStr = (string) $value; |
| 161 | } elseif ($datatype === AttributeValueType::_INT |
| 162 | || $datatype === AttributeValueType::_FLOAT_INT |
| 163 | || $datatype === AttributeValueType::_BOOL |
| 164 | ) { |
| 165 | $this->valueInt = (int) $value; |
| 166 | } elseif ($datatype === AttributeValueType::_FLOAT) { |
| 167 | $this->valueDec = (float) $value; |
| 168 | } elseif ($datatype === AttributeValueType::_DATETIME) { |
| 169 | $this->valueDat = new \DateTime((string) $value); |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | /** |
| 174 | * Get value |
| 175 | * |
| 176 | * @return null|int|string|float|\DateTimeInterface |
| 177 | * |
| 178 | * @since 1.0.0 |
| 179 | */ |
| 180 | public function getValue() : mixed |
| 181 | { |
| 182 | if (!empty($this->valueStr)) { |
| 183 | return $this->valueStr; |
| 184 | } elseif (!empty($this->valueInt)) { |
| 185 | return $this->valueInt; |
| 186 | } elseif (!empty($this->valueDec)) { |
| 187 | return $this->valueDec; |
| 188 | } elseif ($this->valueDat instanceof \DateTimeInterface) { |
| 189 | return $this->valueDat; |
| 190 | } |
| 191 | |
| 192 | return null; |
| 193 | } |
| 194 | |
| 195 | /** |
| 196 | * {@inheritdoc} |
| 197 | */ |
| 198 | public function toArray() : array |
| 199 | { |
| 200 | return [ |
| 201 | 'id' => $this->id, |
| 202 | 'valueInt' => $this->valueInt, |
| 203 | 'valueStr' => $this->valueStr, |
| 204 | 'valueDec' => $this->valueDec, |
| 205 | 'valueDat' => $this->valueDat, |
| 206 | 'isDefault' => $this->isDefault, |
| 207 | ]; |
| 208 | } |
| 209 | |
| 210 | /** |
| 211 | * {@inheritdoc} |
| 212 | */ |
| 213 | public function jsonSerialize() : mixed |
| 214 | { |
| 215 | return $this->toArray(); |
| 216 | } |
| 217 | } |