⚝
One Hat Cyber Team
⚝
Your IP:
216.73.216.19
Server IP:
178.33.27.10
Server:
Linux cpanel.dev-unit.com 3.10.0-1160.108.1.el7.x86_64 #1 SMP Thu Jan 25 16:17:31 UTC 2024 x86_64
Server Software:
Apache/2.4.57 (Unix) OpenSSL/1.0.2k-fips
PHP Version:
8.2.11
Buat File
|
Buat Folder
Eksekusi
Dir :
~
/
home
/
id
/
dalily.dev-unit.com
/
app
/
Services
/
View File Name :
CategoryService.php
<?php /* * LaraClassifier - Classified Ads Web Application * Copyright (c) BeDigit. All Rights Reserved * * Website: https://laraclassifier.com * Author: Mayeul Akpovi (BeDigit - https://bedigit.com) * * LICENSE * ------- * This software is provided under a license agreement and may only be used or copied * in accordance with its terms, including the inclusion of the above copyright notice. * As this software is sold exclusively on CodeCanyon, * please review the full license details here: https://codecanyon.net/licenses/standard */ namespace App\Services; use App\Http\Resources\CategoryResource; use App\Http\Resources\EntityCollection; use App\Models\Category; use App\Services\Category\CategoryBy; use Illuminate\Http\JsonResponse; class CategoryService extends BaseService { use CategoryBy; /** * List categories * * @param int|null $parentId * @param array $params * @return \Illuminate\Http\JsonResponse */ public function getEntries(?int $parentId = null, array $params = []): JsonResponse { $cacheExpiration = $params['cacheExpiration'] ?? $this->cacheExpiration; $locale = config('app.locale'); $perPage = getNumberOfItemsPerPage('categories', $params['perPage'] ?? null, $this->perPage); $page = (int)($params['page'] ?? 1); $embed = getCommaSeparatedStrAsArray($params['embed'] ?? []); $areNestedEntriesIncluded = getIntAsBoolean($params['nestedIncluded'] ?? 0); $parentId = !empty($parentId) ? $parentId : ($params['parentId'] ?? null); $sort = $params['sort'] ?? []; // Cache ID $cacheNestedId = '.nestedIncluded.' . (int)$areNestedEntriesIncluded; $cacheEmbedId = !empty($embed) ? '.embed.' . implode(',', $embed) : ''; $cachePageId = '.page.' . $page . '.of.' . $perPage; $cacheId = 'cats.' . ((int)$parentId) . $cacheNestedId . $cacheEmbedId . $cachePageId . '.' . $locale; $cacheId = md5($cacheId); // Cached Query $categories = cache()->remember($cacheId, $cacheExpiration, function () use ( $perPage, $parentId, $embed, $areNestedEntriesIncluded, $sort ) { $categories = Category::query(); if (!empty($parentId)) { $categories->childrenOf($parentId); } else { if (!$areNestedEntriesIncluded) { $categories->root(); } } if (in_array('parent', $embed)) { $categories->with('parent'); } else { $categories->with('parentClosure'); } if (in_array('children', $embed)) { $categories->with('children'); } // Sorting $categories = $this->applySorting($categories, ['lft'], $sort); if ($areNestedEntriesIncluded) { $categories = $categories->get(); if ($categories->count() > 0) { $categories = $categories->keyBy('id'); } return $categories; } // Adding the withQueryString() to apply filters for AJAX request return $categories->paginate($perPage)->withQueryString(); }); // If the request is made from the app's Web environment, // use the Web URL as the pagination's base URL if (!$areNestedEntriesIncluded) { $categories = setPaginationBaseUrl($categories); } $resourceCollection = new EntityCollection(CategoryResource::class, $categories, $params); $message = ($categories->count() <= 0) ? t('no_categories_found') : null; return apiResponse()->withCollection($resourceCollection, $message); } /** * Get category * * Get category by its unique slug or ID. * * @param int|string $slugOrId * @param string|null $parentSlug * @param array $params * @return \Illuminate\Http\JsonResponse */ public function getEntry(int|string $slugOrId, ?string $parentSlug = null, array $params = []): JsonResponse { $category = is_numeric($slugOrId) ? $this->getCategoryById($slugOrId, $params) : $this->getCategoryBySlug($slugOrId, $parentSlug, $params); abort_if(empty($category), 404, t('category_not_found')); $resource = new CategoryResource($category, $params); return apiResponse()->withResource($resource); } }