능력 매핑 */ protected function abilityMap(): array { return [ 'can_create' => 'sirsoft-ecommerce.shipping-policies.create', 'can_update' => 'sirsoft-ecommerce.shipping-policies.update', 'can_delete' => 'sirsoft-ecommerce.shipping-policies.delete', ]; } /** * 배송정책 컬렉션을 배열로 변환합니다. * * @param Request $request HTTP 요청 객체 * @return array 변환된 배송정책 컬렉션 배열 */ public function toArray(Request $request): array { $result = [ 'data' => $this->mapWithRowNumber(function ($shippingPolicy) { return (new ShippingPolicyResource($shippingPolicy))->toArray(request()); }), 'abilities' => $this->resolveAbilitiesFromMap($this->abilityMap(), $request->user()), ]; if ($this->resource instanceof LengthAwarePaginator) { $result['pagination'] = [ 'current_page' => $this->resource->currentPage(), 'last_page' => $this->resource->lastPage(), 'per_page' => $this->resource->perPage(), 'total' => $this->resource->total(), 'from' => $this->resource->firstItem(), 'to' => $this->resource->lastItem(), 'has_more_pages' => $this->resource->hasMorePages(), ]; } return $result; } /** * 통계가 포함된 형태의 배열을 반환합니다. * * @param array $statistics 통계 데이터 배열 * @return array 통계 정보가 포함된 배송정책 컬렉션 */ public function withStatistics(array $statistics = []): array { $result = [ 'data' => $this->mapWithRowNumber(function ($shippingPolicy) { return (new ShippingPolicyResource($shippingPolicy))->toArray(request()); }), 'abilities' => $this->resolveAbilitiesFromMap($this->abilityMap(), request()->user()), 'statistics' => $statistics, ]; if ($this->resource instanceof LengthAwarePaginator) { $result['pagination'] = [ 'current_page' => $this->resource->currentPage(), 'last_page' => $this->resource->lastPage(), 'per_page' => $this->resource->perPage(), 'total' => $this->resource->total(), 'from' => $this->resource->firstItem(), 'to' => $this->resource->lastItem(), 'has_more_pages' => $this->resource->hasMorePages(), ]; } return $result; } /** * 간단한 목록 형태의 배열을 반환합니다 (Select 옵션용). * * @return array> 간략한 배송정책 정보 배열 */ public function toSimpleArray(): array { return $this->collection->map(function ($shippingPolicy) { $countrySetting = $shippingPolicy->countrySettings->first(); $method = $countrySetting?->shipping_method; if ($method === 'custom') { $name = $countrySetting->custom_shipping_name; $locale = app()->getLocale(); $methodLabel = is_array($name) ? ($name[$locale] ?? $name[config('app.fallback_locale', 'ko')] ?? $name[array_key_first($name)] ?? null) : null; } else { $methodLabel = $method ? \Modules\Sirsoft\Ecommerce\Models\ShippingType::getCachedByCode($method)?->getLocalizedName() : null; } return [ 'id' => $shippingPolicy->id, 'name' => $shippingPolicy->getLocalizedName(), 'shipping_method' => $method, 'shipping_method_label' => $methodLabel, 'charge_policy' => $countrySetting?->charge_policy?->value, ]; })->toArray(); } }