33namespace com \tyme \eightchar ;
44
55
6+ use com \tyme \eightchar \provider \ChildLimitProvider ;
7+ use com \tyme \eightchar \provider \impl \DefaultChildLimitProvider ;
68use com \tyme \enums \Gender ;
79use com \tyme \enums \YinYang ;
8- use com \tyme \solar \SolarMonth ;
910use com \tyme \solar \SolarTime ;
1011
1112/**
1617class ChildLimit
1718{
1819 /**
19- * @var SolarTime 开始(即出生)的公历时刻
20+ * @var ChildLimitProvider|null 童限计算接口
2021 */
21- protected SolarTime $ startTime ;
22-
23- /**
24- * @var SolarTime 结束(即开始起运)的公历时刻
25- */
26- protected SolarTime $ endTime ;
27-
22+ static ?ChildLimitProvider $ provider = null ;
2823 /**
2924 * @var EightChar 八字
3025 */
@@ -36,93 +31,39 @@ class ChildLimit
3631 protected Gender $ gender ;
3732
3833 /**
39- * @var int 年数
40- */
41- protected int $ yearCount ;
42-
43- /**
44- * @var int 月数
45- */
46- protected int $ monthCount ;
47-
48- /**
49- * @var int 日数
50- */
51- protected int $ dayCount ;
52-
53- /**
54- * @var int 小时数
34+ * @var bool 顺逆
5535 */
56- protected int $ hourCount ;
36+ protected bool $ forward ;
5737
5838 /**
59- * @var int 分钟数
39+ * @var ChildLimitInfo 童限信息
6040 */
61- protected int $ minuteCount ;
41+ protected ChildLimitInfo $ info ;
6242
63- /**
64- * @var bool 顺逆
65- */
66- protected bool $ forward ;
43+ private static function init (): void
44+ {
45+ self :: $ provider = new DefaultChildLimitProvider ();
46+ }
6747
6848 protected function __construct (SolarTime $ birthTime , Gender $ gender )
6949 {
70- $ this ->startTime = $ birthTime ;
50+ if (null == self ::$ provider ) {
51+ self ::init ();
52+ }
7153 $ this ->gender = $ gender ;
7254 $ this ->eightChar = $ birthTime ->getLunarHour ()->getEightChar ();
7355 // 阳男阴女顺推,阴男阳女逆推
7456 $ yang = YinYang::YANG == $ this ->eightChar ->getYear ()->getHeavenStem ()->getYinYang ();
7557 $ man = Gender::MAN == $ gender ;
76- $ forward = ($ yang && $ man ) || (!$ yang && !$ man );
58+ $ this -> forward = ($ yang && $ man ) || (!$ yang && !$ man );
7759 $ term = $ birthTime ->getTerm ();
7860 if (!$ term ->isJie ()) {
7961 $ term = $ term ->next (-1 );
8062 }
81- $ start = $ forward ? $ birthTime : $ term ->getJulianDay ()->getSolarTime ();
82- $ end = $ forward ? $ term ->next (2 )->getJulianDay ()->getSolarTime () : $ birthTime ;
83-
84- $ seconds = $ end ->subtract ($ start );
85- // 3天 = 1年,3天=60*60*24*3秒=259200秒 = 1年
86- $ year = intdiv ($ seconds , 259200 );
87- $ seconds %= 259200 ;
88- // 1天 = 4月,1天=60*60*24秒=86400秒 = 4月,85400秒/4=21600秒 = 1月
89- $ month = intdiv ($ seconds , 21600 );
90- $ seconds %= 21600 ;
91- // 1时 = 5天,1时=60*60秒=3600秒 = 5天,3600秒/5=720秒 = 1天
92- $ day = intdiv ($ seconds , 720 );
93- $ seconds %= 720 ;
94- // 1分 = 2时,60秒 = 2时,60秒/2=30秒 = 1时
95- $ hour = intdiv ($ seconds , 30 );
96- $ seconds %= 30 ;
97- // 1秒 = 2分,1秒/2=0.5秒 = 1分
98- $ minute = $ seconds * 2 ;
99-
100- $ this ->forward = $ forward ;
101- $ this ->yearCount = $ year ;
102- $ this ->monthCount = $ month ;
103- $ this ->dayCount = $ day ;
104- $ this ->hourCount = $ hour ;
105- $ this ->minuteCount = $ minute ;
106-
107- $ birthday = $ birthTime ->getDay ();
108- $ birthMonth = $ birthday ->getMonth ();
109-
110- $ d = $ birthday ->getDay () + $ day ;
111- $ h = $ birthTime ->getHour () + $ hour ;
112- $ mi = $ birthTime ->getMinute () + $ minute ;
113- $ h += intdiv ($ mi , 60 );
114- $ mi %= 60 ;
115- $ d += intdiv ($ h , 24 );
116- $ h %= 24 ;
117-
118- $ sm = SolarMonth::fromYm ($ birthMonth ->getYear ()->getYear () + $ year , $ birthMonth ->getMonth ())->next ($ month );
119-
120- $ dc = $ sm ->getDayCount ();
121- if ($ d > $ dc ) {
122- $ d -= $ dc ;
123- $ sm = $ sm ->next (1 );
63+ if ($ this ->forward ) {
64+ $ term = $ term ->next (2 );
12465 }
125- $ this ->endTime = SolarTime:: fromYmdHms ( $ sm -> getYear ()-> getYear (), $ sm -> getMonth () , $ d , $ h , $ mi , $ birthTime -> getSecond () );
66+ $ this ->info = self :: $ provider -> getInfo ( $ birthTime , $ term );
12667 }
12768
12869 /**
@@ -174,7 +115,7 @@ function isForward(): bool
174115 */
175116 function getYearCount (): int
176117 {
177- return $ this ->yearCount ;
118+ return $ this ->info -> getYearCount () ;
178119 }
179120
180121 /**
@@ -184,7 +125,7 @@ function getYearCount(): int
184125 */
185126 function getMonthCount (): int
186127 {
187- return $ this ->monthCount ;
128+ return $ this ->info -> getMonthCount () ;
188129 }
189130
190131 /**
@@ -194,7 +135,7 @@ function getMonthCount(): int
194135 */
195136 function getDayCount (): int
196137 {
197- return $ this ->dayCount ;
138+ return $ this ->info -> getDayCount () ;
198139 }
199140
200141 /**
@@ -204,7 +145,7 @@ function getDayCount(): int
204145 */
205146 function getHourCount (): int
206147 {
207- return $ this ->hourCount ;
148+ return $ this ->info -> getHourCount () ;
208149 }
209150
210151 /**
@@ -214,7 +155,7 @@ function getHourCount(): int
214155 */
215156 function getMinuteCount (): int
216157 {
217- return $ this ->minuteCount ;
158+ return $ this ->info -> getMinuteCount () ;
218159 }
219160
220161 /**
@@ -224,7 +165,7 @@ function getMinuteCount(): int
224165 */
225166 function getStartTime (): SolarTime
226167 {
227- return $ this ->startTime ;
168+ return $ this ->info -> getStartTime () ;
228169 }
229170
230171 /**
@@ -234,7 +175,7 @@ function getStartTime(): SolarTime
234175 */
235176 function getEndTime (): SolarTime
236177 {
237- return $ this ->endTime ;
178+ return $ this ->info -> getEndTime () ;
238179 }
239180
240181 /**
0 commit comments