From 6b20ea6f1630f208612f8c6019548b8b408d3e0f Mon Sep 17 00:00:00 2001 From: Nabeel Shahzad Date: Wed, 16 Aug 2017 08:29:05 -0500 Subject: [PATCH] set days of week using bitwise operators --- app/Facades/Utils.php | 24 ++++++++++++++++++++++++ config/enums.php | 10 ++++++++++ tests/UtilsTest.php | 13 +++++++++++++ 3 files changed, 47 insertions(+) create mode 100644 tests/UtilsTest.php diff --git a/app/Facades/Utils.php b/app/Facades/Utils.php index 1378f884..4fd24b90 100644 --- a/app/Facades/Utils.php +++ b/app/Facades/Utils.php @@ -16,4 +16,28 @@ class Utils extends Facade return $dtF->diff($dtT)->format($format); } + + /** + * Bitwise operator for setting days of week to integer field + * @param int $datefield initial datefield + * @param array $day_enums Array of values from config("enum.days") + * @return int + */ + public static function setDays(int $datefield, array $day_enums) { + foreach($day_enums as $day) { + $datefield |= $day; + } + + return $datefield; + } + + /** + * Bit check if a day exists within a integer bitfield + * @param int $datefield datefield from database + * @param int $day_enum Value from config("enum.days") + * @return bool + */ + public static function hasDay(int $datefield, int $day_enum) { + return ($datefield & $day_enum) === $datefield; + } } diff --git a/config/enums.php b/config/enums.php index 29893d8b..4fe0312f 100644 --- a/config/enums.php +++ b/config/enums.php @@ -26,5 +26,15 @@ return [ 'MOGAS' => 2, ], + 'days' => [ + 'MONDAY' => 1, + 'TUESDAY' => 2, + 'WEDNESDAY' => 4, + 'THURSDAY' => 8, + 'FRIDAY' => 16, + 'SATURDAY' => 32, + 'SUNDAY' => 64 + ], + ]; diff --git a/tests/UtilsTest.php b/tests/UtilsTest.php new file mode 100644 index 00000000..46b20008 --- /dev/null +++ b/tests/UtilsTest.php @@ -0,0 +1,13 @@ +