accessflags

This commit is contained in:
Dr Masroor Ehsan 2024-12-28 12:14:15 +06:00
parent de1dcee423
commit 023eb4616e
3 changed files with 144 additions and 0 deletions

View File

@ -0,0 +1,15 @@
<?php
namespace App\Models\Enums;
use App\Models\Traits\BitmaskFunctionality;
enum StudyAccessFlags: int
{
use BitmaskFunctionality;
case None = 0;
case ViewPatientInfo = 1;
case ViewDicom = 1 << 1;
case ViewReports = 1 << 2;
}

View File

@ -0,0 +1,126 @@
<?php
namespace App\Models\Traits;
use function array_filter;
use function array_reduce;
use function chunk_split;
use function count;
use function sprintf;
use function substr;
/** @psalm-type Int32BitMask = int<0, 2147483647> */
trait BitmaskFunctionality
{
/** @return Int32BitMask */
public static function mask(self ...$bits): int
{
if (count($bits) === 0) {
return 0;
}
return array_reduce($bits, static fn (?int $sum, self $case) => ($sum ?? 0) | $case->value);
}
/** @param Int32BitMask $value */
public static function hasCase(int $value): bool
{
return self::tryFrom($value) !== null;
}
/** @param Int32BitMask $value */
public static function valueToString(int $value): string
{
return '0b'.substr(chunk_split(sprintf('%\'032b', $value), 4, '_'), 0, -1);
}
public function toString(): string
{
return self::valueToString($this->value);
}
/** @return Int32BitMask */
public static function build(self ...$bits): int
{
return self::set(0, ...$bits);
}
/** @param Int32BitMask $value */
/** @return Int32BitMask */
public static function set(int $value, self ...$bits): int
{
return $value | self::mask(...$bits);
}
/** @param Int32BitMask $value */
/** @return Int32BitMask */
public static function clear(int $value, self ...$bits): int
{
return $value & ~self::mask(...$bits);
}
/** @param Int32BitMask $value */
/** @return Int32BitMask */
public static function toggle(int $value, self ...$bits): int
{
return $value ^ self::mask(...$bits);
}
/** @param Int32BitMask $value */
public static function on(int $value, self $bit): bool
{
return ($value & $bit->value) === $bit->value;
}
/** @param Int32BitMask $value */
public static function off(int $value, self $bit): bool
{
return ($value & $bit->value) === 0;
}
/** @param Int32BitMask $value */
public static function any(int $value, self ...$bits): bool
{
if (count($bits) === 0) {
return true;
}
return ($value & self::mask(...$bits)) > 0;
}
/** @param Int32BitMask $value */
public static function all(int $value, self ...$bits): bool
{
if (count($bits) === 0) {
return true;
}
$mask = self::mask(...$bits);
return ($value & $mask) === $mask;
}
/** @param Int32BitMask $value */
public static function none(int $value, self ...$bits): bool
{
if (count($bits) === 0) {
return true;
}
$mask = self::mask(...$bits);
return ($value & $mask) === 0;
}
/**
* @param Int32BitMask $value
* @return list<static>
*/
public static function parse(int $value): array
{
return array_filter(self::cases(), static fn (self $case) => self::on($value, $case));
}
}

View File

@ -1,5 +1,6 @@
<?php <?php
use App\Models\Enums\StudyAccessFlags;
use App\Models\Site; use App\Models\Site;
use App\Models\User; use App\Models\User;
use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Migrations\Migration;
@ -32,6 +33,8 @@ public function up(): void
$table->foreignIdFor(User::class, 'assigned_physician_id')->nullable()->constrained()->onDelete('set null'); $table->foreignIdFor(User::class, 'assigned_physician_id')->nullable()->constrained()->onDelete('set null');
$table->foreignIdFor(User::class, 'interpreting_physician_id')->nullable()->constrained()->onDelete('set null'); $table->foreignIdFor(User::class, 'interpreting_physician_id')->nullable()->constrained()->onDelete('set null');
$table->foreignIdFor(User::class, 'referring_physician_id')->nullable()->constrained()->onDelete('set null'); $table->foreignIdFor(User::class, 'referring_physician_id')->nullable()->constrained()->onDelete('set null');
$table->unsignedTinyInteger('access_flags')->default(StudyAccessFlags::None->value);
$table->string('access_password')->nullable();
$table->timestamps(); $table->timestamps();
$table->index(['referring_physician_id', 'receive_date']); $table->index(['referring_physician_id', 'receive_date']);