Skip to content

Commit

Permalink
fixing
Browse files Browse the repository at this point in the history
  • Loading branch information
soulaimaneyahya committed Mar 1, 2024
1 parent 4af964a commit e94da7a
Show file tree
Hide file tree
Showing 6 changed files with 113 additions and 10 deletions.
Empty file added .github/.gitkeep
Empty file.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,6 @@ Need helps? Reach us

> Multividas.com Ⓜ️
> Email: contact@multividas.com
> Email: multividasdotcom@gmail.com
🌌 🚀
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"authors": [
{
"name": "multividas inc",
"email": "contact@multividas.com"
"email": "multividasdotcom@gmail.com"
}
],
"autoload": {
Expand Down
83 changes: 75 additions & 8 deletions src/Repositories/QueryFiltersRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,46 @@

use Illuminate\Support\Collection;
use Illuminate\Pagination\LengthAwarePaginator;

use Illuminate\Http\Resources\Json\JsonResource;
use Multividas\ApiResponser\Traits\ApiResponser;

use Multividas\QueryFilters\Services\Caching\CacheService;
use Illuminate\Database\Eloquent\Collection as EloquentCollection;
use Multividas\QueryFilters\Interfaces\QueryFiltersRepositoryInterface;

class QueryFiltersRepository implements QueryFiltersRepositoryInterface
{
use ApiResponser;

private string $url;
private string $fullUrl;
private string|array|null $queryParams;
private string $queryString;

/**
* Method __construct
*
* @param private CacheService $cacheService
*
* @return void
*/
public function __construct(
private CacheService $cacheService
) {
$this->url = request()->url();
$this->queryParams = request()->query();
ksort($this->queryParams);
$this->queryString = http_build_query($this->queryParams);

$this->fullUrl = "{$this->url}?{$this->queryString}";
}

/**
* Method applyFilters
*
* @param Collection|EloquentCollection|JsonResource $collection
*
* @return array
*/
public function applyFilters(Collection|EloquentCollection|JsonResource $collection): array
{
if ($collection instanceof Collection || $collection instanceof EloquentCollection) {
Expand All @@ -39,8 +68,31 @@ public function applyFilters(Collection|EloquentCollection|JsonResource $collect
'meta' => $paginatedResult['meta'],
];
}

protected function filterData(

/**
* Method cacheData
*
* @param Collection|EloquentCollection|JsonResource $collection
*
* @return Collection|EloquentCollection|JsonResource
*/
public function cacheData(
Collection|EloquentCollection|JsonResource $collection
): Collection|EloquentCollection|JsonResource {
return $this->cacheService->remember($this->fullUrl, now()->addSeconds(60), function () use ($collection) {
return $collection;
});
}

/**
* Method filterData
*
* @param Collection|EloquentCollection|JsonResource $collection
* @param ?string $transformer
*
* @return Collection|EloquentCollection|JsonResource
*/
public function filterData(
Collection|EloquentCollection|JsonResource $collection,
?string $transformer
): Collection|EloquentCollection|JsonResource {
Expand All @@ -58,8 +110,16 @@ protected function filterData(

return $collection;
}

protected function sortData(

/**
* Method sortData
*
* @param Collection|EloquentCollection|JsonResource $collection
* @param ?string $transformer
*
* @return Collection|EloquentCollection|JsonResource
*/
public function sortData(
Collection|EloquentCollection|JsonResource $collection,
?string $transformer
): Collection|EloquentCollection|JsonResource {
Expand All @@ -77,8 +137,15 @@ protected function sortData(

return $collection;
}

protected function paginateData(Collection|EloquentCollection|JsonResource $collection): array

/**
* Method paginateData
*
* @param Collection|EloquentCollection|JsonResource $collection
*
* @return array
*/
public function paginateData(Collection|EloquentCollection|JsonResource $collection): array
{
$rules = [
'per_page' => ['integer', 'min:2', 'max:50'],
Expand Down
Empty file added src/Services/.gitkeep
Empty file.
36 changes: 36 additions & 0 deletions src/Services/Caching/CacheService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

/**
* (c) 2023 Multividas inc. All rights reserved.
* Unauthorized use prohibited.
* Website: https://www.multividas.com
*/

namespace Multividas\QueryFilters\Services\Caching;

use Illuminate\Cache\Repository;

class CacheService
{
public function __construct(
private Repository $cacheRepository
) {
}

public function remember(string $key, \Closure|\DateTimeInterface|\DateInterval|int|null $ttl, \Closure $callback)
{
return $this->cacheRepository->remember($key, $ttl, $callback);
}

/**
* Method forget
*
* @param string $key
*
* @return bool
*/
public function forget(string $key): bool
{
return $this->cacheRepository->forget($key);
}
}

0 comments on commit e94da7a

Please sign in to comment.