set days of week using bitwise operators
This commit is contained in:
parent
073af3e949
commit
6b20ea6f16
@ -16,4 +16,28 @@ class Utils extends Facade
|
|||||||
|
|
||||||
return $dtF->diff($dtT)->format($format);
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -26,5 +26,15 @@ return [
|
|||||||
'MOGAS' => 2,
|
'MOGAS' => 2,
|
||||||
],
|
],
|
||||||
|
|
||||||
|
'days' => [
|
||||||
|
'MONDAY' => 1,
|
||||||
|
'TUESDAY' => 2,
|
||||||
|
'WEDNESDAY' => 4,
|
||||||
|
'THURSDAY' => 8,
|
||||||
|
'FRIDAY' => 16,
|
||||||
|
'SATURDAY' => 32,
|
||||||
|
'SUNDAY' => 64
|
||||||
|
],
|
||||||
|
|
||||||
];
|
];
|
||||||
|
|
||||||
|
13
tests/UtilsTest.php
Normal file
13
tests/UtilsTest.php
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use App\Facades\Utils;
|
||||||
|
|
||||||
|
class UtilsTest extends TestCase
|
||||||
|
{
|
||||||
|
public function setUp() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testSecondsToTime() {
|
||||||
|
print_r(Utils::secondsToTime(3600));
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user