2017-12-20 10:19:36 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Models\Enums;
|
|
|
|
|
2018-03-19 10:24:41 +08:00
|
|
|
use App\Interfaces\Enum;
|
|
|
|
|
2017-12-21 06:34:52 +08:00
|
|
|
/**
|
|
|
|
* Class Days
|
2018-03-23 10:21:35 +08:00
|
|
|
* Start on Monday - ISO8601
|
2017-12-21 06:34:52 +08:00
|
|
|
* @package App\Models\Enums
|
|
|
|
*/
|
2018-03-20 09:50:40 +08:00
|
|
|
class Days extends Enum
|
|
|
|
{
|
2018-02-11 04:06:25 +08:00
|
|
|
public const MONDAY = 1 << 0;
|
|
|
|
public const TUESDAY = 1 << 1;
|
|
|
|
public const WEDNESDAY = 1 << 2;
|
|
|
|
public const THURSDAY = 1 << 3;
|
|
|
|
public const FRIDAY = 1 << 4;
|
|
|
|
public const SATURDAY = 1 << 5;
|
|
|
|
public const SUNDAY = 1 << 6;
|
2017-12-20 10:19:36 +08:00
|
|
|
|
2018-03-23 10:21:35 +08:00
|
|
|
public static $labels = [
|
2018-05-22 00:55:40 +08:00
|
|
|
Days::MONDAY => 'common.days.mon',
|
|
|
|
Days::TUESDAY => 'common.days.tues',
|
|
|
|
Days::WEDNESDAY => 'common.days.wed',
|
|
|
|
Days::THURSDAY => 'common.days.thurs',
|
|
|
|
Days::FRIDAY => 'common.days.fri',
|
|
|
|
Days::SATURDAY => 'common.days.sat',
|
|
|
|
Days::SUNDAY => 'common.days.sun',
|
2017-12-20 10:19:36 +08:00
|
|
|
];
|
2018-03-23 10:21:35 +08:00
|
|
|
|
|
|
|
public static $codes = [
|
|
|
|
'M' => Days::MONDAY,
|
|
|
|
'T' => Days::TUESDAY,
|
|
|
|
'W' => Days::WEDNESDAY,
|
|
|
|
'Th' => Days::THURSDAY,
|
|
|
|
'F' => Days::FRIDAY,
|
|
|
|
'S' => Days::SATURDAY,
|
|
|
|
'Su' => Days::SUNDAY,
|
|
|
|
];
|
|
|
|
|
2018-04-13 05:12:32 +08:00
|
|
|
/**
|
|
|
|
* Map the ISO8601 numeric today to day
|
|
|
|
*/
|
|
|
|
public static $isoDayMap = [
|
|
|
|
1 => Days::MONDAY,
|
|
|
|
2 => Days::TUESDAY,
|
|
|
|
3 => Days::WEDNESDAY,
|
|
|
|
4 => Days::THURSDAY,
|
|
|
|
5 => Days::FRIDAY,
|
|
|
|
6 => Days::SATURDAY,
|
|
|
|
7 => Days::SUNDAY,
|
|
|
|
];
|
|
|
|
|
2018-03-23 10:21:35 +08:00
|
|
|
/**
|
|
|
|
* Create the masked value for the days of week
|
|
|
|
* @param array $days
|
|
|
|
* @return int|mixed
|
|
|
|
*/
|
|
|
|
public static function getDaysMask(array $days)
|
|
|
|
{
|
|
|
|
$mask = 0;
|
|
|
|
foreach($days as $day) {
|
|
|
|
$mask |= $day;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $mask;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* See if the given mask has a day
|
|
|
|
* @param $mask
|
|
|
|
* @param $day
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public static function in($mask, $day): bool
|
|
|
|
{
|
|
|
|
return ($mask & $day) === $day;
|
|
|
|
}
|
2018-04-13 05:12:32 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Does the mask contain today?
|
|
|
|
* @param $val
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public static function isToday($val): bool
|
|
|
|
{
|
|
|
|
return static::in($val, static::$isoDayMap[(int) date('N')]);
|
|
|
|
}
|
2017-12-20 10:19:36 +08:00
|
|
|
}
|