Skip to content

Commit

Permalink
– period length in the DebtAmortizator as a TimeSpan
Browse files Browse the repository at this point in the history
  • Loading branch information
uruba committed Sep 8, 2015
1 parent ec46828 commit da85fcc
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 72 deletions.
6 changes: 3 additions & 3 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
78 changes: 38 additions & 40 deletions src/Calculators/DebtAmortizator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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();
}

/**
Expand Down Expand Up @@ -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();
}

/**
Expand Down Expand Up @@ -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()
);
}

/**
Expand Down
16 changes: 8 additions & 8 deletions src/Calculators/Factories/DebtAmortizatorFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -24,7 +24,7 @@ public function newYearlyDebtAmortization($debtPrincipal, $debtNoOfPeriods, $deb
[
$debtPrincipal,
$debtNoOfPeriods,
TimeUtils::getDaysFromYears(1),
TimeSpan::asDuration(1),
$debtInterest
]
);
Expand All @@ -42,7 +42,7 @@ public function newMonthlyDebtAmortization($debtPrincipal, $debtNoOfPeriods, $de
[
$debtPrincipal,
$debtNoOfPeriods,
TimeUtils::getDaysFromMonths(1),
TimeSpan::asDuration(0, 1),
$debtInterest
]
);
Expand All @@ -60,7 +60,7 @@ public function newDailyDebtAmortization($debtPrincipal, $debtNoOfPeriods, $debt
[
$debtPrincipal,
$debtNoOfPeriods,
1,
TimeSpan::asDuration(0, 0, 1),
$debtInterest
]
);
Expand All @@ -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
]
);
Expand Down
9 changes: 9 additions & 0 deletions src/Utils/MathFuncs.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
61 changes: 40 additions & 21 deletions tests/DebtAmortizatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -55,7 +56,7 @@ public function testRepaymentsDirectYearly() {
$debtAmortizatorDirect = new DebtAmortizator(
40000,
6,
TimeUtils::getDaysFromYears(1),
TimeSpan::asDuration(1),
0.12);

$this->processResult($debtAmortizatorDirect);
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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());

Expand Down Expand Up @@ -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"]);

Expand Down

0 comments on commit da85fcc

Please sign in to comment.