Skip to content

Commit

Permalink
– getResultAsArray method's implementation moved to the CalculatorAbs…
Browse files Browse the repository at this point in the history
…tract abstract class
  • Loading branch information
uruba committed Aug 26, 2015
1 parent 02ec3d0 commit 1c42121
Show file tree
Hide file tree
Showing 9 changed files with 195 additions and 150 deletions.
31 changes: 19 additions & 12 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -299,16 +299,20 @@ namespace `FinanCalc\Calculators`
* **getAnnuityPeriodLengthInDays()** – gets the length of each compounding periods in days
* **getAnnuityInterest()** – gets i
* **getAnnuityPresentValue(AnnuityPaymentTypes $annuityType)** – gets the present value of the annuity
* *AnuityPaymentTypes $annuityType* = determines whether the payments are made either at the beginning or the end of each of the annuity's periods
[*optional for perpetuities*]
* *AnuityPaymentTypes $annuityType* = determines whether the payments are made either at the beginning or the end of each of the annuity's periods
[*optional for perpetuities*]
* **getAnnuityPresentValueInAdvance()** – shorthand for *getAnnuityPresentValue(new AnnuityPaymentTypes(AnnuityPaymentTypes::IN_ADVANCE))*
* **getAnnuityPresentValueInArrears()** – shorthand for *getAnnuityPresentValue(new AnnuityPaymentTypes(AnnuityPaymentTypes::IN_ARREARS))*
* **getAnnuityFutureValue(AnnuityPaymentTypes $annuityType)** – gets the future value of the annuity
* *AnuityPaymentTypes $annuityType* = determines whether the payments are made either at the beginning or the end of each of the annuity's periods
[*optional for perpetuities*]
* *AnuityPaymentTypes $annuityType* = determines whether the payments are made either at the beginning or the end of each of the annuity's periods
[*optional for perpetuities*]
* **getAnnuityFutureValueInAdvance()** – shorthand for *getAnnuityFutureValue(new AnnuityPaymentTypes(AnnuityPaymentTypes::IN_ADVANCE))*
* **getAnnuityFutureValueInArrears()** – shorthand for *getAnnuityFutureValue(new AnnuityPaymentTypes(AnnuityPaymentTypes::IN_ARREARS))*
* **getAnnuityValue(AnnuityPaymentTypes $annuityPaymentType, AnnuityValueTypes $annuityValueType)** – gets either the present or the future value of the annuity
* *AnuityPaymentTypes $annuityPaymentType* = determines whether the payments are made either at the beginning or the end of each of the annuity's periods
[*optional for perpetuities*]
* *AnuityValueTypes $annuityValueType* = determines whether the result is the present or the future value of the annuity
* **getResultAsArray()** – gets the array of the pertinent ***AnnuityInstance***'s property values
* *AnuityPaymentTypes $annuityPaymentType* = determines whether the payments are made either at the beginning or the end of each of the annuity's periods
[*optional for perpetuities*]
* *AnuityValueTypes $annuityValueType* = determines whether the result is the present or the future value of the annuity
* **getResultAsArray(array $propResultArray = null)** – gets the array of the pertinent property values which you can specify (e.g., if you want only a specified subset thereof) via the optional argument
* **getSerializedResult(SerializerInterface $serializer)** – gets the serialized result, according to the passed SerializerInterface object

#### AnnuityCalculatorFactory (*AnnuityCalculator's factory object*)
Expand Down Expand Up @@ -357,7 +361,8 @@ namespace `FinanCalc\Calculators`
* **getDebtDurationInDays()** – gets the duration of the debt in days
* **getDebtInterest()** – gets i
* **getDebtRepayments()** – gets the **array of RepaymentInstance** objects representing all the individual payments within the debt comprised into an array
* **getResultAsArray()** – gets the array of the pertinent ***DebtInstance***'s property values
* **getDebtRepaymentsAsArrays()** – gets the **array of associative arrays** (i.e., an array of RepaymentInstances converted to arrays) representing all the individual payments within the debt
* **getResultAsArray(array $propResultArray = null)** – gets the array of the pertinent property values which you can specify (e.g., if you want only a specified subset thereof) via the optional argument
* **getSerializedResult(SerializerInterface $serializer)** – gets the serialized result, according to the passed SerializerInterface object

