Euro::getEuroString()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 2
rs 10
ccs 2
cts 2
cp 1
crap 1
1
<?php
2
3
declare( strict_types = 1 );
4
5
namespace WMDE\Euro;
6
7
use InvalidArgumentException;
8
9
/**
10
 * @license GPL-2.0-or-later
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 int $cents;
19
20
	/**
21
	 * @param int $cents
22
	 * @throws InvalidArgumentException
23
	 */
24 52
	private function __construct( int $cents ) {
25 52
		if ( $cents < 0 ) {
26 4
			throw new InvalidArgumentException( 'Amount needs to be positive' );
27
		}
28
29 48
		$this->cents = $cents;
30 48
	}
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 29
	public static function newFromCents( int $cents ): self {
52 29
		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 18
	public static function newFromString( string $euroAmount ): self {
67 18
		if ( !is_numeric( $euroAmount ) ) {
68 3
			throw new InvalidArgumentException( 'Not a number' );
69
		}
70
71 15
		if ( self::stringIsTooLong( $euroAmount ) ) {
72 3
			throw new InvalidArgumentException( 'Number is too big' );
73
		}
74
75 12
		$parts = explode( '.', $euroAmount, 2 );
76
77 12
		$euros = (int)$parts[0];
78 12
		$cents = self::centsFromString( $parts[1] ?? '0' );
79
80 12
		return new self( $euros * self::CENTS_PER_EURO + $cents );
81
	}
82
83 15
	private static function stringIsTooLong( string $euroString ): bool {
84 15
		return strlen( $euroString ) + 2 > log10( PHP_INT_MAX );
85
	}
86
87 12
	private static function centsFromString( string $cents ): int {
88 12
		if ( strlen( $cents ) > self::DECIMAL_COUNT ) {
89 2
			return self::roundCentsToInt( $cents );
90
		}
91
92
		// Turn .1 into .10, so it ends up as 10 cents
93 10
		return (int)str_pad( $cents, self::DECIMAL_COUNT, '0' );
94
	}
95
96 2
	private static function roundCentsToInt( string $cents ): int {
97 2
		$centsInt = (int)substr( $cents, 0, self::DECIMAL_COUNT );
98
99 2
		if ( (int)$cents[self::DECIMAL_COUNT] >= 5 ) {
100 1
			$centsInt++;
101
		}
102
103 2
		return $centsInt;
104
	}
105
106
	/**
107
	 * This method takes into account the errors that can arise from floating
108
	 * point number usage. Amounts with too many decimals are rounded to the
109
	 * nearest whole euro cent amount.
110
	 *
111
	 * @param float $euroAmount
112
	 * @return self
113
	 * @throws InvalidArgumentException
114
	 */
115 9
	public static function newFromFloat( float $euroAmount ): self {
116 9
		self::assertMaximumValueNotExceeded( $euroAmount );
117 6
		return new self( intval(
118 6
			round(
119 6
				round( $euroAmount, self::DECIMAL_COUNT ) * self::CENTS_PER_EURO,
120
				0
121
			)
122
		) );
123
	}
124
125
	/**
126
	 * @param int|float $euroAmount
127
	 */
128 18
	private static function assertMaximumValueNotExceeded( $euroAmount ): void {
129
		// When $euroAmount == PHP_INT_MAX / self::CENTS_PER_EURO, multiplying it
130
		// by self::CENTS_PER_EURO still exceeds PHP_INT_MAX, which leads type errors
131
		// due to float conversion. The safest thing to do here is using a safety
132
		// margin of 1 with self::CENTS_PER_EURO
133 18
		if ( $euroAmount > floor( PHP_INT_MAX / ( self::CENTS_PER_EURO + 1 ) ) ) {
134 6
			throw new InvalidArgumentException( 'Number is too big' );
135
		}
136 12
	}
137
138
	/**
139
	 * @param int $euroAmount
140
	 * @return self
141
	 * @throws InvalidArgumentException
142
	 */
143 9
	public static function newFromInt( int $euroAmount ): self {
144 9
		self::assertMaximumValueNotExceeded( $euroAmount );
145 6
		return new self( $euroAmount * self::CENTS_PER_EURO );
146
	}
147
148 31
	public function getEuroCents(): int {
149 31
		return $this->cents;
150
	}
151
152 12
	public function getEuroFloat(): float {
153 12
		return $this->cents / self::CENTS_PER_EURO;
154
	}
155
156
	public function getEuros(): int {
157
		return intval( $this->cents / self::CENTS_PER_EURO );
158
	}
159
160
	/**
161 8
	 * Returns the euro amount as string with two decimals always present in format "42.00".
162 8
	 *
163
	 * @return string
164
	 */
165 9
	public function getEuroString(): string {
166 9
		return number_format( $this->getEuroFloat(), self::DECIMAL_COUNT, '.', '' );
167
	}
168
169
	public function equals( Euro $euro ): bool {
170
		return $this->cents === $euro->cents;
171
	}
172
173
}
174