stephenlake /
php-google-holidays
| 1 | <?php |
||
| 2 | |||
| 3 | namespace Google; |
||
| 4 | |||
| 5 | class Holidays |
||
| 6 | { |
||
| 7 | /** |
||
| 8 | * Google API Key. |
||
| 9 | * |
||
| 10 | * @var string |
||
| 11 | */ |
||
| 12 | private $api_key; |
||
| 13 | |||
| 14 | /** |
||
| 15 | * Country Code. |
||
| 16 | * |
||
| 17 | * @var string |
||
| 18 | */ |
||
| 19 | private $country_code; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * Start Date. |
||
| 23 | * |
||
| 24 | * @var string |
||
| 25 | */ |
||
| 26 | private $start_date; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * End Date. |
||
| 30 | * |
||
| 31 | * @var string |
||
| 32 | */ |
||
| 33 | private $end_date; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * Minimal output boolean. |
||
| 37 | * |
||
| 38 | * @var bool |
||
| 39 | */ |
||
| 40 | private $minimal = false; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * Dates only output boolean. |
||
| 44 | * |
||
| 45 | * @var bool |
||
| 46 | */ |
||
| 47 | private $dates_only = false; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Construct! |
||
| 51 | * |
||
| 52 | * @return void |
||
| 53 | */ |
||
| 54 | public function __construct() |
||
| 55 | { |
||
| 56 | $this->start_date = date('Y-m-d').'T00:00:00-00:00'; |
||
| 57 | $this->end_date = (date('Y') + 1).'-01-01T00:00:00-00:00'; |
||
| 58 | } |
||
| 59 | |||
| 60 | /** |
||
| 61 | * Setting starting date. |
||
| 62 | * |
||
| 63 | * @param string Date in any format |
||
| 64 | * |
||
| 65 | * @return self |
||
| 66 | */ |
||
| 67 | public function from($str) |
||
| 68 | { |
||
| 69 | $this->start_date = date('Y-m-d', strtotime($str)).'T00:00:00-00:00'; |
||
| 70 | |||
| 71 | return $this; |
||
| 72 | } |
||
| 73 | |||
| 74 | /** |
||
| 75 | * Setting end date. |
||
| 76 | * |
||
| 77 | * @param string Date in any format |
||
| 78 | * |
||
| 79 | * @return self |
||
| 80 | */ |
||
| 81 | public function to($str) |
||
| 82 | { |
||
| 83 | $this->end_date = date('Y-m-d', strtotime($str)).'T00:00:00-00:00'; |
||
| 84 | |||
| 85 | return $this; |
||
| 86 | } |
||
| 87 | |||
| 88 | /** |
||
| 89 | * Setter of API key. |
||
| 90 | * |
||
| 91 | * @return void |
||
| 92 | */ |
||
| 93 | public function withApiKey($api_key) |
||
| 94 | { |
||
| 95 | $this->api_key = $api_key; |
||
| 96 | |||
| 97 | return $this; |
||
|
0 ignored issues
–
show
Bug
Best Practice
introduced
by
Loading history...
|
|||
| 98 | } |
||
| 99 | |||
| 100 | /** |
||
| 101 | * Define the country code to retrieve holidays for. |
||
| 102 | * |
||
| 103 | * @return void |
||
| 104 | */ |
||
| 105 | public function inCountry($country_code) |
||
| 106 | { |
||
| 107 | $this->country_code = strtolower($country_code); |
||
| 108 | |||
| 109 | return $this; |
||
|
0 ignored issues
–
show
|
|||
| 110 | } |
||
| 111 | |||
| 112 | /** |
||
| 113 | * Define the output result as minimal. |
||
| 114 | * |
||
| 115 | * @return void |
||
| 116 | */ |
||
| 117 | public function withMinimalOutput() |
||
| 118 | { |
||
| 119 | $this->minimal = true; |
||
| 120 | |||
| 121 | return $this; |
||
|
0 ignored issues
–
show
|
|||
| 122 | } |
||
| 123 | |||
| 124 | /** |
||
| 125 | * Define the output as dates only. |
||
| 126 | * |
||
| 127 | * @return void |
||
| 128 | */ |
||
| 129 | public function withDatesOnly() |
||
| 130 | { |
||
| 131 | $this->dates_only = true; |
||
| 132 | |||
| 133 | return $this; |
||
|
0 ignored issues
–
show
|
|||
| 134 | } |
||
| 135 | |||
| 136 | /** |
||
| 137 | * Get the list of holidays. |
||
| 138 | * |
||
| 139 | * @return mixed |
||
| 140 | */ |
||
| 141 | public function list() |
||
| 142 | { |
||
| 143 | if (!$this->api_key) { |
||
| 144 | throw new \Exception('Providing an API key might be a better start. RTFM.'); |
||
| 145 | } |
||
| 146 | |||
| 147 | if (!$this->country_code) { |
||
| 148 | throw new \Exception('Providing a Country Code is a good idea. RTFM.'); |
||
| 149 | } |
||
| 150 | |||
| 151 | $result = []; |
||
| 152 | |||
| 153 | $api_url = "https://content.googleapis.com/calendar/v3/calendars/en.{$this->country_code}%23holiday%40group.v.calendar.google.com/events". |
||
| 154 | '?singleEvents=false'. |
||
| 155 | "&timeMax={$this->end_date}". |
||
| 156 | "&timeMin={$this->start_date}". |
||
| 157 | "&key={$this->api_key}"; |
||
| 158 | |||
| 159 | $response = json_decode(file_get_contents($api_url), true); |
||
| 160 | |||
| 161 | if (isset($response['items'])) { |
||
| 162 | if ($this->dates_only === true) { |
||
| 163 | foreach ($response['items'] as $holiday) { |
||
| 164 | $result[] = $holiday['start']['date']; |
||
| 165 | } |
||
| 166 | |||
| 167 | sort($result); |
||
| 168 | } elseif ($this->minimal === true) { |
||
| 169 | foreach ($response['items'] as $holiday) { |
||
| 170 | $result[] = [ |
||
| 171 | 'name' => $holiday['summary'], |
||
| 172 | 'date' => $holiday['start']['date'], |
||
| 173 | ]; |
||
| 174 | } |
||
| 175 | |||
| 176 | usort($result, function ($a, $b) { |
||
| 177 | if ($a['date'] == $b['date']) { |
||
| 178 | return 0; |
||
| 179 | } |
||
| 180 | |||
| 181 | return ($a['date'] < $b['date']) ? -1 : 1; |
||
| 182 | }); |
||
| 183 | } else { |
||
| 184 | $result = $response['items']; |
||
| 185 | |||
| 186 | usort($result, function ($a, $b) { |
||
| 187 | if ($a['start']['date'] == $b['start']['date']) { |
||
| 188 | return 0; |
||
| 189 | } |
||
| 190 | |||
| 191 | return ($a['start']['date'] < $b['start']['date']) ? -1 : 1; |
||
| 192 | }); |
||
| 193 | } |
||
| 194 | } |
||
| 195 | |||
| 196 | return $result; |
||
| 197 | } |
||
| 198 | } |
||
| 199 |