#### DebtAmortizatorFactory (*DebtAmortizator's factory object*)
Expand Down Expand Up @@ -399,7 +404,7 @@ namespace `FinanCalc\Calculators`
* **getBondPaymentFrequency()** – gets the frequency of bond payments
* **getBondNoOfPayments()** – gets the total number of payments during the lifespan of the bond
* **getBondFairValue()** – gets the fair value of the bond [calculated as present value of future cashflows corresponding to the bond by means of the valuation interest rate]
* **getResultAsArray()** – gets the array of the pertinent ***BondInstance***'s property values
* **getResultAsArray(array $propResultArray = null)** – gets the array of the pertinent property values which you can specify (e.g., if you want only a specified subset thereof) via the optional argument
* **getSerializedResult(SerializerInterface $serializer)** – gets the serialized result, according to the passed SerializerInterface object

#### BondFairValueCalculatorFactory (*BondFairValueCalculator's factory object*)
Expand Down Expand Up @@ -436,7 +441,7 @@ namespace `FinanCalc\Calculators`
* **getBondPaymentFrequency()** – gets the frequency of bond payments
* **getBondNoOfPayments()** – gets the total number of payments during the lifespan of the bond
* **getApproxBondYTM()** – gets the approximate value of the bond's yield to maturity in the form of a decimal number [it is the internal rate of return of the bond]
* **getResultAsArray()** – gets the array of the pertinent ***BondInstance***'s property values
* **getResultAsArray(array $propResultArray = null)** – gets the array of the pertinent property values which you can specify (e.g., if you want only a specified subset thereof) via the optional argument
* **getSerializedResult(SerializerInterface $serializer)** – gets the serialized result, according to the passed SerializerInterface object

#### BondYTMCalculatorFactory (*BondYTMCalculator's factory object*)
Expand Down Expand Up @@ -477,7 +482,7 @@ namespace `FinanCalc\Calculators`
* **getBondDiscountedCashFlows()** – gets an array of the bond's discounted cash flows (nominal cash flows which are discounted by the means of the bond's yield per period)
* **getBondPresentValue()** – gets the present value of the bond which is represented by sum of all the bond's discounted cash flows (i.e., all the array members returned by the method getBondDiscountedCashFlows() are summed up)
* **getBondDuration()** – gets the bond's duration in years (can be a decimal number)
* **getResultAsArray()** – gets the array of the pertinent ***BondInstance***'s property values
* **getResultAsArray(array $propResultArray = null)** – gets the array of the pertinent property values which you can specify (e.g., if you want only a specified subset thereof) via the optional argument
* **getSerializedResult(SerializerInterface $serializer)** – gets the serialized result, according to the passed SerializerInterface object

#### BondDurationCalculatorFactory (*BondDurationCalculator's factory object*)
Expand Down Expand Up @@ -511,6 +516,8 @@ namespace `FinanCalc\Calculators`
* **getPayoutRatio()** – gets the payout ratio (also referred to as "dividend payout ratio")
* **getDividendRatio()** – gets the dividend ratio (the payout ratio to the power of -1)
* **getRetentionRatio()** – gets the retention ("plowback") ratio
* **getResultAsArray(array $propResultArray = null)** – gets the array of the pertinent property values which you can specify (e.g., if you want only a specified subset thereof) via the optional argument
* **getSerializedResult(SerializerInterface $serializer)** – gets the serialized result, according to the passed SerializerInterface object

