Skip to content

Commit 627b374

Browse files
committed
Added static method and change convert to constructor
1 parent d00ec9a commit 627b374

File tree

3 files changed

+49
-19
lines changed

3 files changed

+49
-19
lines changed

readme.md

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -26,21 +26,23 @@ Whenever you want to convert any datetime into Khmer language, just wrap it insi
2626

2727
Firstly, you can import or instance class
2828
```php
29+
// Using import namespace
2930
use Phanna\Converter\KhmerDatetime;
30-
....
31-
$khmer = new Phanna\Converter\KhmerDatetime;
31+
32+
// Using fully-qualified class name
33+
$khmer = new Phanna\Converter\KhmerDatetime($date);
3234
```
3335

3436
After that you can you following method below.
3537

3638
```php
37-
$khmer = new KhmerDatetime;
38-
$date = date('Y-m-d'); // or specific date that you want
39-
// Example output 2019-01-22
39+
$date = '2019-01-22'; // or specific date that you want
40+
$khmer = new KhmerDatetime($date);
4041

41-
$khmer->convert($date)->getDate();
42+
$khmer->getDate();
4243
// Output: ២០១៩-មករា-២២
4344
```
45+
4446
### Available methods
4547

4648
This library accept two date format
@@ -50,33 +52,44 @@ $date = '2019-01-22'; // Y-m-d
5052
// and
5153
$date = '2019/01/22'; // Y/m/d
5254
```
53-
Some useful method in this library.
55+
There are two way of converting date
56+
- One is passing date through constructor
57+
- Second use date with static method
5458

5559
```php
56-
$khmer = new KhmerDatetime;
5760
$date = '2019-01-22';
5861

59-
$khmer->convert($date)->getFullMonth();
62+
// With constructor
63+
$khmer = new KhmerDatetime($date);
64+
65+
// With static method
66+
$khmer = KhmerDatetime::with($date);
67+
68+
$khmer->getFullMonth();
6069
// Output: មករា
6170

62-
$khmer->convert($date)->getFullYear();
71+
$khmer->getFullYear();
6372
// Output: ២០១៩
6473

65-
$khmer->convert($date)->getFullDay();
74+
$khmer->getFullDay();
6675
// Output: ២២
6776

6877
$date = '2019/01/22'; // forward slash format
69-
$khmer->convert($date)->getDate();
78+
$khmer->getDate();
7079
// Output: ២០១៩/មករា/២២
7180

7281
$date = '2019-01-22'; // dash format
73-
$khmer->convert($date)->getDate();
82+
$khmer->getDate();
7483
// Output: ២០១៩-មករា-២២
7584

7685
$date = '2019/01/22'; // Reverse from Y/m/d to d/m/Y
77-
$khmer->convert($date)->getDate('reverse');
86+
$khmer->getDate('reverse');
7887
// Output: ២២/មករា/២០១៩
7988

89+
// Or wrap it in one line
90+
$date = '2019/01/22';
91+
KhmerDatetime::with($date)->getDate();
92+
// Output: ២០១៩/មករា/២២
8093
```
8194

8295
## Contributing

src/KhmerDatetime.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class KhmerDatetime
2323
*/
2424
public $date;
2525

26-
public function convert($date)
26+
public function __construct($date)
2727
{
2828
$this->date = $date;
2929
$this->khmerMonth = $this->getKhmerMonths();
@@ -33,6 +33,16 @@ public function convert($date)
3333
return $this;
3434
}
3535

36+
/**
37+
* Immediately given date with static method
38+
*
39+
* @param string $date
40+
* @return static
41+
*/
42+
public static function with($date)
43+
{
44+
return new static($date);
45+
}
3646
/**
3747
* Get full month name in Khmer.
3848
*

tests/KhmerDatetimeTest.php

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ class KhmerDatetimeTest extends \PHPUnit\Framework\TestCase
88

99
public function setUp()
1010
{
11-
$date = date('2019-01-22');
12-
$this->khmer = (new KhmerDatetime())->convert($date);
11+
$date = '2019-01-22';
12+
$this->khmer = new KhmerDatetime($date);
1313
}
1414

1515
public function test_get_full_month_in_khmer()
@@ -35,7 +35,7 @@ public function test_check_dash_format_date()
3535
public function test_check_forward_slash_format()
3636
{
3737
$date = date('2019/01/22');
38-
$khmer = (new KhmerDatetime())->convert($date);
38+
$khmer = new KhmerDatetime($date);
3939

4040
$this->assertFalse($this->khmer->isForwardSlah());
4141
$this->assertTrue($khmer->isForwardSlah());
@@ -51,11 +51,18 @@ public function test_get_reverse_date_in_khmer()
5151
$this->assertEquals('២២-មករា-២០១៩', $this->khmer->getDate('revers'));
5252
}
5353

54+
public function test_with_static_method()
55+
{
56+
$date = date('2019-01-22');
57+
$khmer = KhmerDatetime::with($date);
58+
$this->assertEquals('២០១៩-មករា-២២', $khmer->getDate());
59+
}
60+
5461
public function test_throw_exception_when_wrong_format()
5562
{
5663
$this->expectException(\Exception::class);
5764

5865
$date = date('2019\01\22');
59-
$khmer = (new KhmerDatetime())->convert($date)->getDate();
66+
$khmer = (new KhmerDatetime($date))->getDate();
6067
}
6168
}

0 commit comments

Comments
 (0)