-
Notifications
You must be signed in to change notification settings - Fork 4
/
TanggalMerah.php
82 lines (74 loc) · 2.12 KB
/
TanggalMerah.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
<?php
declare(strict_types=1);
/**
* PHP Indonesia holiday checker (include sunday)
*
* PHP version 7
*
* @category library
* @package TanggalMerah
* @author guangrei <[email protected]>
* @license MIT http://opensource.org/licenses/MIT
* @link https://github.com/guangrei/phptanggalmerah
*/
namespace Grei;
use DateTime;
use DateTimeZone;
use Exception;
class TanggalMerah
{
public $event;
public $date;
protected $data;
public function __construct(string $local = null)
{
$tz = new DateTimeZone("Asia/Jakarta");
$this->event = [];
$this->date = new DateTime("now", $tz);
if (!isset($local)) {
$r = file_get_contents("https://raw.githubusercontent.com/guangrei/APIHariLibur_V2/main/holidays.json");
$this->data = json_decode($r, true);
} else {
$r = file_get_contents($local);
$this->data = json_decode($r, true);
}
} // end __construct()
public function set_timezone(DateTimeZone $tz) : string
{
$this->date = new DateTime("now", $tz);
return $tz->getName();
} // end set_timezone()
public function check() : bool
{
$check = [$this->is_sunday(), $this->is_holiday()];
return in_array(true, $check);
} // end check()
public function is_sunday() : bool
{
$day = $this->date->format("D");
if ($day === "Sun") {
$this->event[] = 'sunday';
return true;
} else {
return false;
}
} // end is_sunday()
public function is_holiday() : bool
{
if (isset($this->data[$this->date->format("Y-m-d")])) {
$this->event[] = $this->data[$this->date->format("Y-m-d")]['summary'];
return true;
} else {
return false;
}
} // end is_holiday()
public function set_date(string $date) : string
{
$this->date = new DateTime($date);
return $date;
} // end set_date()
public function get_event() : array
{
return $this->event;
} // end get_event()
} // end class TanggalMerah