phpvms/app/Models/Enums/Days.php

96 lines
2.0 KiB
PHP
Raw Normal View History

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