Completed
Pull Request — master (#11)
by Tim
03:25
created

Euro::__toString()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
declare( strict_types = 1 );
4
5
namespace WMDE\Euro;
6
7
use InvalidArgumentException;
8
9
/**
10
 * @licence GNU GPL v2+
11
 * @author Jeroen De Dauw < [email protected] >
12
 */
13
final class Euro implements \JsonSerializable {
14
15
	private const DECIMAL_COUNT = 2;
16
	private const CENTS_PER_EURO = 100;
17
18
	private $cents;
19
20
	/**
21
	 * @param int $cents
22
	 * @throws InvalidArgumentException
23
	 */
24 54
	private function __construct( int $cents ) {
25 54
		if ( $cents < 0 ) {
26 5
			throw new InvalidArgumentException( 'Amount needs to be positive' );
27
		}
28
29 49
		$this->cents = $cents;
30 49
	}
31
32
	/**
33
	 * @return string
34
	 */
35 1
	public function __toString(): string {
36 1
		return $this->getEuroString();
37
	}
38
39
	/**
40
	 * @return string
41
	 */
42 1
	public function jsonSerialize(): string {
43 1
		return $this->getEuroString();
44
	}
45
46
	/**
47
	 * @param int $cents
48
	 * @return self
49
	 * @throws InvalidArgumentException
50
	 */
51 32
	public static function newFromCents( int $cents ): self {
52 32
		return new self( $cents );
53
	}
54
55
	/**
56
	 * Constructs a Euro object from a string representation such as "13.37".
57
	 *
58
	 * This method takes into account the errors that can arise from floating
59
	 * point number usage. Amounts with too many decimals are rounded to the
60
	 * nearest whole euro cent amount.
61
	 *
62
	 * @param string $euroAmount
63
	 * @return self
64
	 * @throws InvalidArgumentException
65
	 */
66 15
	public static function newFromString( string $euroAmount ): self {
67 15
		if ( !is_numeric( $euroAmount ) ) {
68 3
			throw new InvalidArgumentException( 'Not a number' );
69
		}
70
71 12
		$parts = explode( '.', $euroAmount, 2 );
72
73 12
		$euros = (int)$parts[0];
74 12
		$cents = self::centsFromString( $parts[1] ?? '0' );
75
76 12
		return new self( $euros * self::CENTS_PER_EURO + $cents );
77
	}
78
79 12
	private static function centsFromString( string $cents ): int {
80 12
		if ( strlen( $cents ) > self::DECIMAL_COUNT ) {
81 2
			return self::roundCentsToInt( $cents );
82
		}
83
84
		// Turn .1 into .10, so it ends up as 10 cents
85 10
		return (int)str_pad( $cents, self::DECIMAL_COUNT, '0' );
86
	}
87
88 2
	private static function roundCentsToInt( string $cents ): int {
89 2
		$centsInt = (int)substr( $cents, 0, self::DECIMAL_COUNT );
90
91 2
		if ( (int)$cents[self::DECIMAL_COUNT] >= 5 ) {
92 1
			$centsInt++;
93
		}
94
95 2
		return $centsInt;
96
	}
97
98
	/**
99
	 * This method takes into account the errors that can arise from floating
100
	 * point number usage. Amounts with too many decimals are rounded to the
101
	 * nearest whole euro cent amount.
102
	 *
103
	 * @param float $euroAmount
104
	 * @return self
105
	 * @throws InvalidArgumentException
106
	 */
107 6
	public static function newFromFloat( float $euroAmount ): self {
108 6
		return new self( intval(
109 6
			round(
110 6
				round( $euroAmount, self::DECIMAL_COUNT ) * self::CENTS_PER_EURO,
111
				0
112
			)
113
		) );
114
	}
115
116
	/**
117
	 * @param int $euroAmount
118
	 * @return self
119
	 * @throws InvalidArgumentException
120
	 */
121 5
	public static function newFromInt( int $euroAmount ): self {
122 5
		return new self( $euroAmount * self::CENTS_PER_EURO );
123
	}
124
125 32
	public function getEuroCents(): int {
126 32
		return $this->cents;
127
	}
128
129 12
	public function getEuroFloat(): float {
130 12
		return $this->cents / self::CENTS_PER_EURO;
131
	}
132
133
	/**
134
	 * Returns the euro amount as string with two decimals always present in format "42.00".
135
	 */
136 8
	public function getEuroString(): string {
137 8
		return number_format( $this->getEuroFloat(), self::DECIMAL_COUNT, '.', '' );
138
	}
139
140 10
	public function equals( Euro $euro ): bool {
141 10
		return $this->cents === $euro->cents;
142
	}
143
144
}
145