-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathSexagenary.php
117 lines (106 loc) · 3.59 KB
/
Sexagenary.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
<?php
namespace LucNham\LunarCalendar;
use Exception;
use LucNham\LunarCalendar\Contracts\LunarDateTime;
use LucNham\LunarCalendar\Contracts\SexagenaryFormattable;
use LucNham\LunarCalendar\Converters\LunarGuaranteedToSexagenary;
use LucNham\LunarCalendar\Formatters\SexagenaryDefaultFormatter;
use LucNham\LunarCalendar\Resolvers\BranchTermResolver;
use LucNham\LunarCalendar\Resolvers\StemTermResolver;
use LucNham\LunarCalendar\Terms\BranchIdentifier;
use LucNham\LunarCalendar\Terms\SexagenaryIdentifier;
use LucNham\LunarCalendar\Terms\SexagenaryMilestone;
use LucNham\LunarCalendar\Terms\StemIdentifier;
/**
* Lunar Sexagenary system
*
* @property StemIdentifier $D Stem of day
* @property StemIdentifier $M Stem of month
* @property StemIdentifier $Y Stem of year
* @property StemIdentifier $H Stem of hour
* @property StemIdentifier $N Stem of fisrt hour of day
* @property StemIdentifier $W Stem of week
* @property BranchIdentifier $d Branch of day
* @property BranchIdentifier $m Branch of month
* @property BranchIdentifier $y Branch of year
* @property BranchIdentifier $h Branch of current hour
* @property BranchIdentifier $w Branch of week
*/
class Sexagenary
{
/**
* Store sexagenary terms corresponding to Lunar date time
*
* @var SexagenaryMilestone
*/
private SexagenaryMilestone $terms;
/**
* Creat new Sexagenary system
*
* @param LunarDateTime $lunar Lunar date time
* @param string $stemIdetifier Stem identifier class, support to localization
* @param string $branchIdentifier Branch identifier class, support to localization
* @param SexagenaryFormattable $formatter Formatter
*/
public function __construct(
private LunarDateTime $lunar,
private string $stemIdetifier = StemIdentifier::class,
private string $branchIdentifier = BranchIdentifier::class,
private SexagenaryFormattable $formatter = new SexagenaryDefaultFormatter,
) {
$this->terms = (new LunarGuaranteedToSexagenary(
lunar: $this->lunar->getGuaranteedLunarDateTime(),
offset: $this->lunar->getOffset(),
stemResolver: $this->createStemResolver(),
branchResolver: $this->createBranchResolver(),
))->getOutput();
}
/**
* Create Stem terms resolver
*
* @return StemTermResolver
*/
protected function createStemResolver(): StemTermResolver
{
$resolve = new StemTermResolver();
$resolve->setTargetTermClass($this->stemIdetifier);
return $resolve;
}
/**
* Create Branches terms resolver
*
* @return BranchTermResolver
*/
protected function createBranchResolver(): BranchTermResolver
{
$resolve = new BranchTermResolver();
$resolve->setTargetTermClass($this->branchIdentifier);
return $resolve;
}
/**
* Magic getter
*
* @param string $name
* @return SexagenaryIdentifier
*/
public function __get(string $name): SexagenaryIdentifier
{
if (!property_exists($this->terms, $name)) {
throw new Exception("Target property '{$name}' dose not exists");
}
return $this->terms->{$name};
}
/**
* Returns sexagenary terms formatted according to given format
*
* @param string $formatter
* @return string
*/
public function format(string $formatter): string
{
return $this->formatter->format(
formatter: $formatter,
terms: $this->terms
);
}
}