Skip to content

Commit

Permalink
Merge pull request #1 from multividas/FIXING
Browse files Browse the repository at this point in the history
fixing
  • Loading branch information
xtxt22 authored Jul 1, 2024
2 parents ba8248b + 436c053 commit f5cdfcd
Show file tree
Hide file tree
Showing 7 changed files with 167 additions and 87 deletions.
22 changes: 22 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
root = true

[*]
charset = utf-8
end_of_line = lf
indent_size = 4
indent_style = space
insert_final_newline = true
trim_trailing_whitespace = true

[*.{js,ts,vue,json,lock}]
indent_size = 2

[*.md]
indent_size = 2
trim_trailing_whitespace = false

[*.{yml,yaml}]
indent_size = 2

[docker-compose.yml]
indent_size = 4
6 changes: 6 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
* text=auto eol=lf

*.md diff=markdown
*.php diff=php

/.github export-ignore
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ Composer package to simplify the process of filtering and sorting queries
---

## Installation

Require this package with composer.

```shell
Expand Down
114 changes: 57 additions & 57 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,60 +1,60 @@
{
"name": "multividas/query-filters",
"type": "package",
"license": "MIT",
"description": "Composer package to simplify the process of filtering and sorting queries",
"keywords": [
"query-filters"
],
"authors": [
{
"name": "Multividas",
"email": "multividasdotcom@gmail.com"
}
],
"autoload": {
"psr-4": {
"Multividas\\QueryFilters\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"Multividas\\QueryFilters\\Tests\\": "tests/"
}
},
"scripts": {
"test": "vendor/bin/phpunit tests/Feature",
"php-psr": [
"find src -type f -name \"*.php\" -print0 | xargs -0 -n1 php -lf",
"vendor/bin/php-cs-fixer fix --allow-risky=yes src --rules=declare_strict_types,@PSR12",
"./vendor/bin/phpcs --standard=PSR2 --encoding=utf-8 --extensions=php src/*"
]
},
"require": {
"php": "^8.2"
},
"require-dev": {
"squizlabs/php_codesniffer": "4.0.x-dev",
"phpunit/phpunit": "9.6.x-dev",
"orchestra/testbench": "8.x-dev",
"friendsofphp/php-cs-fixer": "dev-master"
},
"minimum-stability": "stable",
"config": {
"optimize-autoloader": true
},
"extra": {
"laravel": {
"providers": [
"Multividas\\QueryFilters\\Providers\\QueryFiltersServiceProvider"
],
"aliases": {
"QueryFilters": "Multividas\\QueryFilters\\Facades\\QueryFilters"
}
}
},
"support": {
"issues": "https://github.com/multividas/query-filters/issues",
"source": "https://github.com/multividas/query-filters"
"name": "multividas/query-filters",
"type": "package",
"license": "MIT",
"description": "Composer package to simplify the process of filtering and sorting queries",
"keywords": [
"query-filters"
],
"authors": [
{
"name": "Multividas",
"email": "multividasdotcom@gmail.com"
}
],
"autoload": {
"psr-4": {
"Multividas\\QueryFilters\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"Multividas\\QueryFilters\\Tests\\": "tests/"
}
},
"scripts": {
"test": "vendor/bin/phpunit tests/Feature",
"php-psr": [
"find src -type f -name \"*.php\" -print0 | xargs -0 -n1 php -lf",
"vendor/bin/php-cs-fixer fix --allow-risky=yes src --rules=declare_strict_types,@PSR12",
"./vendor/bin/phpcs --standard=PSR2 --encoding=utf-8 --extensions=php src/*"
]
},
"require": {
"php": "^8.2"
},
"require-dev": {
"squizlabs/php_codesniffer": "4.0.x-dev",
"phpunit/phpunit": "9.6.x-dev",
"orchestra/testbench": "8.x-dev",
"friendsofphp/php-cs-fixer": "dev-master"
},
"minimum-stability": "stable",
"config": {
"optimize-autoloader": true
},
"extra": {
"laravel": {
"providers": [
"Multividas\\QueryFilters\\Providers\\QueryFiltersServiceProvider"
],
"aliases": {
"QueryFilters": "Multividas\\QueryFilters\\Facades\\QueryFilters"
}
}
},
"support": {
"issues": "https://github.com/multividas/query-filters/issues",
"source": "https://github.com/multividas/query-filters"
}
}
8 changes: 4 additions & 4 deletions src/Interfaces/QueryFiltersRepositoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@

interface QueryFiltersRepositoryInterface
{
public function listAll(
Collection|EloquentCollection|JsonResource $collection
): Collection|EloquentCollection|JsonResource;

public function applyFilters(Collection|EloquentCollection|JsonResource $collection): array|JsonResponse;

public function filterData(
Expand All @@ -29,9 +33,5 @@ public function sortData(
?string $transformer
): Collection|EloquentCollection|JsonResource;

public function cacheData(
Collection|EloquentCollection|JsonResource $collection
): Collection|EloquentCollection|JsonResource;

public function paginateData(Collection|EloquentCollection|JsonResource $collection): array;
}
47 changes: 21 additions & 26 deletions src/Repositories/QueryFiltersRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,34 +11,44 @@
namespace Multividas\QueryFilters\Repositories;

use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Cache;
use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Http\Resources\Json\JsonResource;
use Multividas\QueryFilters\Services\CachingService;
use Illuminate\Database\Eloquent\Collection as EloquentCollection;
use Multividas\QueryFilters\Interfaces\QueryFiltersRepositoryInterface;

class QueryFiltersRepository implements QueryFiltersRepositoryInterface
{
private string $url;
private string $fullUrl;
private string $queryString;
private string|array|null $queryParams;

/**
* Method __construct
*
* @return void
*/
public function __construct()
{
$this->url = request()->url();

$this->queryParams = request()->query();
ksort($this->queryParams);
}

/**
* Method listAll
*
* @param Collection|EloquentCollection|JsonResource $collection
*
* @return array
*/
public function listAll(Collection|EloquentCollection|JsonResource $collection): Collection|EloquentCollection|JsonResource
{
if ($collection instanceof Collection || $collection instanceof EloquentCollection) {
$transformer = $collection->first()?->transformer ?? null;
} elseif ($collection instanceof JsonResource) {
$transformer = $collection->collects ?? null;
}

$this->queryString = http_build_query($this->queryParams);
$collection = $this->filterData($collection, $transformer);
$collection = $this->sortData($collection, $transformer);
$cachedData = app(CachingService::class)->cacheData($collection);

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

/**
Expand Down Expand Up @@ -121,21 +131,6 @@ public function sortData(
return $collection;
}

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

/**
* Method paginateData
*
Expand Down
56 changes: 56 additions & 0 deletions src/Services/CachingService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

declare(strict_types=1);

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

namespace Multividas\QueryFilters\Services;

use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Cache;
use Illuminate\Http\Resources\Json\JsonResource;
use Illuminate\Database\Eloquent\Collection as EloquentCollection;

class CachingService
{
private string $url;
private string $fullUrl;
private string $queryString;
private string|array|null $queryParams;

/**
* Method __construct
*
* @return void
*/
public function __construct()
{
$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 cacheData
*
* @param Collection|EloquentCollection|JsonResource $collection
*
* @return Collection|EloquentCollection|JsonResource
*/
public function cacheData(
Collection|EloquentCollection|JsonResource $collection
): Collection|EloquentCollection|JsonResource {
return Cache::remember($this->fullUrl, now()->addSeconds(60), function () use ($collection) {
return $collection;
});
}
}

0 comments on commit f5cdfcd

Please sign in to comment.