Skip to content

Commit

Permalink
added option to use {{b64(some base64 encoded string)}} in values
Browse files Browse the repository at this point in the history
  • Loading branch information
matthenning committed Jul 22, 2017
1 parent 1baaaa2 commit cfd9209
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 17 deletions.
31 changes: 14 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,27 +44,20 @@ class UserController extends Controller

# Documentation

### New in v1.1
### New in v1.3

##### Chain multiple queries by and/or:
##### Use base64 encoded values
```
.../model?filter[field]=like:*val1:and:like:val2*:or:null
.../model?filter[field]=lt:{{b64(MjAxNy0wNy0yMiAyMzo1OTo1OQ==)}}
```
will result in:
```
SELECT * FROM models
WHERE (
field LIKE '%val1'
AND field LIKE 'val2%'
)
OR field IS NULL
```

##### Added Trait

You can now use Matthenning\EloquentApiFilter\Traits\FiltersEloquentApi to simply filter your requests with
```
$this->filterApiRequest($request, $query);
SELECT
*
FROM
models
WHERE
field < '2017-07-22 23:59:59'
```

### URL Syntax
Expand Down Expand Up @@ -108,6 +101,10 @@ Join posts-relation on users

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

Filter for a base64 encoded value

`.../model?filter[field]=lt:{{b64(MjAxNy0wNy0yMiAyMzo1OTo1OQ==)}}`

### Known issues

* Sorting by related fields doesn't work yet.
* Sorting by related fields doesn't work yet.
22 changes: 22 additions & 0 deletions src/Matthenning/EloquentApiFilter/EloquentApiFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,8 @@ private function applyWhereClause(Builder $query, $field, $operator, $value, $or
$null_verb = $or ? 'orWhereNull' : 'whereNull';
$not_null_verb = $or ? 'orWhereNotNull' : 'whereNotNull';

$value = $this->base64decodeIfNecessary($value);

switch ($value) {
case 'today':
return $query->$verb($field, 'like', Carbon::now()->format('Y-m-d') . '%');
Expand Down Expand Up @@ -268,6 +270,7 @@ private function applyNestedOrder($relation_name, Builder $query, $relation_fiel
*/
private function applyOrderByClause(Builder $query, $field, $value)
{
$value = $this->base64decodeIfNecessary($value);
return $query->orderBy($field, $value);
}

Expand Down Expand Up @@ -300,4 +303,23 @@ private function getFilterOperator($filter)

return $operator;
}

/**
* Searches for {{b64(some based 64 encoded string)}}
* If found, returns the decoded content
* If not, returns the original value
*
* @param $value
* @return bool|string
*/
private function base64decodeIfNecessary($value)
{
preg_match("/\{\{b64\((.*)\)\}\}/", $value, $matches);
if ($matches) {
return base64_decode($matches[1]);
}
else {
return $value;
}
}
}

0 comments on commit cfd9209

Please sign in to comment.