Skip to content

Commit

Permalink
Merge 4e36519 into f4a3a20
Browse files Browse the repository at this point in the history
  • Loading branch information
JeroenDeDauw committed Mar 20, 2018
2 parents f4a3a20 + 4e36519 commit 0a534cb
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 17 deletions.
4 changes: 4 additions & 0 deletions README.md
Expand Up @@ -96,6 +96,10 @@ For a full CI run

## Release notes

### 1.1.0 (2018-03-20)

* Bumped minimum PHP version from 7.0 to 7.1

### 1.0.2 (2018-03-20)

* Internal changes to avoid dealing with floats when constructing an Euro object from string
Expand Down
8 changes: 4 additions & 4 deletions composer.json
Expand Up @@ -4,13 +4,13 @@
"homepage": "https://github.com/wmde/Euro",
"license": "GPL-2.0-or-later",
"require": {
"php": ">=7.0"
"php": ">=7.1"
},
"require-dev": {
"phpunit/phpunit": "~6.2",
"giorgiosironi/eris": "~0.9.0",
"squizlabs/php_codesniffer": "~2.5",
"mediawiki/mediawiki-codesniffer": "~0.6.0",
"squizlabs/php_codesniffer": "~3.2",
"mediawiki/mediawiki-codesniffer": "~15.0",
"ockcyp/covers-validator": "~0.6"
},
"autoload": {
Expand All @@ -20,7 +20,7 @@
},
"extra": {
"branch-alias": {
"dev-master": "1.0.x-dev"
"dev-master": "1.1.x-dev"
}
},
"scripts": {
Expand Down
26 changes: 13 additions & 13 deletions src/Euro.php
Expand Up @@ -12,8 +12,8 @@
*/
final class Euro {

private static $DECIMAL_COUNT = 2;
private static $CENTS_PER_EURO = 100;
private const DECIMAL_COUNT = 2;
private const CENTS_PER_EURO = 100;

private $cents;

Expand Down Expand Up @@ -59,22 +59,22 @@ public static function newFromString( string $euroAmount ): self {
$euros = (int)$parts[0];
$cents = self::centsFromString( $parts[1] ?? '0' );

return new self( $euros * self::$CENTS_PER_EURO + $cents );
return new self( $euros * self::CENTS_PER_EURO + $cents );
}

private static function centsFromString( string $cents ): int {
if ( strlen( $cents ) > self::$DECIMAL_COUNT ) {
if ( strlen( $cents ) > self::DECIMAL_COUNT ) {
return self::roundCentsToInt( $cents );
}

// Turn .1 into .10, so it ends up as 10 cents
return (int)str_pad( $cents, self::$DECIMAL_COUNT, '0' );
return (int)str_pad( $cents, self::DECIMAL_COUNT, '0' );
}

private static function roundCentsToInt( string $cents ): int {
$centsInt = (int)substr( $cents, 0, self::$DECIMAL_COUNT );
$centsInt = (int)substr( $cents, 0, self::DECIMAL_COUNT );

if ( (int)$cents[self::$DECIMAL_COUNT] >= 5 ) {
if ( (int)$cents[self::DECIMAL_COUNT] >= 5 ) {
$centsInt++;
}

Expand All @@ -90,10 +90,10 @@ private static function roundCentsToInt( string $cents ): int {
* @return self
* @throws InvalidArgumentException
*/
public static function newFromFloat( float $euroAmount ) {
public static function newFromFloat( float $euroAmount ): self {
return new self( intval(
round(
round( $euroAmount, self::$DECIMAL_COUNT ) * self::$CENTS_PER_EURO,
round( $euroAmount, self::DECIMAL_COUNT ) * self::CENTS_PER_EURO,
0
)
) );
Expand All @@ -104,23 +104,23 @@ public static function newFromFloat( float $euroAmount ) {
* @return self
* @throws InvalidArgumentException
*/
public static function newFromInt( int $euroAmount ) {
return new self( $euroAmount * self::$CENTS_PER_EURO );
public static function newFromInt( int $euroAmount ): self {
return new self( $euroAmount * self::CENTS_PER_EURO );
}

public function getEuroCents(): int {
return $this->cents;
}

public function getEuroFloat(): float {
return $this->cents / self::$CENTS_PER_EURO;
return $this->cents / self::CENTS_PER_EURO;
}

/**
* Returns the euro amount as string with two decimals always present in format "42.00".
*/
public function getEuroString(): string {
return number_format( $this->getEuroFloat(), self::$DECIMAL_COUNT, '.', '' );
return number_format( $this->getEuroFloat(), self::DECIMAL_COUNT, '.', '' );
}

public function equals( Euro $euro ): bool {
Expand Down

0 comments on commit 0a534cb

Please sign in to comment.