Passed
Push — developer ( 1ee7e4...bad962 )
by Radosław
19:40
created

Vtiger_Double_InventoryField::getEditValue()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 4
ccs 0
cts 1
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
crap 2
1
<?php
2
3
/**
4
 * Inventory Double Field Class.
5
 *
6
 * @package   InventoryField
7
 *
8
 * @copyright YetiForce S.A.
9
 * @license   YetiForce Public License 5.0 (licenses/LicenseEN.txt or yetiforce.com)
10
 * @author    Mariusz Krzaczkowski <[email protected]>
11
 * @author    Radosław Skrzypczak <[email protected]>
12
 */
13
use App\Fields\Double;
14
15
class Vtiger_Double_InventoryField extends Vtiger_Basic_InventoryField
16
{
17
	protected $type = 'Double';
18
	protected $defaultLabel = 'LBL_DOUBLE';
19
	protected $columnName = 'double';
20
	protected $dbType = [\yii\db\Schema::TYPE_DECIMAL, '28,8'];
21
	protected $onlyOne = false;
22
	protected $maximumLength = '99999999999999999999';
23
	protected $purifyType = \App\Purifier::NUMBER;
24
25
	/** {@inheritdoc} */
26
	public function getDisplayValue($value, array $rowData = [], bool $rawText = false)
27
	{
28
		return \App\Fields\Double::formatToDisplay($value, Double::FORMAT_TRUNCATE_TRAILING_ZEROS | Double::FORMAT_DIGITS_UP_TO_PRECISION);
29
	}
30
31
	/** {@inheritdoc} */
32
	public function getEditValue(array $itemData, string $column = '')
33
	{
34
		$value = parent::getEditValue($itemData, $column);
35
		return \App\Fields\Double::formatToDisplay($value, Double::FORMAT_TRUNCATE_TRAILING_ZEROS | Double::FORMAT_DIGITS_UP_TO_PRECISION);
36
	}
37
38
	/** {@inheritdoc} */
39
	public function getDBValue($value, ?string $name = '')
40
	{
41
		if (!isset($this->dbValue["{$value}"])) {
42
			$this->dbValue["{$value}"] = App\Fields\Double::formatToDb($value);
0 ignored issues
show
Bug Best Practice introduced by
The property dbValue does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
43
		}
44
		return $this->dbValue["{$value}"];
45
	}
46
47
	/** {@inheritdoc} */
48
	public function validate($value, string $columnName, bool $isUserFormat, $originalValue = null)
49
	{
50
		if ($isUserFormat) {
51
			$value = $this->getDBValue($value, $columnName);
52
		}
53
		$moduleName = $this->getModuleName();
54
		if (!is_numeric($value)) {
55
			throw new \App\Exceptions\Security("ERR_ILLEGAL_FIELD_VALUE||$columnName||$moduleName||$value", 406);
56
		}
57
		if ($this->maximumLength < $value || -$this->maximumLength > $value) {
58
			throw new \App\Exceptions\Security("ERR_VALUE_IS_TOO_LONG||$columnName||$moduleName||$value", 406);
59
		}
60
	}
61
62
	/** {@inheritdoc} */
63
	public function compare($value, $prevValue, string $column): bool
64
	{
65
		return \App\Validator::floatIsEqual((float) $value, (float) $prevValue, 8);
66
	}
67
}
68