1+ <?php
2+ namespace Zwei \LoanCalculator ;
3+
4+ use Zwei \LoanCalc \Exception \ParamsException ;
5+ use Zwei \LoanCalculator \Calculator \EqualTotalPaymentCalculator ;
6+ use Zwei \LoanCalculator \Calculator \EqualPrincipalPaymentCalculator ;
7+ use Zwei \LoanCalculator \Calculator \MonthlyInterestPaymentCalculator ;
8+ use Zwei \LoanCalculator \Calculator \OncePayPrincipalInterestPaymentCalculator ;
9+
10+ /**
11+ * 还款计算器工厂类
12+ * Class PaymentCalculatorFactory
13+ * @package Zwei\LoanCalculator
14+ */
15+ class PaymentCalculatorFactory
16+ {
17+ /**
18+ * 等额本金计算器
19+ */
20+ const TYPE_EQUAL_PRINCIPAL = 1 ;
21+
22+ /**
23+ * 等额本息计算器
24+ */
25+ const TYPE_EQUAL_TOTAL_PAYMENT = 2 ;
26+
27+ /**
28+ * 每月还息到期还本还款方式计算器
29+ */
30+ const TYPE_MONTHLY_INTEREST = 3 ;
31+
32+ /**
33+ * 一次性还本付息还款方式计算器
34+ */
35+ const TYPE_ONCE_PAY_PRINCIPAL_INTEREST = 4 ;
36+
37+ /**
38+ * 获取计算器对象
39+ *
40+ * @param integer $type 类型
41+ * @param float $principal 本金
42+ * @param float $yearInterestRate 年利率
43+ * @param int $months 月数
44+ * @param int $time 借款时间
45+ * @param int $decimalDigits 保留几位小数(默认2)
46+ * @return PaymentCalculatorAbstract
47+ * @throws ParamsException
48+ */
49+ public static function getPaymentCalculatorObj ($ type , $ principal , $ yearInterestRate , $ months , $ time , $ decimalDigits = 2 )
50+ {
51+ switch ($ type ) {
52+ case self ::TYPE_EQUAL_PRINCIPAL :
53+ // 等额本金计算器
54+ $ obj = new EqualPrincipalPaymentCalculator ($ principal , $ yearInterestRate , $ months , $ time , $ decimalDigits );
55+ break ;
56+ case self ::TYPE_EQUAL_TOTAL_PAYMENT :
57+ $ obj = new EqualTotalPaymentCalculator ($ principal , $ yearInterestRate , $ months , $ time , $ decimalDigits );
58+ break ;
59+ case self ::TYPE_MONTHLY_INTEREST :
60+ $ obj = new MonthlyInterestPaymentCalculator ($ principal , $ yearInterestRate , $ months , $ time , $ decimalDigits );
61+ break ;
62+ case self ::TYPE_ONCE_PAY_PRINCIPAL_INTEREST :
63+ $ obj = new OncePayPrincipalInterestPaymentCalculator ($ principal , $ yearInterestRate , $ months , $ time , $ decimalDigits );
64+ break ;
65+ default :
66+ throw new ParamsException ('参数非法 ' );
67+ break ;
68+ }
69+ return $ obj ;
70+ }
71+ }
0 commit comments