Passed
Push — master ( c7a8cf...1689c1 )
by Aimeos
06:55
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, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2016-2018-2018
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
	use \Aimeos\MShop\Common\Item\ListRef\Traits;
25
26
27
	private $precision;
28
29
30
	/**
31
	 * Initalizes the object with the given values
32
	 *
33
	 * @param string $prefix Prefix for the keys returned by toArray()
34
	 * @param array $values Associative array of key/value pairs for price, costs, rebate and currencyid
35
	 * @param \Aimeos\MShop\Common\Lists\Item\Iface[] $listItems List of list items
36
	 * @param \Aimeos\MShop\Common\Item\Iface[] $refItems List of referenced items
37
	 * @param integer $precision Number of decimal digits
38
	 */
39
	public function __construct( $prefix, array $values = [], array $listItems = [], array $refItems = [], $precision = 2 )
40
	{
41
		parent::__construct( $prefix, $values );
42
43
		$this->initListItems( $listItems, $refItems );
44
		$this->precision = $precision;
45
	}
46
47
48
	/**
49
	 * Compares the properties of the given price item with its own one.
50
	 *
51
	 * This method compare only the essential price properties:
52
	 * * Value
53
	 * * Costs
54
	 * * Rebate
55
	 * * Tax rate
56
	 * * Tax flag
57
	 * * Quantity
58
	 * * Currency ID
59
	 *
60
	 * All other item properties are not compared.
61
	 *
62
	 * @param \Aimeos\MShop\Price\Item\Iface $price Price item to compare with
63
	 * @return boolean True if equal, false if not
64
	 * @since 2014.09
65
	 */
66
	public function compare( \Aimeos\MShop\Price\Item\Iface $price )
67
	{
68
		if( $this->getValue() === $price->getValue()
69
			&& $this->getCosts() === $price->getCosts()
70
			&& $this->getRebate() === $price->getRebate()
71
			&& $this->getTaxRate() === $price->getTaxRate()
72
			&& $this->getTaxFlag() === $price->getTaxFlag()
73
			&& $this->getQuantity() === $price->getQuantity()
74
			&& $this->getCurrencyId() === $price->getCurrencyId()
75
		) {
76
			return true;
77
		}
78
79
		return false;
80
	}
81
82
83
	/**
84
	 * Returns the decimal precision of the price
85
	 *
86
	 * @return integer Number of decimal digits
87
	 */
88
	public function getPrecision()
89
	{
90
		return $this->precision;
91
	}
92
93
94
	/**
95
	 * Returns the item type
96
	 *
97
	 * @return string Item type, subtypes are separated by slashes
98
	 */
99
	public function getResourceType()
100
	{
101
		return 'price';
102
	}
103
104
105
	/**
106
	 * Tests if the price is within the requirements.
107
	 *
108
	 * @param string|integer|double $value Monetary value
109
	 * @param integer|null $precision Number of decimal digits, null for default value
110
	 * @return string Sanitized monetary value
111
	 */
112
	protected function checkPrice( $value, $precision = null )
113
	{
114
		if( $value != '' && !is_numeric( $value ) ) {
115
			throw new \Aimeos\MShop\Price\Exception( sprintf( 'Invalid characters in price "%1$s"', $value ) );
116
		}
117
118
		return $this->formatNumber( $value, $precision );
119
	}
120
121
122
	/**
123
	 * Formats the money value.
124
	 *
125
	 * @param string|integer|double $number Money value
126
	 * @param integer|null $precision Number of decimal digits, null for default value
127
	 * @return string Formatted money value
128
	 */
129
	protected function formatNumber( $number, $precision = null )
130
	{
131
		return number_format( (double) $number, $precision ?: $this->precision, '.', '' );
132
	}
133
}
134