Skip to content

Commit

Permalink
v1.1.6 新增人元司令分野;修复立春比正月初一早导致大小运错误的问题。
Browse files Browse the repository at this point in the history
  • Loading branch information
6tail committed Dec 9, 2024
1 parent 105557f commit e9b86da
Show file tree
Hide file tree
Showing 15 changed files with 292 additions and 11 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,7 @@

## [1.1.5] - 2024-11-13
1. 新增:2025年法定假日。

## [1.1.6] - 2024-12-09
1. 新增:人元司令分野。
2. 修复:立春比正月初一早导致大小运错误的问题。
4 changes: 2 additions & 2 deletions src/ExtendTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ trait ExtendTrait
* @param Closure $function 方法体
* @return void
*/
public static function extend(string $methodName, Closure $function): void
static function extend(string $methodName, Closure $function): void
{
static::$methods[$methodName] = $function;
}
Expand All @@ -35,7 +35,7 @@ public static function extend(string $methodName, Closure $function): void
* @return mixed
* @throws BadMethodCallException
*/
public function __call(string $method, mixed $parameters)
function __call(string $method, mixed $parameters)
{
if (!isset(static::$methods[$method])) {
throw new BadMethodCallException(sprintf('Method %s not exist in %s', $method, static::class));
Expand Down
2 changes: 1 addition & 1 deletion src/culture/plumrain/PlumRainDay.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ function getPlumRain(): PlumRain
return $this->culture;
}

public function __toString(): string
function __toString(): string
{
return $this->getPlumRain()->getIndex() == 0 ? parent::__toString() : $this->culture->getName();
}
Expand Down
22 changes: 22 additions & 0 deletions src/eightchar/ChildLimit.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
use com\tyme\eightchar\provider\impl\DefaultChildLimitProvider;
use com\tyme\enums\Gender;
use com\tyme\enums\YinYang;
use com\tyme\lunar\LunarHour;
use com\tyme\lunar\LunarYear;
use com\tyme\solar\SolarTerm;
use com\tyme\solar\SolarTime;

/**
Expand Down Expand Up @@ -199,4 +202,23 @@ function getStartFortune(): Fortune
return Fortune::fromChildLimit($this, 0);
}

/**
* 结束农历年
*
* @return LunarYear 农历年
*/
function getEndLunarYear(): LunarYear
{
$endTime = $this->getEndTime();
$solarYear = $endTime->getYear();
$y = $endTime->getLunarHour()->getLunarDay()->getLunarMonth()->getLunarYear();
if ($y->getYear() < $solarYear) {
// 正月初一在立春之后的,农历年往后推一年
if (LunarHour::fromYmdHms($solarYear, 1, 1, 0, 0, 0)->getSolarTime()->isAfter(SolarTerm::fromIndex($solarYear, 3)->getJulianDay()->getSolarTime())) {
$y = $y->next(1);
}
}
return $y;
}

}
2 changes: 1 addition & 1 deletion src/eightchar/ChildLimitInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class ChildLimitInfo
* @param int $hourCount 小时数
* @param int $minuteCount 分钟数
*/
public function __construct(SolarTime $startTime, SolarTime $endTime, int $yearCount, int $monthCount, int $dayCount, int $hourCount, int $minuteCount)
function __construct(SolarTime $startTime, SolarTime $endTime, int $yearCount, int $monthCount, int $dayCount, int $hourCount, int $minuteCount)
{
$this->startTime = $startTime;
$this->endTime = $endTime;
Expand Down
4 changes: 2 additions & 2 deletions src/eightchar/DecadeFortune.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ static function fromChildLimit(ChildLimit $childLimit, int $index): static
*/
function getStartAge(): int
{
return $this->childLimit->getYearCount() + 1 + $this->index * 10;
return $this->childLimit->getEndTime()->getYear() - $this->childLimit->getStartTime()->getYear() + 1 + $this->index * 10;
}

/**
Expand All @@ -62,7 +62,7 @@ function getEndAge(): int
*/
function getStartLunarYear(): LunarYear
{
return $this->childLimit->getEndTime()->getLunarHour()->getLunarDay()->getLunarMonth()->getLunarYear()->next($this->index * 10);
return $this->childLimit->getEndLunarYear()->next($this->index * 10);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/eightchar/Fortune.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ static function fromChildLimit(ChildLimit $childLimit, int $index): static
*/
function getAge(): int
{
return $this->childLimit->getYearCount() + 1 + $this->index;
return $this->childLimit->getEndTime()->getYear() - $this->childLimit->getStartTime()->getYear() + 1 + $this->index;
}

/**
Expand All @@ -52,7 +52,7 @@ function getAge(): int
*/
function getLunarYear(): LunarYear
{
return $this->childLimit->getEndTime()->getLunarHour()->getLunarDay()->getLunarMonth()->getLunarYear()->next($this->index);
return $this->childLimit->getEndLunarYear()->next($this->index);
}

/**
Expand Down
55 changes: 55 additions & 0 deletions src/enums/HideHeavenStemType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

namespace com\tyme\enums;

/**
* 藏干类型
* @author 6tail
* @package com\tyme\enums
*/
enum HideHeavenStemType: int
{
case RESIDUAL = 0;
case MIDDLE = 1;
case MAIN = 2;

function getCode(): int
{
return $this->value;
}

function getName(): string
{
return match ($this) {
self::RESIDUAL => '余气',
self::MIDDLE => '中气',
self::MAIN => '本气'
};
}

static function fromCode(int $code): HideHeavenStemType
{
return match ($code) {
0 => self::RESIDUAL,
1 => self::MIDDLE,
2 => self::MAIN,
default => null
};
}

static function fromName(string $name): HideHeavenStemType
{
return match ($name) {
'余气' => self::RESIDUAL,
'中气' => self::MIDDLE,
'本气' => self::MAIN,
default => null
};
}

function equals(HideHeavenStemType $o): bool
{
return $this->value == $o->value;
}

}
23 changes: 22 additions & 1 deletion src/sixtycycle/EarthBranch.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@
use com\tyme\culture\Element;
use com\tyme\culture\pengzu\PengZuEarthBranch;
use com\tyme\culture\Zodiac;
use com\tyme\enums\HideHeavenStemType;
use com\tyme\enums\YinYang;
use com\tyme\LoopTyme;

/**
* 地支
* 地支(地元)
* @author 6tail
* @package com\tyme\sixtycycle
*/
Expand Down Expand Up @@ -95,6 +96,26 @@ function getHideHeavenStemResidual(): ?HeavenStem
return $n == -1 ? null : HeavenStem::fromIndex($n);
}

/**
* 藏干列表
*
* @return HideHeavenStem[] 藏干列表
*/
function getHideHeavenStems(): array
{
$l = array();
$l[] = new HideHeavenStem($this->getHideHeavenStemMain(), HideHeavenStemType::MAIN);
$o = $this->getHideHeavenStemMiddle();
if (null != $o) {
$l[] = new HideHeavenStem($o, HideHeavenStemType::MIDDLE);
}
$o = $this->getHideHeavenStemResidual();
if (null != $o) {
$l[] = new HideHeavenStem($o, HideHeavenStemType::RESIDUAL);
}
return $l;
}

/**
* 生肖
*
Expand Down
2 changes: 1 addition & 1 deletion src/sixtycycle/HeavenStem.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
use com\tyme\LoopTyme;

/**
* 天干
* 天干(天元)
* @author 6tail
* @package com\tyme\sixtycycle
*/
Expand Down
63 changes: 63 additions & 0 deletions src/sixtycycle/HideHeavenStem.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

namespace com\tyme\sixtycycle;


use com\tyme\AbstractCulture;
use com\tyme\enums\HideHeavenStemType;

/**
* 藏干(即人元,司令取天干,分野取天干的五行)
* @author 6tail
* @package com\tyme\sixtycycle
*/
class HideHeavenStem extends AbstractCulture
{

/**
* @var HeavenStem 天干
*/
protected HeavenStem $heavenStem;

/**
* @var HideHeavenStemType 藏干类型
*/
protected HideHeavenStemType $type;

function __construct(HeavenStem|string|int $heavenStem, HideHeavenStemType $type)
{
if (is_string($heavenStem)) {
$this->heavenStem = HeavenStem::fromName($heavenStem);
} elseif (is_int($heavenStem)) {
$this->heavenStem = HeavenStem::fromIndex($heavenStem);
} else {
$this->heavenStem = $heavenStem;
}
$this->type = $type;
}

/**
* 天干
*
* @return HeavenStem 天干
*/
function getHeavenStem(): HeavenStem
{
return $this->heavenStem;
}

/**
* 藏干类型
*
* @return HideHeavenStemType 藏干类型
*/
function getType(): HideHeavenStemType
{
return $this->type;
}

function getName(): string
{
return $this->heavenStem->getName();
}
}
40 changes: 40 additions & 0 deletions src/sixtycycle/HideHeavenStemDay.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace com\tyme\sixtycycle;


use com\tyme\AbstractCultureDay;

/**
* 人元司令分野(地支藏干+天索引)
* @author 6tail
* @package com\tyme\sixycycle
*/
class HideHeavenStemDay extends AbstractCultureDay
{
function __construct(HideHeavenStem $hideHeavenStem, int $dayIndex)
{
parent::__construct($hideHeavenStem, $dayIndex);
}

/**
* 藏干
*
* @return HideHeavenStem 藏干
*/
function getHideHeavenStem(): HideHeavenStem
{
return $this->culture;
}

function getName(): string
{
$heavenStem = $this->getHideHeavenStem()->getHeavenStem();
return $heavenStem->getName() . $heavenStem->getElement()->getName();
}

function __toString(): string
{
return sprintf('%s第%d天', $this->getName(), $this->getDayIndex() + 1);
}
}
39 changes: 39 additions & 0 deletions src/solar/SolarDay.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,14 @@
use com\tyme\culture\plumrain\PlumRain;
use com\tyme\culture\plumrain\PlumRainDay;
use com\tyme\culture\Week;
use com\tyme\enums\HideHeavenStemType;
use com\tyme\festival\SolarFestival;
use com\tyme\holiday\LegalHoliday;
use com\tyme\jd\JulianDay;
use com\tyme\lunar\LunarDay;
use com\tyme\lunar\LunarMonth;
use com\tyme\sixtycycle\HideHeavenStem;
use com\tyme\sixtycycle\HideHeavenStemDay;
use InvalidArgumentException;

/**
Expand Down Expand Up @@ -328,6 +331,42 @@ function getNineDay(): ?NineDay
return new NineDay(Nine::fromIndex(intdiv($days, 9)), $days % 9);
}

/**
* 人元司令分野
*
* @return HideHeavenStemDay 人元司令分野
*/
function getHideHeavenStemDay(): HideHeavenStemDay
{
$dayCounts = [3, 5, 7, 9, 10, 30];
$term = $this->getTerm();
if ($term->isQi()) {
$term = $term->next(-1);
}
$dayIndex = $this->subtract($term->getJulianDay()->getSolarDay());
$startIndex = ($term->getIndex() - 1) * 3;
$data = substr('93705542220504xx1513904541632524533533105544806564xx7573304542018584xx95', $startIndex, 6);
$days = 0;
$heavenStemIndex = 0;
$typeIndex = 0;
while ($typeIndex < 3) {
$i = $typeIndex * 2;
$d = substr($data, $i, 1);
$count = 0;
if ($d != 'x') {
$heavenStemIndex = intval($d);
$count = $dayCounts[intval(substr($data, $i + 1, 1))];
$days += $count;
}
if ($dayIndex <= $days) {
$dayIndex -= $days - $count;
break;
}
$typeIndex++;
}
return new HideHeavenStemDay(new HideHeavenStem($heavenStemIndex, HideHeavenStemType::fromCode($typeIndex)), $dayIndex);
}

/**
* 梅雨天(芒种后的第1个丙日入梅,小暑后的第1个未日出梅)
* @return PlumRainDay|null 梅雨天
Expand Down
2 changes: 1 addition & 1 deletion test/EightCharTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,7 @@ function test16()
// 小运
$fortune = $childLimit->getStartFortune();
// 年龄
$this->assertEquals(7, $fortune->getAge());
$this->assertEquals(8, $fortune->getAge());
}

function test17()
Expand Down
Loading

0 comments on commit e9b86da

Please sign in to comment.