1<?php
2
3namespace PhpOffice\PhpSpreadsheet\Calculation\Financial\CashFlow\Constant\Periodic;
4
5use PhpOffice\PhpSpreadsheet\Calculation\Financial\Constants as FinancialConstants;
6
7class InterestAndPrincipal
8{
9    protected $interest;
10
11    protected $principal;
12
13    public function __construct(
14        float $rate = 0.0,
15        int $period = 0,
16        int $numberOfPeriods = 0,
17        float $presentValue = 0,
18        float $futureValue = 0,
19        int $type = FinancialConstants::PAYMENT_END_OF_PERIOD
20    ) {
21        $payment = Payments::annuity($rate, $numberOfPeriods, $presentValue, $futureValue, $type);
22        $capital = $presentValue;
23        $interest = 0.0;
24        $principal = 0.0;
25        for ($i = 1; $i <= $period; ++$i) {
26            $interest = ($type === FinancialConstants::PAYMENT_BEGINNING_OF_PERIOD && $i == 1) ? 0 : -$capital * $rate;
27            $principal = $payment - $interest;
28            $capital += $principal;
29        }
30
31        $this->interest = $interest;
32        $this->principal = $principal;
33    }
34
35    public function interest(): float
36    {
37        return $this->interest;
38    }
39
40    public function principal(): float
41    {
42        return $this->principal;
43    }
44}
45