-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9f84fdd
commit f06ac24
Showing
5 changed files
with
115 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Facades; | ||
|
||
use App\Services\HolidayService; | ||
use Illuminate\Support\Facades\Facade; | ||
|
||
/** | ||
* @method static mixed fetch($year = null, $date = true, $format = 'json') | ||
* | ||
* @see HolidayService | ||
*/ | ||
class Holiday extends Facade | ||
{ | ||
/** | ||
* Create a new class instance. | ||
*/ | ||
public function __construct() | ||
{ | ||
// | ||
} | ||
|
||
public static function getFacadeAccessor(): string | ||
{ | ||
return HolidayService::class; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Services; | ||
|
||
use Illuminate\Support\Facades\Http; | ||
use InvalidArgumentException; | ||
use Symfony\Component\HttpKernel\Exception\HttpException; | ||
use Throwable; | ||
|
||
class HolidayService | ||
{ | ||
private static string $uri = 'https://holidays-jp.github.io/api/v1'; | ||
|
||
/** | ||
* Create a new class instance. | ||
*/ | ||
public function __construct() | ||
{ | ||
// | ||
} | ||
|
||
public static function fetch($year = null, $date = true, $format = 'json') | ||
{ | ||
$uri = self::$uri; | ||
if ($year) { | ||
$uri .= '/' . $year; | ||
} | ||
$uri .= ($date ? '/date' : '/datetime') . '.' . $format; | ||
|
||
if (! in_array(strtolower($format), ['json', 'csv'])) { | ||
throw new InvalidArgumentException(trans('validation.messages.holiday.format.in', [ | ||
'format' => $format, | ||
'values' => 'json, csv', | ||
])); | ||
} | ||
|
||
try { | ||
$response = Http::get($uri); | ||
|
||
return $format === 'json' ? $response->json() : $response->body(); | ||
} catch (Throwable $th) { | ||
throw new HttpException($th->getCode(), $th->getMessage()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters