instances added god may help
This commit is contained in:
@@ -7,7 +7,7 @@ use MintyPHP\Repository\Support\RepoQuery;
|
||||
|
||||
class PermissionRepository
|
||||
{
|
||||
public static function list(): array
|
||||
public function list(): array
|
||||
{
|
||||
$rows = DB::select('select id, `key`, description, active, is_system, created from permissions order by `key` asc');
|
||||
if (!is_array($rows)) {
|
||||
@@ -23,7 +23,7 @@ class PermissionRepository
|
||||
return $list;
|
||||
}
|
||||
|
||||
public static function listActive(): array
|
||||
public function listActive(): array
|
||||
{
|
||||
$rows = DB::select(
|
||||
'select id, `key`, description, active, is_system, created from permissions where active = 1 order by `key` asc'
|
||||
@@ -41,7 +41,7 @@ class PermissionRepository
|
||||
return $list;
|
||||
}
|
||||
|
||||
public static function listPaged(array $options): array
|
||||
public function listPaged(array $options): array
|
||||
{
|
||||
$search = trim((string) ($options['search'] ?? ''));
|
||||
$allowedOrder = [
|
||||
@@ -79,7 +79,7 @@ class PermissionRepository
|
||||
return ['data' => $list, 'total' => (int) $total];
|
||||
}
|
||||
|
||||
public static function find(int $id): ?array
|
||||
public function find(int $id): ?array
|
||||
{
|
||||
$row = DB::selectOne(
|
||||
'select id, `key`, description, active, is_system, created from permissions where id = ? limit 1',
|
||||
@@ -89,7 +89,7 @@ class PermissionRepository
|
||||
return is_array($data) ? $data : null;
|
||||
}
|
||||
|
||||
public static function findByKey(string $key): ?array
|
||||
public function findByKey(string $key): ?array
|
||||
{
|
||||
$row = DB::selectOne(
|
||||
'select id, `key`, description, active, is_system, created from permissions where `key` = ? limit 1',
|
||||
@@ -99,7 +99,7 @@ class PermissionRepository
|
||||
return is_array($data) ? $data : null;
|
||||
}
|
||||
|
||||
public static function create(array $data): ?int
|
||||
public function create(array $data): ?int
|
||||
{
|
||||
$key = trim((string) ($data['key'] ?? ''));
|
||||
$desc = trim((string) ($data['description'] ?? ''));
|
||||
@@ -115,7 +115,7 @@ class PermissionRepository
|
||||
return $result ? (int) $result : null;
|
||||
}
|
||||
|
||||
public static function update(int $id, array $data): bool
|
||||
public function update(int $id, array $data): bool
|
||||
{
|
||||
$key = trim((string) ($data['key'] ?? ''));
|
||||
$desc = trim((string) ($data['description'] ?? ''));
|
||||
@@ -132,7 +132,7 @@ class PermissionRepository
|
||||
return $result !== false;
|
||||
}
|
||||
|
||||
public static function delete(int $id): bool
|
||||
public function delete(int $id): bool
|
||||
{
|
||||
$result = DB::delete('delete from permissions where id = ?', (string) $id);
|
||||
return (bool) $result;
|
||||
|
||||
@@ -6,7 +6,7 @@ use MintyPHP\DB;
|
||||
|
||||
class RolePermissionRepository
|
||||
{
|
||||
public static function listPermissionIdsByRoleId(int $roleId): array
|
||||
public function listPermissionIdsByRoleId(int $roleId): array
|
||||
{
|
||||
$rows = DB::select('select permission_id from role_permissions where role_id = ?', (string) $roleId);
|
||||
if (!is_array($rows)) {
|
||||
@@ -22,7 +22,7 @@ class RolePermissionRepository
|
||||
return array_values(array_unique($ids));
|
||||
}
|
||||
|
||||
public static function countPermissionsByRoleIds(array $roleIds): array
|
||||
public function countPermissionsByRoleIds(array $roleIds): array
|
||||
{
|
||||
$roleIds = array_values(array_unique(array_map('intval', $roleIds)));
|
||||
$roleIds = array_values(array_filter($roleIds, static fn ($id) => $id > 0));
|
||||
@@ -54,7 +54,7 @@ class RolePermissionRepository
|
||||
return $result;
|
||||
}
|
||||
|
||||
public static function replaceForRole(int $roleId, array $permissionIds): bool
|
||||
public function replaceForRole(int $roleId, array $permissionIds): bool
|
||||
{
|
||||
DB::delete('delete from role_permissions where role_id = ?', (string) $roleId);
|
||||
$ids = array_values(array_unique(array_filter(array_map('intval', $permissionIds))));
|
||||
@@ -71,7 +71,7 @@ class RolePermissionRepository
|
||||
return true;
|
||||
}
|
||||
|
||||
public static function listRoleIdsByPermissionId(int $permissionId): array
|
||||
public function listRoleIdsByPermissionId(int $permissionId): array
|
||||
{
|
||||
$rows = DB::select('select role_id from role_permissions where permission_id = ?', (string) $permissionId);
|
||||
if (!is_array($rows)) {
|
||||
@@ -87,7 +87,7 @@ class RolePermissionRepository
|
||||
return array_values(array_unique($ids));
|
||||
}
|
||||
|
||||
public static function replaceForPermission(int $permissionId, array $roleIds): bool
|
||||
public function replaceForPermission(int $permissionId, array $roleIds): bool
|
||||
{
|
||||
DB::delete('delete from role_permissions where permission_id = ?', (string) $permissionId);
|
||||
$ids = array_values(array_unique(array_filter(array_map('intval', $roleIds))));
|
||||
@@ -104,7 +104,7 @@ class RolePermissionRepository
|
||||
return true;
|
||||
}
|
||||
|
||||
public static function listPermissionKeysByRoleIds(array $roleIds): array
|
||||
public function listPermissionKeysByRoleIds(array $roleIds): array
|
||||
{
|
||||
$ids = array_values(array_unique(array_filter(array_map('intval', $roleIds))));
|
||||
if (!$ids) {
|
||||
@@ -132,7 +132,7 @@ class RolePermissionRepository
|
||||
return array_values(array_unique($keys));
|
||||
}
|
||||
|
||||
public static function listPermissionsWithRolesByRoleIds(array $roleIds): array
|
||||
public function listPermissionsWithRolesByRoleIds(array $roleIds): array
|
||||
{
|
||||
$ids = array_values(array_unique(array_filter(array_map('intval', $roleIds))));
|
||||
if (!$ids) {
|
||||
@@ -179,7 +179,7 @@ class RolePermissionRepository
|
||||
return array_values($permMap);
|
||||
}
|
||||
|
||||
private static function extractIntField(array $row, string $field): int
|
||||
private function extractIntField(array $row, string $field): int
|
||||
{
|
||||
foreach ($row as $value) {
|
||||
if (!is_array($value)) {
|
||||
|
||||
@@ -7,7 +7,7 @@ use MintyPHP\Repository\Support\RepoQuery;
|
||||
|
||||
class RoleRepository
|
||||
{
|
||||
private static function unwrap(?array $row): ?array
|
||||
private function unwrap(?array $row): ?array
|
||||
{
|
||||
if (!$row) {
|
||||
return null;
|
||||
@@ -15,7 +15,7 @@ class RoleRepository
|
||||
return $row['roles'] ?? null;
|
||||
}
|
||||
|
||||
private static function unwrapList($rows): array
|
||||
private function unwrapList($rows): array
|
||||
{
|
||||
if (!is_array($rows)) {
|
||||
return [];
|
||||
@@ -30,23 +30,23 @@ class RoleRepository
|
||||
return $list;
|
||||
}
|
||||
|
||||
public static function list(): array
|
||||
public function list(): array
|
||||
{
|
||||
$rows = DB::select(
|
||||
'select id, uuid, description, code, active, created_by, modified_by, created, modified from roles order by id desc'
|
||||
);
|
||||
return self::unwrapList($rows);
|
||||
return $this->unwrapList($rows);
|
||||
}
|
||||
|
||||
public static function listActive(): array
|
||||
public function listActive(): array
|
||||
{
|
||||
$rows = DB::select(
|
||||
'select id, uuid, description, code, active, created_by, modified_by, created, modified from roles where active = 1 order by description asc'
|
||||
);
|
||||
return self::unwrapList($rows);
|
||||
return $this->unwrapList($rows);
|
||||
}
|
||||
|
||||
public static function listByIds(array $roleIds): array
|
||||
public function listByIds(array $roleIds): array
|
||||
{
|
||||
$roleIds = array_values(array_unique(array_map('intval', $roleIds)));
|
||||
$roleIds = array_values(array_filter($roleIds, static fn ($id) => $id > 0));
|
||||
@@ -59,10 +59,10 @@ class RoleRepository
|
||||
'select id, uuid, description, code, active, created_by, modified_by, created, modified from roles where id in (' . $placeholders . ')',
|
||||
...array_map('strval', $roleIds)
|
||||
);
|
||||
return self::unwrapList($rows);
|
||||
return $this->unwrapList($rows);
|
||||
}
|
||||
|
||||
public static function listIds(): array
|
||||
public function listIds(): array
|
||||
{
|
||||
$rows = DB::select('select id from roles');
|
||||
if (!is_array($rows)) {
|
||||
@@ -78,7 +78,7 @@ class RoleRepository
|
||||
return array_values(array_unique($ids));
|
||||
}
|
||||
|
||||
public static function listActiveIds(): array
|
||||
public function listActiveIds(): array
|
||||
{
|
||||
$rows = DB::select('select id from roles where active = 1');
|
||||
if (!is_array($rows)) {
|
||||
@@ -94,7 +94,7 @@ class RoleRepository
|
||||
return array_values(array_unique($ids));
|
||||
}
|
||||
|
||||
public static function listPaged(array $options): array
|
||||
public function listPaged(array $options): array
|
||||
{
|
||||
$search = trim((string) ($options['search'] ?? ''));
|
||||
$allowedOrder = ['id', 'uuid', 'description', 'code', 'active', 'created', 'modified'];
|
||||
@@ -123,29 +123,29 @@ class RoleRepository
|
||||
|
||||
return [
|
||||
'total' => $total,
|
||||
'rows' => self::unwrapList($rows),
|
||||
'rows' => $this->unwrapList($rows),
|
||||
];
|
||||
}
|
||||
|
||||
public static function find(int $id): ?array
|
||||
public function find(int $id): ?array
|
||||
{
|
||||
$row = DB::selectOne(
|
||||
'select id, uuid, description, code, active, created_by, modified_by, created, modified from roles where id = ? limit 1',
|
||||
(string) $id
|
||||
);
|
||||
return self::unwrap($row);
|
||||
return $this->unwrap($row);
|
||||
}
|
||||
|
||||
public static function findByUuid(string $uuid): ?array
|
||||
public function findByUuid(string $uuid): ?array
|
||||
{
|
||||
$row = DB::selectOne(
|
||||
'select id, uuid, description, code, active, created_by, modified_by, created, modified from roles where uuid = ? limit 1',
|
||||
$uuid
|
||||
);
|
||||
return self::unwrap($row);
|
||||
return $this->unwrap($row);
|
||||
}
|
||||
|
||||
public static function existsByCode(string $code, int $excludeId = 0): bool
|
||||
public function existsByCode(string $code, int $excludeId = 0): bool
|
||||
{
|
||||
$code = trim($code);
|
||||
if ($code === '') {
|
||||
@@ -159,7 +159,7 @@ class RoleRepository
|
||||
return $count ? ((int) $count > 0) : false;
|
||||
}
|
||||
|
||||
public static function create(array $data)
|
||||
public function create(array $data)
|
||||
{
|
||||
return DB::insert(
|
||||
'insert into roles (uuid, description, code, active, created_by, created) values (?,?,?,?,?,NOW())',
|
||||
@@ -171,7 +171,7 @@ class RoleRepository
|
||||
);
|
||||
}
|
||||
|
||||
public static function update(int $id, array $data): bool
|
||||
public function update(int $id, array $data): bool
|
||||
{
|
||||
$fields = [
|
||||
'description' => $data['description'],
|
||||
@@ -195,7 +195,7 @@ class RoleRepository
|
||||
return $result !== false;
|
||||
}
|
||||
|
||||
public static function delete(int $id): bool
|
||||
public function delete(int $id): bool
|
||||
{
|
||||
$result = DB::delete('delete from roles where id = ?', (string) $id);
|
||||
return $result !== false;
|
||||
|
||||
@@ -6,7 +6,7 @@ use MintyPHP\DB;
|
||||
|
||||
class UserRoleRepository
|
||||
{
|
||||
public static function listRoleIdsByUserId(int $userId): array
|
||||
public function listRoleIdsByUserId(int $userId): array
|
||||
{
|
||||
$rows = DB::select('select role_id from user_roles where user_id = ?', (string) $userId);
|
||||
if (!is_array($rows)) {
|
||||
@@ -22,7 +22,7 @@ class UserRoleRepository
|
||||
return array_values(array_unique($ids));
|
||||
}
|
||||
|
||||
public static function replaceForUser(int $userId, array $roleIds): bool
|
||||
public function replaceForUser(int $userId, array $roleIds): bool
|
||||
{
|
||||
DB::delete('delete from user_roles where user_id = ?', (string) $userId);
|
||||
if (!$roleIds) {
|
||||
@@ -40,17 +40,17 @@ class UserRoleRepository
|
||||
return true;
|
||||
}
|
||||
|
||||
public static function countUsersByRoleIds(array $roleIds): array
|
||||
public function countUsersByRoleIds(array $roleIds): array
|
||||
{
|
||||
return self::countByRoleIds($roleIds, false);
|
||||
return $this->countByRoleIds($roleIds, false);
|
||||
}
|
||||
|
||||
public static function countActiveUsersByRoleIds(array $roleIds): array
|
||||
public function countActiveUsersByRoleIds(array $roleIds): array
|
||||
{
|
||||
return self::countByRoleIds($roleIds, true);
|
||||
return $this->countByRoleIds($roleIds, true);
|
||||
}
|
||||
|
||||
private static function countByRoleIds(array $roleIds, bool $activeOnly): array
|
||||
private function countByRoleIds(array $roleIds, bool $activeOnly): array
|
||||
{
|
||||
$roleIds = array_values(array_unique(array_map('intval', $roleIds)));
|
||||
$roleIds = array_values(array_filter($roleIds, static fn ($id) => $id > 0));
|
||||
@@ -73,17 +73,17 @@ class UserRoleRepository
|
||||
|
||||
$result = [];
|
||||
foreach ($rows as $row) {
|
||||
$roleId = self::extractIntField($row, 'role_id');
|
||||
$roleId = $this->extractIntField($row, 'role_id');
|
||||
if ($roleId <= 0) {
|
||||
continue;
|
||||
}
|
||||
$result[$roleId] = self::extractIntField($row, 'user_count');
|
||||
$result[$roleId] = $this->extractIntField($row, 'user_count');
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
private static function extractIntField(array $row, string $field): int
|
||||
private function extractIntField(array $row, string $field): int
|
||||
{
|
||||
foreach ($row as $value) {
|
||||
if (!is_array($value)) {
|
||||
|
||||
Reference in New Issue
Block a user