Skip to content

Commit

Permalink
feat(services): holiday api
Browse files Browse the repository at this point in the history
  • Loading branch information
zhaiyuxin103 committed Jan 17, 2025
1 parent 9f84fdd commit f06ac24
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 24 deletions.
29 changes: 29 additions & 0 deletions app/Facades/Holiday.php
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;
}
}
47 changes: 47 additions & 0 deletions app/Services/HolidayService.php
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());
}
}
}
21 changes: 13 additions & 8 deletions lang/en/validation.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,19 @@
'string' => 'The :attribute must not be greater than :max characters.',
],
'max_digits' => 'The :attribute field must not have more than :max digits.',
'mimes' => 'The :attribute must be a file of type: :values.',
'mimetypes' => 'The :attribute must be a file of type: :values.',
'min' => [
'messages' => [
'phone' => [
'phone' => 'The :attribute field must be a valid phone number.',
],
'holiday' => [
'format' => [
'in' => 'The format :format is not supported. Supported formats are: :values.',
],
],
],
'mimes' => 'The :attribute must be a file of type: :values.',
'mimetypes' => 'The :attribute must be a file of type: :values.',
'min' => [
'array' => 'The :attribute must have at least :min items.',
'file' => 'The :attribute must be at least :min kilobytes.',
'numeric' => 'The :attribute must be at least :min.',
Expand Down Expand Up @@ -277,9 +287,4 @@
'year' => 'year',
'zip' => 'zip code',
],
'messages' => [
'phone' => [
'phone' => 'The :attribute field must be a valid phone number.',
],
],
];
21 changes: 13 additions & 8 deletions lang/ja/validation.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,19 @@
'string' => ':Attributeの文字数は、:max文字以下である必要があります。',
],
'max_digits' => ':Attributeは、:max桁以下の数字である必要があります。',
'mimes' => ':Attributeには、以下のファイルタイプを指定してください。:values',
'mimetypes' => ':Attributeには、以下のファイルタイプを指定してください。:values',
'min' => [
'messages' => [
'phone' => [
'phone' => ':Attributeは有効な電話番号ではありません。',
],
'holiday' => [
'format' => [
'in' => 'フォーマット「:format」は無効です,:values のいずれかを指定してください。',
],
],
],
'mimes' => ':Attributeには、以下のファイルタイプを指定してください。:values',
'mimetypes' => ':Attributeには、以下のファイルタイプを指定してください。:values',
'min' => [
'array' => ':Attributeの項目数は、:min個以上にしてください。',
'file' => ':Attributeには、:min KB以上のファイルを指定してください。',
'numeric' => ':Attributeには、:min以上の数値を指定してください。',
Expand Down Expand Up @@ -277,9 +287,4 @@
'year' => '',
'zip' => '郵便番号',
],
'messages' => [
'phone' => [
'phone' => ':Attributeは有効な電話番号ではありません。',
],
],
];
21 changes: 13 additions & 8 deletions lang/zh_CN/validation.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,19 @@
'string' => ':Attribute 不能大于 :max 个字符。',
],
'max_digits' => ':Attribute 不能超过 :max 位数。',
'mimes' => ':Attribute 必须是一个 :values 类型的文件。',
'mimetypes' => ':Attribute 必须是一个 :values 类型的文件。',
'min' => [
'messages' => [
'phone' => [
'phone' => '电话号码格式不正确。',
],
'holiday' => [
'format' => [
'in' => '不支持的格式: :format,支持的格式有: :values。',
],
],
],
'mimes' => ':Attribute 必须是一个 :values 类型的文件。',
'mimetypes' => ':Attribute 必须是一个 :values 类型的文件。',
'min' => [
'array' => ':Attribute 至少有 :min 个单元。',
'file' => ':Attribute 大小不能小于 :min KB。',
'numeric' => ':Attribute 必须大于等于 :min。',
Expand Down Expand Up @@ -277,9 +287,4 @@
'year' => '',
'zip' => '邮政编码',
],
'messages' => [
'phone' => [
'phone' => '电话号码格式不正确。',
],
],
];

0 comments on commit f06ac24

Please sign in to comment.