Skip to content

Commit 7d76628

Browse files
committed
restructure to new format style
1 parent 297471f commit 7d76628

File tree

3 files changed

+70
-47
lines changed

3 files changed

+70
-47
lines changed

src/Config.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ class Config
1818
];
1919
private $days = ['អាទិត្យ', 'ច័ន្ទ', 'អង្គារ', 'ពុធ', 'ព្រហស្បតិ៍', 'សុក្រ', 'សៅរ៍'];
2020
private $months = ['មករា', 'កុម្ភៈ', 'មីនា', 'មេសា', 'ឧសភា', 'មិថុនា', 'កក្កដា', 'សីហា', 'កញ្ញា', 'តុលា', 'វិច្ឆិកា', 'ធ្នូ'];
21+
public $meridiem = [
22+
"am" => "ព្រឹក",
23+
"pm" => "ល្ងាច"
24+
];
2125

2226
/**
2327
* Get Khmer day base on index

src/KhmerDateTime.php

Lines changed: 40 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,15 @@ class KhmerDateTime
1616
/**
1717
* @var Config
1818
*/
19-
private $data;
19+
private $config;
2020
/**
2121
* @var string
2222
*/
2323
private $sign;
2424

2525
public function __construct()
2626
{
27-
$this->data = new Config();
27+
$this->config = new Config();
2828
return $this;
2929
}
3030

