Passed
Push — developer ( 5a7d78...6a35f4 )
by Radosław
17:06
created

Vtiger_Currency_InventoryField::compare()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 3
crap 2
1
<?php
2
3
/**
4
 * Inventory Currency 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
class Vtiger_Currency_InventoryField extends Vtiger_Basic_InventoryField
14
{
15
	protected $type = 'Currency';
16
	protected $defaultLabel = 'LBL_CURRENCY';
17
	protected $columnName = 'currency';
18
	protected $dbType = [\yii\db\Schema::TYPE_INTEGER, 11];
19
	protected $customColumn = [
20
		'currencyparam' => [\yii\db\Schema::TYPE_STRING, 1024],
21
	];
22
	protected $blocks = [0];
23
	protected $maximumLength = '-2147483648,2147483647';
24
	protected $customMaximumLength = [
25
		'currencyparam' => 1024
26
	];
27
	protected $purifyType = \App\Purifier::INTEGER;
28
	protected $customPurifyType = [
29
		'currencyparam' => App\Purifier::TEXT
30
	];
31
32
	/** {@inheritdoc} */
33
	public function getDisplayValue($value, array $rowData = [], bool $rawText = false)
34
	{
35
		if (empty($value)) {
36
			return '';
37
		}
38
		return \App\Fields\Currency::getById($value)['currency_name'];
39
	}
40
41
	/**
42
	 * Gets currency param.
43
	 *
44
	 * @param array  $currencies
45
	 * @param string $param
46
	 *
47
	 * @throws \App\Exceptions\AppException
48
	 *
49
	 * @return array
50
	 */
51
	public function getCurrencyParam(array $currencies, $param = '')
52
	{
53
		$params = [];
54
		if ($param) {
55
			$params = \App\Json::decode($param);
56
		}
57
		foreach ($currencies as $currency) {
58
			if (!isset($params[$currency['id']])) {
59
				$params[$currency['id']] = vtlib\Functions::getConversionRateInfo($currency['id']);
60
			}
61
		}
62
		return $params;
63
	}
64
65
	/** {@inheritdoc} */
66
	public function getDBValue($value, ?string $name = '')
67
	{
68
		if ($name === $this->getColumnName()) {
69
			$value = (int) $value;
70
		}
71
		return $value;
72
	}
73
74
	/** {@inheritdoc} */
75 2
	public function validate($value, string $columnName, bool $isUserFormat, $originalValue = null)
76
	{
77 2
		if ($columnName === $this->getColumnName()) {
78 2
			if (!is_numeric($value) || !isset(\App\Fields\Currency::getAll()[$value])) {
79
				throw new \App\Exceptions\Security("ERR_ILLEGAL_FIELD_VALUE||$columnName||" . print_r($value, true), 406);
0 ignored issues
show
Bug introduced by
Are you sure print_r($value, true) of type string|true can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

79
				throw new \App\Exceptions\Security("ERR_ILLEGAL_FIELD_VALUE||$columnName||" . /** @scrutinizer ignore-type */ print_r($value, true), 406);
Loading history...
80 2
			}
81
		} elseif (!\is_array($value) && \App\TextUtils::getTextLength($value) > $this->customMaximumLength[$columnName]) {
82
			throw new \App\Exceptions\Security("ERR_VALUE_IS_TOO_LONG||$columnName||" . print_r($value, true), 406);
83
		}
84
	}
85
86 2
	/** {@inheritdoc} */
87
	public function compare($value, $prevValue, string $column): bool
88 2
	{
89 2
		return $column === $this->getColumnName() ? (int) $value === (int) $prevValue : parent::compare($value, $prevValue, $column);
90 2
	}
91
}
92