Skip to content

Commit

Permalink
fix static calculator and some code style
Browse files Browse the repository at this point in the history
  • Loading branch information
piotrantosik committed Jan 12, 2016
1 parent 984e76b commit 0a2565f
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 30 deletions.
3 changes: 2 additions & 1 deletion src/Calculator.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
<?php

namespace Money;

/**
* Calculator Interface
*
* @author Frederik Bosch
*/
interface Calculator
Expand Down Expand Up @@ -94,5 +96,4 @@ public function round($number, $roundingMode);
* @return int|string
*/
public function share($amount, $ratio, $total);

}
72 changes: 45 additions & 27 deletions src/Money.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Money;

use InvalidArgumentException;
use RuntimeException;
use JsonSerializable;

/**
Expand Down Expand Up @@ -34,7 +35,7 @@ final class Money implements JsonSerializable
/**
* @var Calculator
*/
private static $calculator;
private $calculator;

/**
* @var array
Expand All @@ -59,10 +60,6 @@ public function __construct($amount, Currency $currency)

$this->amount = $amount;
$this->currency = $currency;

if (self::$calculator === null) {
self::$calculator = $this->initializeCalculator();
}
}

/**
Expand All @@ -76,6 +73,8 @@ public function __construct($amount, Currency $currency)
* @param array $arguments
*
* @return Money
*
* @throws InvalidArgumentException If amount is not integer
*/
public static function __callStatic($method, $arguments)
{
Expand All @@ -88,6 +87,8 @@ public static function __callStatic($method, $arguments)
* @param int $amount
*
* @return Money
*
* @throws InvalidArgumentException If amount is not integer
*/
private function newInstance($amount)
{
Expand All @@ -109,6 +110,8 @@ public function isSameCurrency(Money $other)
/**
* Asserts that a Money has the same currency as this.
*
* @param Money $other
*
* @throws InvalidArgumentException If $other has a different currency
*/
private function assertSameCurrency(Money $other)
Expand All @@ -127,7 +130,7 @@ private function assertSameCurrency(Money $other)
*/
public function equals(Money $other)
{
return $this->isSameCurrency($other) && $this->amount == $other->amount;
return $this->isSameCurrency($other) && $this->amount === $other->amount;
}

/**
Expand All @@ -142,7 +145,8 @@ public function equals(Money $other)
public function compare(Money $other)
{
$this->assertSameCurrency($other);
return self::$calculator->compare($this->amount, $other->amount);

return $this->getCalculator()->compare($this->amount, $other->amount);
}

/**
Expand All @@ -154,7 +158,7 @@ public function compare(Money $other)
*/
public function greaterThan(Money $other)
{
return 1 == $this->compare($other);
return 1 === $this->compare($other);
}

/**
Expand All @@ -176,7 +180,7 @@ public function greaterThanOrEqual(Money $other)
*/
public function lessThan(Money $other)
{
return -1 == $this->compare($other);
return -1 === $this->compare($other);
}

/**
Expand Down Expand Up @@ -233,7 +237,7 @@ public function add(Money $addend)
{
$this->assertSameCurrency($addend);

return new self(self::$calculator->add($this->amount, $addend->amount), $this->currency);
return new self($this->getCalculator()->add($this->amount, $addend->amount), $this->currency);
}

/**
Expand All @@ -248,7 +252,7 @@ public function subtract(Money $subtrahend)
{
$this->assertSameCurrency($subtrahend);

return new self(self::$calculator->subtract($this->amount, $subtrahend->amount), $this->currency);
return new self($this->getCalculator()->subtract($this->amount, $subtrahend->amount), $this->currency);
}

/**
Expand Down Expand Up @@ -276,7 +280,7 @@ private function assertRoundingMode($roundingMode)
$roundingMode, [
self::ROUND_HALF_DOWN, self::ROUND_HALF_EVEN, self::ROUND_HALF_ODD,
self::ROUND_HALF_UP, self::ROUND_UP, self::ROUND_DOWN,
]
], true
)) {
throw new InvalidArgumentException(
'Rounding mode should be Money::ROUND_HALF_DOWN | '.
Expand All @@ -301,7 +305,7 @@ public function multiply($multiplier, $roundingMode = self::ROUND_HALF_UP)

$this->assertRoundingMode($roundingMode);

$product = $this->round(self::$calculator->multiply($this->amount, $multiplier), $roundingMode);
$product = $this->round($this->getCalculator()->multiply($this->amount, $multiplier), $roundingMode);

return $this->newInstance($product);
}
Expand All @@ -316,7 +320,7 @@ public function multiply($multiplier, $roundingMode = self::ROUND_HALF_UP)
public function convert(Currency $targetCurrency, $conversionRate, $roundingMode = self::ROUND_HALF_UP)
{
$this->assertRoundingMode($roundingMode);
$amount = self::$calculator->round(self::$calculator->multiply($this->amount, $conversionRate), $roundingMode);
$amount = $this->getCalculator()->round($this->getCalculator()->multiply($this->amount, $conversionRate), $roundingMode);
return new Money($amount, $targetCurrency);
}

Expand All @@ -338,7 +342,7 @@ public function divide($divisor, $roundingMode = self::ROUND_HALF_UP)
throw new InvalidArgumentException('Division by zero');
}

$quotient = $this->round(self::$calculator->divide($this->amount, $divisor), $roundingMode);
$quotient = $this->round($this->getCalculator()->divide($this->amount, $divisor), $roundingMode);

return $this->newInstance($quotient);
}
Expand All @@ -357,14 +361,14 @@ public function allocate(array $ratios)
$total = array_sum($ratios);

foreach ($ratios as $ratio) {
$share = self::$calculator->share($this->amount, $ratio, $total);
$share = $this->getCalculator()->share($this->amount, $ratio, $total);
$results[] = $this->newInstance($share);
$remainder = self::$calculator->subtract($remainder, $share);
$remainder = $this->getCalculator()->subtract($remainder, $share);
}

for ($i = 0; self::$calculator->compare($remainder, 0) === 1; $i++) {
$results[$i]->amount = self::$calculator->add($results[$i]->amount, 1);
$remainder = self::$calculator->subtract($remainder, 1);
for ($i = 0; $this->getCalculator()->compare($remainder, 0) === 1; $i++) {
$results[$i]->amount = $this->getCalculator()->add($results[$i]->amount, 1);
$remainder = $this->getCalculator()->subtract($remainder, 1);
}

return $results;
Expand Down Expand Up @@ -398,13 +402,13 @@ private function round($amount, $rounding_mode)
{
$this->assertRoundingMode($rounding_mode);
if ($rounding_mode === self::ROUND_UP) {
return self::$calculator->ceil($amount);
return $this->getCalculator()->ceil($amount);
}
if ($rounding_mode === self::ROUND_DOWN) {
return self::$calculator->floor($amount);
return $this->getCalculator()->floor($amount);
}

return self::$calculator->round($amount, $rounding_mode);
return $this->getCalculator()->round($amount, $rounding_mode);
}

/**
Expand All @@ -414,7 +418,7 @@ private function round($amount, $rounding_mode)
*/
public function isZero()
{
return 0 === self::$calculator->compare($this->amount, 0);
return 0 === $this->getCalculator()->compare($this->amount, 0);
}

/**
Expand All @@ -424,7 +428,7 @@ public function isZero()
*/
public function isPositive()
{
return 1 === self::$calculator->compare($this->amount, 0);
return 1 === $this->getCalculator()->compare($this->amount, 0);
}

/**
Expand All @@ -434,7 +438,7 @@ public function isPositive()
*/
public function isNegative()
{
return -1 === self::$calculator->compare($this->amount, 0);
return -1 === $this->getCalculator()->compare($this->amount, 0);
}

/**
Expand Down Expand Up @@ -489,6 +493,8 @@ public static function registerCalculator(Calculator $calculator)

/**
* @return Calculator
*
* @throws RuntimeException If cannot find calculator for money calculations
*/
private static function initializeCalculator()
{
Expand All @@ -500,6 +506,18 @@ private static function initializeCalculator()
}
}

throw new \RuntimeException('Cannot find calculator for money calculations');
throw new RuntimeException('Cannot find calculator for money calculations');
}

/**
* @return Calculator
*/
private function getCalculator()
{
if ($this->calculator === null) {
$this->calculator = self::initializeCalculator();
}

return $this->calculator;
}
}
3 changes: 1 addition & 2 deletions src/Number.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<?php

namespace Money;

/**
* Class Number
* @package Money
*/
final class Number
{
Expand Down Expand Up @@ -94,5 +94,4 @@ public function __toString()
{
return $this->number;
}

}

0 comments on commit 0a2565f

Please sign in to comment.