#### StockInvestmentRatiosCalculatorFactory (*StockInvestmentRatiosCalculator's factory object*)
namespace `FinanCalc\Calculators\Factories`
Expand Down
2 changes: 1 addition & 1 deletion src/FinanCalc.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
* Module php-bcmath
*
* @author Václav Uruba
* @version 0.3.0
* @version 0.3
* @license http://unlicense.org The Unlicense
*/
namespace FinanCalc {
Expand Down
98 changes: 61 additions & 37 deletions src/calculators/AnnuityCalculator.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,28 @@ class AnnuityCalculator extends CalculatorAbstract {
// the interest rate by which the unpaid balance is multiplied (i.e., a decimal number) = 'i'
private $annuityInterest;

// props returned by the getResultAsArray method by default
protected $propResultArray = [
"annuitySinglePaymentAmount",
"annuityNoOfCompoundingPeriods",
"annuityInterest",
"annuityPeriodLength" =>
[
"years" => "annuityPeriodLengthInYears",
"months" => "annuityPeriodLengthInMonths",
"days" => "annuityPeriodLengthInDays"
],
"annuityPresentValue" =>
[
"in_advance" => "annuityPresentValueInAdvance",
"in_arrears" => "annuityPresentValueInArrears"
],
"annuityFutureValue" =>
[
"in_advance" => "annuityFutureValueInAdvance",
"in_arrears" => "annuityFutureValueInArrears"
],
];

/**
* @param $annuitySinglePaymentAmount
Expand Down Expand Up @@ -151,6 +173,26 @@ public function getAnnuityPresentValue(AnnuityPaymentTypes $annuityType = null)
);
}

/**
* @return null|string
*/
public function getAnnuityPresentValueInAdvance() {
return $this
->getAnnuityPresentValue(
new AnnuityPaymentTypes(AnnuityPaymentTypes::IN_ADVANCE)
);
}

/**
* @return null|string
*/
public function getAnnuityPresentValueInArrears() {
return $this
->getAnnuityPresentValue(
new AnnuityPaymentTypes(AnnuityPaymentTypes::IN_ARREARS)
);
}

/**
* @param AnnuityPaymentTypes $annuityType
* @return null|string
Expand All @@ -163,6 +205,25 @@ public function getAnnuityFutureValue(AnnuityPaymentTypes $annuityType = null) {
);
}

/**
* @return null|string
*/
public function getAnnuityFutureValueInAdvance() {
return $this
->getAnnuityFutureValue(
new AnnuityPaymentTypes(AnnuityPaymentTypes::IN_ADVANCE)
);
}

/**
* @return null|string
*/
public function getAnnuityFutureValueInArrears() {
return $this->getAnnuityFutureValue(
new AnnuityPaymentTypes(AnnuityPaymentTypes::IN_ARREARS)
);
}

/**
* @param AnnuityPaymentTypes $annuityPaymentType
* @param AnnuityValueTypes $annuityValueType
Expand Down Expand Up @@ -250,42 +311,5 @@ public function getAnnuityValue(AnnuityPaymentTypes $annuityPaymentType = null,

return null;
}

/**
* @return array
*/
public function getResultAsArray()
{
return
[
"annuitySinglePaymentAmount" => $this->getAnnuitySinglePaymentAmount(),
"annuityNoOfCompoundingPeriods" => $this->getAnnuityNoOfCompoundingPeriods(),
"annuityInterest" => $this->getAnnuityInterest(),
"annuityPeriodLength" =>
[
"years" => $this->getAnnuityPeriodLengthInYears(),
"months" => $this->getAnnuityPeriodLengthInMonths(),
"days" => $this->getAnnuityPeriodLengthInDays()
],
"annuityPresentValue" =>
[
"in_advance" => $this->getAnnuityPresentValue(
new AnnuityPaymentTypes(AnnuityPaymentTypes::IN_ADVANCE)
),
"in_arrears" => $this->getAnnuityPresentValue(
new AnnuityPaymentTypes(AnnuityPaymentTypes::IN_ARREARS)
)
],
"annuityFutureValue" =>
[
"in_advance" => $this->getAnnuityFutureValue(
new AnnuityPaymentTypes(AnnuityPaymentTypes::IN_ADVANCE)
),
"in_arrears" => $this->getAnnuityFutureValue(
new AnnuityPaymentTypes(AnnuityPaymentTypes::IN_ARREARS)
)
],
];
}
}
}
34 changes: 14 additions & 20 deletions src/calculators/BondDurationCalculator.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,20 @@ class BondDurationCalculator extends BondCalculatorAbstract {
// e.g.: divisor 2 means semi-annual payments
// $bondPaymentFrequency;

// props returned by the getResultAsArray method by default
protected $propResultArray = [
"bondFaceValue",
"bondAnnualCouponRate",
"bondAnnualYield",
"bondYearsToMaturity",
"bondPaymentFrequency",
"bondYieldPerPaymentPeriod",
"bondNominalCashFlows",
"bondDiscountedCashFlows",
"bondPresentValue",
"bondDuration"
];

/**
* @param $bondFaceValue
* @param $bondAnnualCouponRate
Expand Down Expand Up @@ -148,25 +162,5 @@ public function getBondDuration() {

return $duration;
}

/**
* @return array
*/
public function getResultAsArray()
{
return
[
"bondFaceValue" => $this->getBondFaceValue(),
"bondAnnualCouponRate" => $this->getBondAnnualCouponRate(),
"bondAnnualYield" => $this->getBondAnnualYield(),
"bondYearsToMaturity" => $this->getBondYearsToMaturity(),
"bondPaymentFrequency" => $this->getBondPaymentFrequency(),
"bondYieldPerPaymentPeriod" => $this->getBondYieldPerPaymentPeriod(),
"bondNominalCashFlows" => $this->getBondNominalCashFlows(),
"bondDiscountedCashFlows" => $this->getBondDiscountedCashFlows(),
"bondPresentValue" => $this->getBondPresentValue(),
"bondDuration" => $this->getBondDuration()
];
}
}
}
24 changes: 9 additions & 15 deletions src/calculators/BondFairValueCalculator.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,15 @@ class BondFairValueCalculator extends BondCalculatorAbstract {
// e.g.: divisor 2 means semi-annual payments
// $bondPaymentFrequency;

// props returned by the getResultAsArray method by default
protected $propResultArray = [
"bondFaceValue",
"bondAnnualCouponRate",
"bondVIR",
"bondYearsToMaturity",
"bondFairValue"
];

/**
* @param $bondFaceValue
* @param $bondAnnualCouponRate
Expand Down Expand Up @@ -128,20 +137,5 @@ public function getBondFairValue() {

return $fairValue;
}

/**
* @return array
*/
public function getResultAsArray()
{
return
[
"bondFaceValue" => $this->getBondFaceValue(),
"bondAnnualCouponRate" => $this->getBondAnnualCouponRate(),
"bondVIR" => $this->getBondVIR(),
"bondYearsToMaturity" => $this->getBondYearsToMaturity(),
"bondFairValue" => $this->getBondFairValue()
];
}
}
}
26 changes: 10 additions & 16 deletions src/calculators/BondYTMCalculator.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,16 @@ class BondYTMCalculator extends BondCalculatorAbstract {
// e.g.: divisor 2 means semi-annual payments
// $bondPaymentFrequency;

// props returned by the getResultAsArray method by default
protected $propResultArray = [
"bondFaceValue",
"bondMarketValue",
"bondAnnualCouponRate",
"bondYearsToMaturity",
"bondPaymentFrequency",
"bondApproxYTM" => "approxBondYTM"
];

/**
* @param $bondFaceValue
* @param $bondMarketValue
Expand Down Expand Up @@ -101,21 +111,5 @@ public function getApproxBondYTM() {
}

// TODO – add a method for precise bond YTM calculation by means of a polynominal equation

/**
* @return array
*/
public function getResultAsArray()
{
return
[
"bondFaceValue" => $this->getBondFaceValue(),
"bondMarketValue" => $this->getBondMarketValue(),
"bondAnnualCouponRate" => $this->getBondAnnualCouponRate(),
"bondYearsToMaturity" => $this->getBondYearsToMaturity(),
"bondPaymentFrequency" => $this->getBondPaymentFrequency(),
"bondApproxYTM" => $this->getApproxBondYTM()
];
}
}
}
Loading

0 comments on commit 1c42121

Please sign in to comment.