Skip to content

Commit cfd9209

Browse files
committed
added option to use {{b64(some base64 encoded string)}} in values
1 parent 1baaaa2 commit cfd9209

File tree

2 files changed

+36
-17
lines changed

2 files changed

+36
-17
lines changed

README.md

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -44,27 +44,20 @@ class UserController extends Controller
4444

4545
# Documentation
4646

47-
### New in v1.1
47+
### New in v1.3
4848

49-
##### Chain multiple queries by and/or:
49+
##### Use base64 encoded values
5050
```
51-
.../model?filter[field]=like:*val1:and:like:val2*:or:null
51+
.../model?filter[field]=lt:{{b64(MjAxNy0wNy0yMiAyMzo1OTo1OQ==)}}
5252
```
5353
will result in:
5454
```
55-
SELECT * FROM models
56-
WHERE (
57-
field LIKE '%val1'
58-
AND field LIKE 'val2%'
59-
)
60-
OR field IS NULL
61-
```
62-
63-
##### Added Trait
64-
65-
You can now use Matthenning\EloquentApiFilter\Traits\FiltersEloquentApi to simply filter your requests with
66-
```
67-
$this->filterApiRequest($request, $query);
55+
SELECT
56+
*
57+
FROM
58+
models
59+
WHERE
60+
field < '2017-07-22 23:59:59'
6861
```
6962

7063
### URL Syntax
@@ -108,6 +101,10 @@ Join posts-relation on users
108101

109102
`.../users?with[]=posts`
110103

104+
Filter for a base64 encoded value
105+
106+
`.../model?filter[field]=lt:{{b64(MjAxNy0wNy0yMiAyMzo1OTo1OQ==)}}`
107+
111108
### Known issues
112109

113-
* Sorting by related fields doesn't work yet.
110+
* Sorting by related fields doesn't work yet.

src/Matthenning/EloquentApiFilter/EloquentApiFilter.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,8 @@ private function applyWhereClause(Builder $query, $field, $operator, $value, $or
209209
$null_verb = $or ? 'orWhereNull' : 'whereNull';
210210
$not_null_verb = $or ? 'orWhereNotNull' : 'whereNotNull';
211211

212+
$value = $this->base64decodeIfNecessary($value);
213+
212214
switch ($value) {
213215
case 'today':
214216
return $query->$verb($field, 'like', Carbon::now()->format('Y-m-d') . '%');
@@ -268,6 +270,7 @@ private function applyNestedOrder($relation_name, Builder $query, $relation_fiel
268270
*/
269271
private function applyOrderByClause(Builder $query, $field, $value)
270272
{
273+
$value = $this->base64decodeIfNecessary($value);
271274
return $query->orderBy($field, $value);
272275
}
273276

@@ -300,4 +303,23 @@ private function getFilterOperator($filter)
300303

301304
return $operator;
302305
}
306+
307+
/**
308+
* Searches for {{b64(some based 64 encoded string)}}
309+
* If found, returns the decoded content
310+
* If not, returns the original value
311+
*
312+
* @param $value
313+
* @return bool|string
314+
*/
315+
private function base64decodeIfNecessary($value)
316+
{
317+
preg_match("/\{\{b64\((.*)\)\}\}/", $value, $matches);
318+
if ($matches) {
319+
return base64_decode($matches[1]);
320+
}
321+
else {
322+
return $value;
323+
}
324+
}
303325
}

0 commit comments

Comments
 (0)