v7.0.0-beta.1 release

This commit is contained in:
HeuJung committed 2026-04-01 10:30:52 +09:00
commit 6595fd0eb5
3967 files changed
+1875383

No files matched your search

@@ -0,0 +1,67 @@
<?php
namespace App\Http\Resources\Traits;
use App\Helpers\PermissionHelper;
use App\Models\User;
/**
* 권한(ability) 체크 공통 로직을 제공합니다.
*
* BaseApiResource와 ResourceCollection 양쪽에서 사용하여
* checkAbility() 로직 중복을 방지합니다.
*/
trait HasAbilityCheck
{
/**
* 단일 permission identifier를 체크합니다.
* Gate/Policy 미사용 — User 모델 직접 호출.
*
* @param string $ability Permission identifier
* @param User|null $user 현재 사용자 (null = Guest)
* @return bool 권한 보유 여부
*/
protected function checkAbility(string $ability, ?User $user): bool
{
return PermissionHelper::check($ability, $user);
}
/**
* 능력 맵을 불리언으로 변환합니다.
*
* 권한 체크 후 scope_type 기반 스코프 접근 체크를 수행합니다.
*
* @param array<string, string> $map ['can_update' => 'module.entity.update', ...]
* @param User|null $user 현재 사용자
* @return array<string, bool> 능력 불리언 맵
*/
protected function resolveAbilitiesFromMap(array $map, ?User $user): array
{
if (empty($map)) {
return [];
}
return collect($map)
->mapWithKeys(function (string $identifier, string $key) use ($user) {
$hasPermission = PermissionHelper::check($identifier, $user);
// 권한이 없으면 스코프 체크 불필요
if (! $hasPermission) {
return [$key => false];
}
// 리소스 모델이 있으면 스코프 접근 체크
// JsonResource 이중 래핑 시 내부 모델까지 추출
$resource = $this->resource;
while ($resource instanceof \Illuminate\Http\Resources\Json\JsonResource) {
$resource = $resource->resource;
}
if ($resource instanceof \Illuminate\Database\Eloquent\Model) {
return [$key => PermissionHelper::checkScopeAccess($resource, $identifier, $user)];
}
return [$key => true];
})
->toArray();
}
}
@@ -0,0 +1,79 @@
<?php
namespace App\Http\Resources\Traits;
use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Support\Collection;
/**
* 컬렉션 항목에 순번(number)을 부여하는 Trait
*
* 페이지네이션 정보와 정렬 방향을 기반으로 각 항목에 순번을 계산합니다.
* - 내림차순(desc): 역순 (total, total-1, ...)
* - 오름차순(asc): 기본순 (1, 2, 3, ...)
*/
trait HasRowNumber
{
/**
* 순번이 포함된 데이터 매핑을 수행합니다.
*
* @param callable $callback 각 항목을 변환하는 콜백 함수
* @param string|null $sortOrder 정렬 방향 (asc|desc), null인 경우 request에서 추출
* @return Collection 순번이 포함된 변환된 컬렉션
*/
protected function mapWithRowNumber(callable $callback, ?string $sortOrder = null): Collection
{
$sortOrder = $sortOrder ?? request()->input('sort_order', 'desc');
// 페이지네이션이 아닌 경우 단순 인덱스 기반 순번 부여
if (! $this->resource instanceof LengthAwarePaginator) {
return $this->collection->map(function ($item, $index) use ($callback, $sortOrder) {
$total = $this->collection->count();
$number = $sortOrder === 'asc' ? $index + 1 : $total - $index;
$data = $callback($item);
return array_merge(['number' => $number], $data);
});
}
// 페이지네이션 정보 추출
$total = $this->resource->total();
$perPage = $this->resource->perPage();
$currentPage = $this->resource->currentPage();
return $this->collection->map(function ($item, $index) use ($callback, $sortOrder, $total, $perPage, $currentPage) {
$number = $this->calculateRowNumber($index, $sortOrder, $total, $perPage, $currentPage);
$data = $callback($item);
return array_merge(['number' => $number], $data);
});
}
/**
* 순번을 계산합니다.
*
* @param int $index 현재 항목의 인덱스 (0부터 시작)
* @param string $sortOrder 정렬 방향 (asc|desc)
* @param int $total 전체 항목 수
* @param int $perPage 페이지당 항목 수
* @param int $currentPage 현재 페이지 번호
* @return int 계산된 순번
*/
protected function calculateRowNumber(
int $index,
string $sortOrder,
int $total,
int $perPage,
int $currentPage
): int {
if ($sortOrder === 'asc') {
// 오름차순: 1, 2, 3, ... (페이지별 연속)
// 예: 2페이지 → ((2-1) * 10) + 0 + 1 = 11, 12, 13, ...
return (($currentPage - 1) * $perPage) + $index + 1;
}
// 내림차순: total, total-1, ... (역순)
// 예: 총 30개, 2페이지 → 30 - ((2-1) * 10) - 0 = 20, 19, 18, ...
return $total - (($currentPage - 1) * $perPage) - $index;
}
}