royscheepens/currently-open

Laravel 5.5+ 的当前开放包

dev-master 2017-11-24 08:00 UTC

This package is not auto-updated.

Last update: 2024-09-29 05:08:02 UTC


README

Total Downloads License Latest Stable Version Monthly Downloads

这是一个简单的 Laravel 包,用于根据可配置的配置设置确定商店是营业还是关闭

此包旨在检查您的业务是否营业,基于可配置的设置。它目前支持为一周中的每天设置默认时间段,并可以根据特定日期设置例外。

安装

使用 composer 安装

Laravel 5.4 及以上版本

composer require royscheepens/currently-open:^0.1.0

然后在 config/app.php 中添加服务提供者

'providers' => [
    ........,
    RoyScheepens\CurrentlyOpen\CurrentlyOpenServiceProvider::class,
]

如果您想使用外观,请将以下内容添加到 config/app.php 中的外观中

'aliases' => [
    ........,
    'CurrentlyOpen' => RoyScheepens\CurrentlyOpen\CurrentlyOpenFacade::class,
]

要发布配置文件,请运行

php artisan vendor:publish --provider="RoyScheepens\CurrentlyOpen\CurrentlyOpenServiceProvider"

配置

唯一必需的配置值是 'weekdays',它允许您设置一周中每天的开业时间。如果未设置某天,我们假设您当天不营业。

也可以为特定日期设置例外,但这不是必需的。

return [

    // Add timeslots for each day in the week. Weekdays start at 0 (Sunday)
    'weekdays'      =>  [
        0 => false, // False means we're closed. Set to true to be opened all day
        1 => ['09:00', '17:30'],
        2 => ['09:00', '17:30'],
        3 => ['09:00', '17:30'],
        4 => ['09:00', '17:30'],
        5 => ['09:00', '17:30'],
        6 => ['10:00', '12:30'
    ],

    // Possible exceptions per date. Please note that 'weekday' rules will be overridden
    'exceptions'    =>  [
        '2017-12-25' => false // Closed on Christmas
        '2017-12-26' => ['12:00', '17:30'] // Opening a bit later... *burp*
    ]

];

用法

所有示例均基于上述配置示例。

$result = CurrentlyOpen::check()

var_dump($result->open); // True if we're open, false if not

此检查您的配置与当前日期和时间,例如 'now'。您也可以提供日期以检查未来日期。变量 $date 可以是 Carbon 实例,或者 Carbon 可以解析的字符串。

$date = '2017-12-25 12:00'; // Let's see if we're open for Christmas
$result = CurrentlyOpen::check($date);

var_dump($result->open); // False

check() 方法的结果还包含一个 until 属性,它告诉您何时营业。此属性作为 Carbon 实例返回,并且仅在 open 为 true 时包含。

$date = '2017-12-05 12:00'; // Let's see if we're open this Tuesday in December
$result = CurrentlyOpen::check($date);

var_dump($result->open); // True
var_dump($result->until->toDateTimeString()); // 2017-12-05 17:30:00

如果您只想检查是否营业,请使用以下方法。它返回布尔值,也接受与上面相同的 $date 变量。

$result = CurrentlyOpen::checkSimple()

var_dump($result) // True if we're open, false if not

未来路线图

此包仍然非常基础,可以添加更多功能,如

  • 当整个白天营业时,until 也应检查以下一天的时段
  • 当关闭时,添加我们再次营业的日期和时间
  • 每天支持多个时间段(也适用于例外)
  • 更健壮的配置检查