Skip to content

Commit 014b75e

Browse files
author
oooiik
committed
update structure
1 parent 9ba908b commit 014b75e

File tree

3 files changed

+132
-61
lines changed

3 files changed

+132
-61
lines changed

src/Filters/QueryFilter.php

+3-56
Original file line numberDiff line numberDiff line change
@@ -2,75 +2,22 @@
22

33
namespace Oooiik\LaravelQueryFilter\Filters;
44

5-
use http\Exception\BadMethodCallException;
65
use Illuminate\Database\Eloquent\Builder;
76

87
abstract class QueryFilter
98
{
109
/** @var Builder */
11-
protected $builder;
12-
/** @var Builder */
13-
protected $realBuilder;
10+
public $builder;
1411

1512
/**
1613
* Here, default parameters for functions are saved
1714
* @var array
1815
*/
19-
public array $default = [];
16+
public $default = [];
2017

2118
/**
2219
* Here, if a function does not work, a helper function is shown for it
2320
* @var array
2421
*/
25-
public array $fallback = [];
26-
27-
public function __construct(Builder $builder)
28-
{
29-
$this->realBuilder = $builder;
30-
$this->builder = clone $this->realBuilder;
31-
}
32-
33-
public static function builder(Builder $builder)
34-
{
35-
return new static($builder);
36-
}
37-
38-
public function filters()
39-
{
40-
$staticClassMethods = get_class_methods(static::class);
41-
$selfClassMethods = get_class_methods(self::class);
42-
return array_diff($staticClassMethods, $selfClassMethods);
43-
}
44-
45-
public function apply(array $validated)
46-
{
47-
$validatedKeys = array_keys($validated);
48-
$defaultKeys = array_keys($this->default);
49-
$fallbackKeys = array_keys($this->fallback);
50-
51-
foreach ($this->filters() as $filter) {
52-
if (in_array($filter, $validatedKeys)) {
53-
$this->$filter($validated[$filter], $validated);
54-
} elseif (in_array($filter, $defaultKeys)) {
55-
$this->$filter($this->default[$filter], $validated);
56-
} elseif (in_array($filter, $fallbackKeys)) {
57-
if(!in_array($this->fallback[$filter], $this->filters())){
58-
throw new \BadMethodCallException("This method not found!", 500);
59-
}
60-
$this->{$this->fallback[$filter]}(null, $validated);
61-
}
62-
}
63-
return $this;
64-
}
65-
66-
public function resetApply(array $validated)
67-
{
68-
$this->builder = clone $this->realBuilder;
69-
return $this->apply($validated);
70-
}
71-
72-
public function query()
73-
{
74-
return $this->builder;
75-
}
22+
public $fallback = [];
7623
}

src/Services/FilterService.php

+123
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
<?php
2+
3+
namespace Oooiik\LaravelQueryFilter\Services;
4+
5+
use Illuminate\Database\Eloquent\Builder;
6+
use Oooiik\LaravelQueryFilter\Filters\QueryFilter;
7+
8+
class FilterService
9+
{
10+
protected $client;
11+
12+
protected $baseBuilder;
13+
14+
protected $params = [];
15+
16+
/**
17+
* @param QueryFilter|string $client
18+
* @param Builder $builder
19+
*/
20+
public function __construct($client, Builder $builder)
21+
{
22+
if(is_string($client)){
23+
$client = new $client();
24+
}
25+
$this->client = $client;
26+
$this->baseBuilder = $builder;
27+
$this->getClient()->builder = clone $this->baseBuilder;
28+
}
29+
30+
protected function getBaseBuilder()
31+
{
32+
return $this->baseBuilder;
33+
}
34+
35+
protected function getClient()
36+
{
37+
return $this->client;
38+
}
39+
40+
protected function getClientBuilder()
41+
{
42+
return $this->getClient()->builder;
43+
}
44+
45+
protected function getClientMethods()
46+
{
47+
return get_class_methods($this->getClient());
48+
}
49+
50+
protected function getClientDefault(string $key = null)
51+
{
52+
if (empty($key)) {
53+
return $this->getClient()->default ?? [];
54+
} else {
55+
return $this->getClientDefault()[$key] ?? null;
56+
}
57+
}
58+
59+
protected function getClientFallback(string $key = null)
60+
{
61+
if ($key === null) {
62+
return $this->getClient()->fallback ?? [];
63+
} else {
64+
return $this->getClientFallback()[$key] ?? null;
65+
}
66+
}
67+
68+
public function query()
69+
{
70+
return $this->getClientBuilder();
71+
}
72+
73+
protected function setParams($params)
74+
{
75+
$this->params = array_merge($this->getClientDefault(), $params);
76+
}
77+
78+
protected function getParam($key = null)
79+
{
80+
if ($key === null) {
81+
return $this->params;
82+
} else {
83+
return $this->params[$key] ?? null;
84+
}
85+
}
86+
87+
protected function callMethod($method)
88+
{
89+
if (!in_array($method, $this->getClientMethods())) {
90+
throw new \BadMethodCallException("This method is not found in fallback!", 500);
91+
}
92+
call_user_func([$this->getClient(), $method], $this->getParam($method), $this->getParam());
93+
}
94+
95+
protected function filterApply($method)
96+
{
97+
$paramsKeys = array_keys($this->getParam());
98+
$fallbackKeys = array_keys($this->getClientFallback());
99+
100+
if (in_array($method, $paramsKeys)) {
101+
$this->callMethod($method);
102+
} elseif (in_array($method, $fallbackKeys)) {
103+
$this->callMethod($this->getClientFallback($method));
104+
}
105+
}
106+
107+
public function apply(array $params = [])
108+
{
109+
$this->setParams($params);
110+
111+
foreach ($this->getClientMethods() as $method) {
112+
$this->filterApply($method);
113+
}
114+
115+
return $this;
116+
}
117+
118+
public function resetApply()
119+
{
120+
$this->getClient()->builder = clone $this->getBaseBuilder();
121+
return $this;
122+
}
123+
}

src/Traits/Model/Filterable.php

+6-5
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Illuminate\Database\Eloquent\Builder;
66
use Oooiik\LaravelQueryFilter\Filters\QueryFilter;
7+
use Oooiik\LaravelQueryFilter\Services\FilterService;
78
use Symfony\Component\ErrorHandler\Error\ClassNotFoundError;
89

910
/**
@@ -14,20 +15,20 @@ trait Filterable
1415
{
1516
// protected $defaultFilter;
1617

17-
public function scopeFilter(Builder $query, array $validated = [])
18+
public function scopeFilter(Builder $query, array $params = [])
1819
{
1920
if (!class_exists($this->defaultFilter)) {
2021
throw new ClassNotFoundError('Class not found', 500);
2122
}
2223
if (!is_subclass_of($this->defaultFilter, QueryFilter::class)) {
2324
throw new ClassNotFoundError('It is not a successor class of Filter', 500);
2425
}
25-
return $this->defaultFilter::builder($query)->apply($validated)->query();
26+
return (new FilterService( $this->defaultFilter, $query))->apply($params)->query();
2627
}
2728

2829
/**
29-
* @param string $filter
30-
* @return QueryFilter
30+
* @param QueryFilter|string $filter
31+
* @return FilterService
3132
*/
3233
public static function createFilter($filter)
3334
{
@@ -38,6 +39,6 @@ public static function createFilter($filter)
3839
throw new ClassNotFoundError('It is not a successor class of Filter', 500);
3940
}
4041
$query = self::query();
41-
return $filter::builder($query);
42+
return new FilterService($filter, $query);
4243
}
4344
}

0 commit comments

Comments
 (0)