33namespace com \tyme \lunar ;
44
55
6- use com \tyme \AbstractTyme ;
76use com \tyme \culture \Direction ;
87use com \tyme \culture \Duty ;
98use com \tyme \culture \Element ;
2625use com \tyme \sixtycycle \ThreePillars ;
2726use com \tyme \solar \SolarDay ;
2827use com \tyme \solar \SolarTerm ;
28+ use com \tyme \unit \DayUnit ;
2929use InvalidArgumentException ;
3030
3131/**
3434 * @author 6tail
3535 * @package com\tyme\lunar
3636 */
37- class LunarDay extends AbstractTyme
37+ class LunarDay extends DayUnit
3838{
3939 static array $ NAMES = ['初一 ' , '初二 ' , '初三 ' , '初四 ' , '初五 ' , '初六 ' , '初七 ' , '初八 ' , '初九 ' , '初十 ' , '十一 ' , '十二 ' , '十三 ' , '十四 ' , '十五 ' , '十六 ' , '十七 ' , '十八 ' , '十九 ' , '二十 ' , '廿一 ' , '廿二 ' , '廿三 ' , '廿四 ' , '廿五 ' , '廿六 ' , '廿七 ' , '廿八 ' , '廿九 ' , '三十 ' ];
4040
41- /**
42- * @var LunarMonth 农历月
43- */
44- protected LunarMonth $ month ;
45-
46- /**
47- * @var int 日
48- */
49- protected int $ day ;
50-
51- /**
52- * @var SolarDay|null 公历日(第一次使用时才会初始化)
53- */
54- protected ?SolarDay $ solarDay = null ;
55-
56- /**
57- * @var SixtyCycleDay|null 干支日(第一次使用时才会初始化)
58- */
59- protected ?SixtyCycleDay $ sixtyCycleDay = null ;
60-
6141 protected function __construct (int $ year , int $ month , int $ day )
6242 {
43+ self ::validate ($ year , $ month , $ day );
44+ parent ::__construct ($ year , $ month , $ day );
45+ }
46+
47+ static function validate (int $ year , int $ month , int $ day ): void
48+ {
49+ if ($ day < 1 ) {
50+ throw new InvalidArgumentException (sprintf ('illegal lunar day %d ' , $ day ));
51+ }
6352 $ m = LunarMonth::fromYm ($ year , $ month );
64- if ($ day < 1 || $ day > $ m ->getDayCount ()) {
53+ if ($ day > $ m ->getDayCount ()) {
6554 throw new InvalidArgumentException (sprintf ('illegal day %d in %s ' , $ day , $ m ));
6655 }
67- $ this ->month = $ m ;
68- $ this ->day = $ day ;
6956 }
7057
7158 static function fromYmd (int $ year , int $ month , int $ day ): static
@@ -80,37 +67,7 @@ static function fromYmd(int $year, int $month, int $day): static
8067 */
8168 function getLunarMonth (): LunarMonth
8269 {
83- return $ this ->month ;
84- }
85-
86- /**
87- * 年
88- *
89- * @return int 年
90- */
91- function getYear (): int
92- {
93- return $ this ->month ->getYear ();
94- }
95-
96- /**
97- * 月
98- *
99- * @return int 月,闰月为负数
100- */
101- function getMonth (): int
102- {
103- return $ this ->month ->getMonthWithLeap ();
104- }
105-
106- /**
107- * 日
108- *
109- * @return int 日
110- */
111- function getDay (): int
112- {
113- return $ this ->day ;
70+ return LunarMonth::fromYm ($ this ->year , $ this ->month );
11471 }
11572
11673 function getName (): string
@@ -120,7 +77,7 @@ function getName(): string
12077
12178 function __toString (): string
12279 {
123- return sprintf ('%s%s ' , $ this ->month , $ this ->getName ());
80+ return sprintf ('%s%s ' , $ this ->getLunarMonth () , $ this ->getName ());
12481 }
12582
12683 function next (int $ n ): LunarDay
@@ -136,17 +93,13 @@ function next(int $n): LunarDay
13693 */
13794 function isBefore (LunarDay $ target ): bool
13895 {
139- $ aYear = $ this ->getYear ();
140- $ bYear = $ target ->getYear ();
141- if ($ aYear != $ bYear ) {
142- return $ aYear < $ bYear ;
96+ if ($ this ->year != $ target ->year ) {
97+ return $ this ->year < $ target ->year ;
14398 }
144- $ aMonth = $ this ->getMonth ();
145- $ bMonth = $ target ->getMonth ();
146- if ($ aMonth != $ bMonth ) {
147- return abs ($ aMonth ) < abs ($ bMonth );
99+ if ($ this ->month != $ target ->month ) {
100+ return abs ($ this ->month ) < abs ($ target ->month );
148101 }
149- return $ this ->day < $ target ->getDay () ;
102+ return $ this ->day < $ target ->day ;
150103 }
151104
152105 /**
@@ -157,17 +110,13 @@ function isBefore(LunarDay $target): bool
157110 */
158111 function isAfter (LunarDay $ target ): bool
159112 {
160- $ aYear = $ this ->getYear ();
161- $ bYear = $ target ->getYear ();
162- if ($ aYear != $ bYear ) {
163- return $ aYear > $ bYear ;
113+ if ($ this ->year != $ target ->year ) {
114+ return $ this ->year > $ target ->year ;
164115 }
165- $ aMonth = $ this ->getMonth ();
166- $ bMonth = $ target ->getMonth ();
167- if ($ aMonth != $ bMonth ) {
168- return abs ($ aMonth ) >= abs ($ bMonth );
116+ if ($ this ->month != $ target ->month ) {
117+ return abs ($ this ->month ) >= abs ($ target ->month );
169118 }
170- return $ this ->day > $ target ->getDay () ;
119+ return $ this ->day > $ target ->day ;
171120 }
172121
173122 /**
@@ -211,7 +160,7 @@ function getMonthSixtyCycle(): SixtyCycle
211160 */
212161 function getSixtyCycle (): SixtyCycle
213162 {
214- $ offset = (int )$ this ->month ->getFirstJulianDay ()->next ($ this ->day - 12 )->getDay ();
163+ $ offset = (int )$ this ->getLunarMonth () ->getFirstJulianDay ()->next ($ this ->day - 12 )->getDay ();
215164 return SixtyCycle::fromName (sprintf ('%s%s ' , HeavenStem::fromIndex ($ offset )->getName (), EarthBranch::fromIndex ($ offset )->getName ()));
216165 }
217166
@@ -276,7 +225,7 @@ function getNineStar(): NineStar
276225 function getJupiterDirection (): Direction
277226 {
278227 $ index = $ this ->getSixtyCycle ()->getIndex ();
279- return $ index % 12 < 6 ? Element::fromIndex (intdiv ($ index , 12 ))->getDirection () : $ this ->month ->getLunarYear ()->getJupiterDirection ();
228+ return $ index % 12 < 6 ? Element::fromIndex (intdiv ($ index , 12 ))->getDirection () : $ this ->getLunarMonth () ->getLunarYear ()->getJupiterDirection ();
280229 }
281230
282231 /**
@@ -297,7 +246,7 @@ function getFetusDay(): FetusDay
297246 function getPhaseDay (): PhaseDay
298247 {
299248 $ today = $ this ->getSolarDay ();
300- $ m = $ this ->month ->next (1 );
249+ $ m = $ this ->getLunarMonth () ->next (1 );
301250 $ p = Phase::fromIndex ($ m ->getYear (), $ m ->getMonthWithLeap (), 0 );
302251 $ d = $ p ->getSolarDay ();
303252 while ($ d ->isAfter ($ today )) {
@@ -324,11 +273,7 @@ function getPhase(): Phase
324273 */
325274 function getSolarDay (): SolarDay
326275 {
327- if ($ this ->solarDay == null )
328- {
329- $ this ->solarDay = $ this ->month ->getFirstJulianDay ()->next ($ this ->day - 1 )->getSolarDay ();
330- }
331- return $ this ->solarDay ;
276+ return $ this ->getLunarMonth ()->getFirstJulianDay ()->next ($ this ->day - 1 )->getSolarDay ();
332277 }
333278
334279 /**
@@ -338,11 +283,7 @@ function getSolarDay(): SolarDay
338283 */
339284 function getSixtyCycleDay (): SixtyCycleDay
340285 {
341- if ($ this ->sixtyCycleDay == null )
342- {
343- $ this ->sixtyCycleDay = $ this ->getSolarDay ()->getSixtyCycleDay ();
344- }
345- return $ this ->sixtyCycleDay ;
286+ return $ this ->getSolarDay ()->getSixtyCycleDay ();
346287 }
347288
348289 /**
@@ -362,7 +303,7 @@ function getTwentyEightStar(): TwentyEightStar
362303 */
363304 function getFestival (): ?LunarFestival
364305 {
365- return LunarFestival::fromYmd ($ this ->getYear () , $ this ->getMonth () , $ this ->day );
306+ return LunarFestival::fromYmd ($ this ->year , $ this ->month , $ this ->day );
366307 }
367308
368309 /**
@@ -372,12 +313,10 @@ function getFestival(): ?LunarFestival
372313 */
373314 function getHours (): array
374315 {
375- $ y = $ this ->getYear ();
376- $ m = $ this ->getMonth ();
377316 $ l = array ();
378- $ l [] = LunarHour::fromYmdHms ($ y , $ m , $ this ->day , 0 , 0 , 0 );
317+ $ l [] = LunarHour::fromYmdHms ($ this -> year , $ this -> month , $ this ->day , 0 , 0 , 0 );
379318 for ($ i = 0 ; $ i < 24 ; $ i += 2 ) {
380- $ l [] = LunarHour::fromYmdHms ($ y , $ m , $ this ->day , $ i + 1 , 0 , 0 );
319+ $ l [] = LunarHour::fromYmdHms ($ this -> year , $ this -> month , $ this ->day , $ i + 1 , 0 , 0 );
381320 }
382321 return $ l ;
383322 }
@@ -422,7 +361,7 @@ function getAvoids(): array
422361 */
423362 function getSixStar (): SixStar
424363 {
425- return SixStar::fromIndex (($ this ->month ->getMonth () + $ this ->day - 2 ) % 6 );
364+ return SixStar::fromIndex (($ this ->getLunarMonth () ->getMonth () + $ this ->day - 2 ) % 6 );
426365 }
427366
428367 /**
0 commit comments