@@ -39,11 +39,7 @@ public static function parse($date) {
3939
$instance = new static();
4040
$instance->date = strtotime($date);
4141

42-
if ($instance->isForwardSlash($date)) {
43-
$instance->sign = "/";
44-
} elseif ($instance->isDash($date)) {
45-
$instance->sign = "-";
46-
} else {
42+
if (!$instance->date) {
4743
throw new Exception('Undefined date format');
4844
}
4945

@@ -71,7 +67,7 @@ public static function now()
7167
public function month()
7268
{
7369
$month = date('m', $this->date);
74-
return $this->data->numbers($month);
70+
return $this->config->numbers($month);
7571
}
7672

7773
/**
@@ -82,7 +78,7 @@ public function month()
8278
public function fullMonth()
8379
{
8480
$month = date('n', $this->date);
85-
return $this->data->months($month);
81+
return $this->config->months($month);
8682
}
8783

8884
/**
@@ -92,7 +88,7 @@ public function fullMonth()
9288
*/
9389
public function day()
9490
{
95-
return $this->data->numbers(date('d', $this->date));
91+
return $this->config->numbers(date('d', $this->date));
9692
}
9793

9894
/**
@@ -102,7 +98,7 @@ public function day()
10298
*/
10399
public function fullDay()
104100
{
105-
return $this->data->days(date('w', $this->date));
101+
return $this->config->days(date('w', $this->date));
106102
}
107103

108104
/**
@@ -112,53 +108,57 @@ public function fullDay()
112108
*/
113109
public function year()
114110
{
115-
return $this->data->numbers(date('Y', $this->date));
111+
return $this->config->numbers(date('Y', $this->date));
116112
}
117113

118114
/**
119-
* Get date base on format.
115+
* Get hour in Khmer
120116
*
121-
* @param null $format
122117
* @return string
123-
* @throws Exception
124118
*/
125-
public function date($format = null)
119+
public function hour()
126120
{
127-
if ($format == "long") {
128-
return "ថ្ងៃ".$this->fullDay()." ទី".$this->day()." ខែ".$this->fullMonth()." ឆ្នាំ".$this->year();
129-
}
130-
if ($format == "short") {
131-
return $this->day()." ".$this->fullMonth()." ".$this->year();
132-
}
133-
if ($format == "forward") {
134-
return $this->day()."/".$this->month()."/".$this->year();
135-
}
136-
if ($format == "dash") {
137-
return $this->day()."-".$this->month()."-".$this->year();
138-
}
139-
140-
return implode(" ", [$this->fullDay(), $this->day(), $this->fullMonth(), $this->year()]);
121+
$hour = date('H', $this->date);
122+
return $this->config->numbers($hour);
141123
}
142124

143125
/**
144-
* Checking if given date is a forward slash format.
126+
* Get minute in Khmer
145127
*
146-
* @param $date
147-
* @return bool
128+
* @return string
148129
*/
149-
private function isForwardSlash($date)
130+
public function minute()
150131
{
151-
return is_object(DateTime::createFromFormat('Y/m/d', $date));
132+
return $this->config->numbers(date('i', $this->date));
152133
}
153134

154135
/**
155-
* Checking if given date is a dash format.
136+
* Get time meridiem
156137
*
157-
* @param $date
158-
* @return bool
138+
* @return string
159139
*/
160-
private function isDash($date)
140+
public function meridiem() {
141+
return $this->config->meridiem[date('a', $this->date)];
142+
}
143+
144+
public function format($format)
161145
{
162-
return is_object(DateTime::createFromFormat('Y-m-d', $date));
146+
try {
147+
return $this->availableFormats()[$format];
148+
} catch (Exception $e) {
149+
throw new Exception("Invalid format");
150+
}
151+
}
152+
153+
public function availableFormats() {
154+
return [
155+
'L' => $this->day()."/".$this->month()."/".$this->year(),
156+
'LL' => $this->day()." ".$this->fullMonth()." ".$this->year(),
157+
'LLT' => $this->day()." ".$this->fullMonth()." ".$this->year()." ".$this->hour().":".$this->minute()." ".$this->meridiem(),
158+
'LLL' => $this->fullDay()." ".$this->day()." ".$this->fullMonth()." ".$this->year(),
159+
'LLLT' => $this->fullDay()." ".$this->day()." ".$this->fullMonth()." ".$this->year()." ".$this->hour().":".$this->minute()." ".$this->meridiem(),
160+
'LLLL' => "ថ្ងៃ".$this->fullDay()." ទី".$this->day()." ខែ".$this->fullMonth()." ឆ្នាំ".$this->year(),
161+
'LLLLT' => "ថ្ងៃ".$this->fullDay()." ទី".$this->day()." ខែ".$this->fullMonth()." ឆ្នាំ".$this->year()." ".$this->hour().":".$this->minute()." ".$this->meridiem(),
162+
];
163163
}
164164
}

tests/KhmerDateTimeTest.php

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,25 +7,44 @@
77

88
class KhmerDateTimeTest extends TestCase
99
{
10-
public function test_khmer_date_time_parsing()
10+
public function test_khmer_date_time_parsing_without_time()
1111
{
1212
$dateTime = KhmerDateTime::parse('2019-01-22');
13+
1314
$this->assertEquals('២២', $dateTime->day());
1415
$this->assertEquals('អង្គារ', $dateTime->fullDay());
1516
$this->assertEquals('០១', $dateTime->month());
1617
$this->assertEquals('មករា', $dateTime->fullMonth());
1718
$this->assertEquals('២០១៩', $dateTime->year());
18-
$this->assertEquals("អង្គារ ២២ មករា ២០១៩", $dateTime->date());
19-
$this->assertEquals("២២ មករា ២០១៩", $dateTime->date("short"));
20-
$this->assertEquals("២២-០១-២០១៩", $dateTime->date("dash"));
21-
$this->assertEquals("២២/០១/២០១៩", $dateTime->date("forward"));
22-
$this->assertEquals("ថ្ងៃអង្គារ ទី២២ ខែមករា ឆ្នាំ២០១៩", $dateTime->date("long"));
19+
$this->assertEquals('០០', $dateTime->hour());
20+
$this->assertEquals('០០', $dateTime->minute());
21+
$this->assertEquals('ព្រឹក', $dateTime->meridiem());
2322
}
2423

25-
public function test_throw_exception_when_wrong_format()
24+
public function test_khmer_date_time_parsing_format_with_time()
25+
{
26+
$dateTime = KhmerDateTime::parse('2020-09-20 12:40');
27+
$this->assertEquals("អាទិត្យ ២០ កញ្ញា ២០២០", $dateTime->date());
28+
$this->assertEquals("២០/០៩/២០២០", $dateTime->format("L"));
29+
$this->assertEquals("២០ កញ្ញា ២០២០", $dateTime->format("LL"));
30+
$this->assertEquals("២០ កញ្ញា ២០២០ ១២:៤០ ល្ងាច", $dateTime->format("LLT"));
31+
$this->assertEquals("អាទិត្យ ២០ កញ្ញា ២០២០", $dateTime->format("LLL"));
32+
$this->assertEquals("អាទិត្យ ២០ កញ្ញា ២០២០ ១២:៤០ ល្ងាច", $dateTime->format("LLLT"));
33+
$this->assertEquals("ថ្ងៃអាទិត្យ ទី២០ ខែកញ្ញា ឆ្នាំ២០២០", $dateTime->format("LLLL"));
34+
$this->assertEquals("ថ្ងៃអាទិត្យ ទី២០ ខែកញ្ញា ឆ្នាំ២០២០ ១២:៤០ ល្ងាច", $dateTime->format("LLLLT"));
35+
}
36+
37+
public function test_throw_exception_when_parsing_incorrect_date_time_format()
2638
{
2739
$this->expectException(\Exception::class);
2840

2941
(new KhmerDateTime)->parse('2019\01\22');
3042
}
43+
44+
public function test_throw_exception_when_parsing_incorrect_format()
45+
{
46+
$this->expectException(\Exception::class);
47+
48+
KhmerDateTime::parse('2020-09-20 12:40')->format("other");
49+
}
3150
}

0 commit comments

Comments
 (0)