Passed
Push — master ( 01f5f1...8a4484 )
by Aimeos
04:51
created

Base::getPrecision()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * @license LGPLv3, https://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2016-2024
6
 * @package MShop
7
 * @subpackage Price
8
 */
9
10
11
namespace Aimeos\MShop\Price\Item;
12
13
14
/**
15
 * Basic methods for all price implementations
16
 *
17
 * @package MShop
18
 * @subpackage Price
19
 */
20
abstract class Base
21
	extends \Aimeos\MShop\Common\Item\Base
22
	implements \Aimeos\MShop\Price\Item\Iface
23
{
24
	/**
25
	 * Compares the properties of the given price item with its own one.
26
	 *
27
	 * This method compare only the essential price properties:
28
	 * * Value
29
	 * * Costs
30
	 * * Rebate
31
	 * * Tax rate
32
	 * * Tax flag
33
	 * * Quantity
34
	 * * Currency ID
35
	 *
36
	 * All other item properties are not compared.
37
	 *
38
	 * @param \Aimeos\MShop\Price\Item\Iface $price Price item to compare with
39
	 * @return bool True if equal, false if not
40
	 * @since 2014.09
41
	 */
42
	public function compare( \Aimeos\MShop\Price\Item\Iface $price ) : bool
43
	{
44
		if( $this->getValue() === $price->getValue()
45
			&& $this->getCosts() === $price->getCosts()
46
			&& $this->getRebate() === $price->getRebate()
47
			&& $this->getTaxFlag() === $price->getTaxFlag()
48
			&& $this->getTaxRate() === $price->getTaxRate()
49
			&& $this->getTaxRates() === $price->getTaxRates()
50
			&& $this->getQuantity() === $price->getQuantity()
51
			&& $this->getCurrencyId() === $price->getCurrencyId()
52
		) {
53
			return true;
54
		}
55
56
		return false;
57
	}
58
59
60
	/**
61
	 * Tests if the price is within the requirements.
62
	 *
63
	 * @param string|int|float|null $value Monetary value
64
	 * @param int|null $precision Number of decimal digits, null for default value
65
	 * @return string|null Sanitized monetary value
66
	 */
67
	protected function checkPrice( $value, int $precision = null ) : ?string
68
	{
69
		if( $value != '' && !is_numeric( $value ) ) {
70
			throw new \Aimeos\MShop\Price\Exception( sprintf( 'Invalid characters in price "%1$s"', $value ) );
71
		}
72
73
		return $this->formatNumber( $value !== '' ? $value : null, $precision );
74
	}
75
76
77
	/**
78
	 * Formats the money value.
79
	 *
80
	 * @param string|int|float|null $number Money value
81
	 * @param int|null $precision Number of decimal digits, null for default value
82
	 * @return string|null Formatted money value
83
	 */
84
	protected function formatNumber( $number, int $precision = null ) : ?string
85
	{
86
		return $number !== null ? number_format( $number, $precision ?: $this->getPrecision(), '.', '' ) : null;
0 ignored issues
show
Bug introduced by
It seems like $number can also be of type string; however, parameter $num of number_format() does only seem to accept double, maybe add an additional type check? ( Ignorable by Annotation )

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

86
		return $number !== null ? number_format( /** @scrutinizer ignore-type */ $number, $precision ?: $this->getPrecision(), '.', '' ) : null;
Loading history...
87
	}
88
}
89