From da85fcc6a8a13df78aa40fc088dbbb3df275252c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=A1clav=20Uruba?= Date: Tue, 8 Sep 2015 21:13:02 +0200 Subject: [PATCH] =?UTF-8?q?=E2=80=93=20period=20length=20in=20the=20DebtAm?= =?UTF-8?q?ortizator=20as=20a=20TimeSpan?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- readme.md | 6 +- src/Calculators/DebtAmortizator.php | 78 +++++++++---------- .../Factories/DebtAmortizatorFactory.php | 16 ++-- src/Utils/MathFuncs.php | 9 +++ tests/DebtAmortizatorTest.php | 61 ++++++++++----- 5 files changed, 98 insertions(+), 72 deletions(-) diff --git a/readme.md b/readme.md index f9cf79a..ea0d2f5 100644 --- a/readme.md +++ b/readme.md @@ -338,16 +338,16 @@ namespace `FinanCalc\Constants` ### DebtAmortizator namespace `FinanCalc\Calculators` -* **__construct($debtPrincipal, $debtNoOfCompoundingPeriods, $debtPeriodLength, $debtInterest)** +* **__construct($debtPrincipal, $debtNoOfCompoundingPeriods, TimeSpan $debtPeriodLength, $debtInterest)** * *$debtPrincipal* = **'PV'** – the principal of the debt (number greater than zero) * *$debtNoOfCompoundingPeriods* = **'n'** – number of the debt's compounding periods (number greater than zero) - * *$debtPeriodLength* = length of each of the debt's compounding periods in days (number greater than zero) + * *$debtPeriodLength* = length of each of the debt's compounding periods as a TimeSpan object * *$debtInterest* = **'i'** – interest per a compounding period, by which the outstanding balance is multiplied (i.e., a decimal number typically lower than 1 and greater than 0) ##### Setters * **setDebtPrincipal($debtPrincipal)** – sets PV * **setDebtNoOfCompoundingPeriods($debtNoOfCompoundingPeriods)** – sets n -* **setDebtPeriodLength($debtPeriodLength)** – sets the length of each compounding period in days +* **setDebtPeriodLength(TimeSpan $debtPeriodLength)** – sets the length of each compounding period * **setDebtInterest($debtInterest)** – sets i ##### Getters diff --git a/src/Calculators/DebtAmortizator.php b/src/Calculators/DebtAmortizator.php index cc3343d..27c499b 100644 --- a/src/Calculators/DebtAmortizator.php +++ b/src/Calculators/DebtAmortizator.php @@ -5,7 +5,7 @@ use FinanCalc\Interfaces\Calculator\CalculatorAbstract; use FinanCalc\Utils\Helpers; use FinanCalc\Utils\MathFuncs; - use FinanCalc\Utils\Time\TimeUtils; + use FinanCalc\Utils\Time\TimeSpan; /** * Class DebtAmortizator @@ -21,6 +21,7 @@ class DebtAmortizator extends CalculatorAbstract { // number of periods pertaining to the interest compounding = 'n' private $debtNoOfCompoundingPeriods; // length of a single period in days + /** @var TimeSpan */ private $debtPeriodLength; // the interest rate by which the unpaid balance is multiplied (i.e., a decimal number) = 'i' private $debtInterest; @@ -48,15 +49,14 @@ class DebtAmortizator extends CalculatorAbstract { ]; /** - * @param string $debtPrincipal [Value of the debt's principal as a string] - * @param string $debtNoOfCompoundingPeriods [Number of the debt's compounding periods as a string] - * @param string $debtPeriodLength [Length of each of the debt's compounding periods in days as a string] - * @param string $debtInterest [Value of the debt's interest in a decimal number 'multiplier' form as a string] - * @throws \InvalidArgumentException + * @param $debtPrincipal + * @param $debtNoOfCompoundingPeriods + * @param TimeSpan $debtPeriodLength + * @param $debtInterest */ function __construct($debtPrincipal, $debtNoOfCompoundingPeriods, - $debtPeriodLength, + TimeSpan $debtPeriodLength, $debtInterest) { $this->setDebtPrincipalWithoutRecalculation($debtPrincipal); $this->setDebtNoOfCompoundingPeriodsWithoutRecalculation($debtNoOfCompoundingPeriods); @@ -109,20 +109,20 @@ public function setDebtNoOfCompoundingPeriods($debtNoOfCompoundingPeriods) { } /** - * @param $debtInterest + * @param $debtPeriodLength */ - public function setDebtInterest($debtInterest) { - $this->setDebtInterestWithoutRecalculation($debtInterest); - $this->calculateDebtRepayments(); + public function setDebtPeriodLength(TimeSpan $debtPeriodLength) { + if (Helpers::checkIfPositiveNumberOrThrowAnException((string)$debtPeriodLength)) { + $this->debtPeriodLength = $debtPeriodLength; + } } /** - * @param $debtPeriodLength + * @param $debtInterest */ - public function setDebtPeriodLength($debtPeriodLength) { - if (Helpers::checkIfPositiveNumberOrThrowAnException($debtPeriodLength)) { - $this->debtPeriodLength = (string)$debtPeriodLength; - } + public function setDebtInterest($debtInterest) { + $this->setDebtInterestWithoutRecalculation($debtInterest); + $this->calculateDebtRepayments(); } /** @@ -161,31 +161,32 @@ public function getDebtNoOfCompoundingPeriods() { return $this->debtNoOfCompoundingPeriods; } + /** + * @return TimeSpan + */ + public function getDebtPeriodLength() { + return $this->debtPeriodLength; + } + /** * @return string [Length of each of the debt's compounding periods in years as a string] */ public function getDebtPeriodLengthInYears() { - return TimeUtils::getYearsFromDays( - $this->debtPeriodLength - ); + return $this->debtPeriodLength->toYears(); } /** * @return string [Length of each of the debt's compounding periods in months as a string] */ public function getDebtPeriodLengthInMonths() { - return TimeUtils::getMonthsFromDays( - $this->debtPeriodLength - ); + return $this->debtPeriodLength->toMonths(); } /** * @return string [Length of each of the debt's compounding periods in days as a string] */ public function getDebtPeriodLengthInDays() { - return TimeUtils::getDaysFromDays( - $this->debtPeriodLength - ); + return $this->debtPeriodLength->toDays(); } /** @@ -214,33 +215,30 @@ public function getDebtDiscountFactor() { * @return string [Duration of the debt in years as a string] */ public function getDebtDurationInYears() { - return TimeUtils::getYearsFromDays( - MathFuncs::mul( - $this->debtNoOfCompoundingPeriods, - $this->debtPeriodLength) - ); + return MathFuncs::mul( + $this->debtNoOfCompoundingPeriods, + $this->debtPeriodLength->toYears() + ); } /** * @return string [Duration of the debt in months as a string] */ public function getDebtDurationInMonths() { - return TimeUtils::getMonthsFromDays( - MathFuncs::mul( - $this->debtNoOfCompoundingPeriods, - $this->debtPeriodLength) - ); + return MathFuncs::mul( + $this->debtNoOfCompoundingPeriods, + $this->debtPeriodLength->toMonths() + ); } /** * @return string [Duration of the debt in years as a string] */ public function getDebtDurationInDays() { - return TimeUtils::getDaysFromDays( - MathFuncs::mul( - $this->debtNoOfCompoundingPeriods, - $this->debtPeriodLength) - ); + return MathFuncs::mul( + $this->debtNoOfCompoundingPeriods, + $this->debtPeriodLength->toDays() + ); } /** diff --git a/src/Calculators/Factories/DebtAmortizatorFactory.php b/src/Calculators/Factories/DebtAmortizatorFactory.php index 82492cc..c64d617 100644 --- a/src/Calculators/Factories/DebtAmortizatorFactory.php +++ b/src/Calculators/Factories/DebtAmortizatorFactory.php @@ -3,7 +3,7 @@ namespace FinanCalc\Calculators\Factories { use \FinanCalc\Calculators\DebtAmortizator; use FinanCalc\Interfaces\Calculator\CalculatorFactoryAbstract; - use FinanCalc\Utils\Time\TimeUtils; + use FinanCalc\Utils\Time\TimeSpan; /** * Class DebtAmortizatorFactory @@ -24,7 +24,7 @@ public function newYearlyDebtAmortization($debtPrincipal, $debtNoOfPeriods, $deb [ $debtPrincipal, $debtNoOfPeriods, - TimeUtils::getDaysFromYears(1), + TimeSpan::asDuration(1), $debtInterest ] ); @@ -42,7 +42,7 @@ public function newMonthlyDebtAmortization($debtPrincipal, $debtNoOfPeriods, $de [ $debtPrincipal, $debtNoOfPeriods, - TimeUtils::getDaysFromMonths(1), + TimeSpan::asDuration(0, 1), $debtInterest ] ); @@ -60,7 +60,7 @@ public function newDailyDebtAmortization($debtPrincipal, $debtNoOfPeriods, $debt [ $debtPrincipal, $debtNoOfPeriods, - 1, + TimeSpan::asDuration(0, 0, 1), $debtInterest ] ); @@ -69,16 +69,16 @@ public function newDailyDebtAmortization($debtPrincipal, $debtNoOfPeriods, $debt /** * @param $debtPrincipal * @param $debtNoOfPeriods + * @param TimeSpan $debtPeriodLength * @param $debtInterest - * @param $debtSinglePeriodLength - * @return DebtAmortizator + * @return \FinanCalc\Interfaces\Calculator\CalculatorAbstract */ - public function newDebtAmortizationCustomPeriodLength($debtPrincipal, $debtNoOfPeriods, $debtInterest, $debtSinglePeriodLength) { + public function newDebtAmortizationCustomPeriodLength($debtPrincipal, $debtNoOfPeriods, TimeSpan $debtPeriodLength, $debtInterest) { return $this->manufactureInstance( [ $debtPrincipal, $debtNoOfPeriods, - $debtSinglePeriodLength, + $debtPeriodLength, $debtInterest ] ); diff --git a/src/Utils/MathFuncs.php b/src/Utils/MathFuncs.php index 1cdcb75..1fbd692 100644 --- a/src/Utils/MathFuncs.php +++ b/src/Utils/MathFuncs.php @@ -61,5 +61,14 @@ public static function pow($leftOperand, $rightOperand) { public static function comp($leftOperand, $rightOperand) { return bccomp($leftOperand, $rightOperand, Defaults::MONEY_DECIMAL_PLACES_PRECISION); } + + /** + * @param $roundedNumber + * @param $precision + * @return string + */ + public static function round($roundedNumber, $precision = 2) { + return (string)round((float)$roundedNumber, $precision); + } } } diff --git a/tests/DebtAmortizatorTest.php b/tests/DebtAmortizatorTest.php index 8c55751..3ef7805 100644 --- a/tests/DebtAmortizatorTest.php +++ b/tests/DebtAmortizatorTest.php @@ -3,6 +3,7 @@ use FinanCalc\Calculators\DebtAmortizator; use FinanCalc\FinanCalc; use FinanCalc\Utils\MathFuncs; +use FinanCalc\Utils\Time\TimeSpan; use FinanCalc\Utils\Time\TimeUtils; /** @@ -55,7 +56,7 @@ public function testRepaymentsDirectYearly() { $debtAmortizatorDirect = new DebtAmortizator( 40000, 6, - TimeUtils::getDaysFromYears(1), + TimeSpan::asDuration(1), 0.12); $this->processResult($debtAmortizatorDirect); @@ -110,8 +111,8 @@ public function testRepaymentsFactoryCustom() { ->newDebtAmortizationCustomPeriodLength( 40000, 6, - 0.12, - TimeUtils::getDaysFromYears(1)); + TimeSpan::asDuration(1), + 0.12); $this->processResult($debtAmortizatorFactory); $this->processArray($debtAmortizatorFactory->getResultAsArray()); @@ -139,17 +140,27 @@ private function processResult(DebtAmortizator $result) { $result->getDebtPeriodLengthInMonths()); $this->assertEquals( - MathFuncs::div( - $result->getDebtDurationInDays(), - TimeUtils::getDaysFromYears(1) - ), - $result->getDebtDurationInYears()); + MathFuncs::round( + MathFuncs::div( + $result->getDebtDurationInDays(), + TimeUtils::getDaysFromYears(1) + ) + ), + MathFuncs::round( + $result->getDebtDurationInYears() + ) + ); $this->assertEquals( - MathFuncs::div( - $result->getDebtDurationInDays(), - TimeUtils::getDaysFromMonths(1) - ), - $result->getDebtDurationInMonths()); + MathFuncs::round( + MathFuncs::div( + $result->getDebtDurationInDays(), + TimeUtils::getDaysFromMonths(1) + ) + ), + MathFuncs::round( + $result->getDebtDurationInMonths() + ) + ); $this->assertEquals("0.12", $result->getDebtInterest()); @@ -203,17 +214,25 @@ private function processArray(array $resultArray) { $resultArray["debtPeriodLength"]["months"]); $this->assertEquals( - MathFuncs::div( - $resultArray["debtDuration"]["days"], - TimeUtils::getDaysFromYears(1) + MathFuncs::round( + MathFuncs::div( + $resultArray["debtDuration"]["days"], + TimeUtils::getDaysFromYears(1) + ) ), - $resultArray["debtDuration"]["years"]); + MathFuncs::round( + $resultArray["debtDuration"]["years"] + )); $this->assertEquals( - MathFuncs::div( - $resultArray["debtDuration"]["days"], - TimeUtils::getDaysFromMonths(1) + MathFuncs::round( + MathFuncs::div( + $resultArray["debtDuration"]["days"], + TimeUtils::getDaysFromMonths(1) + ) ), - $resultArray["debtDuration"]["months"]); + MathFuncs::round( + $resultArray["debtDuration"]["months"] + )); $this->assertEquals("0.12", $resultArray["debtInterest"]);