diff --git a/README.md b/README.md index af0c0f6..c1f4770 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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. \ No newline at end of file diff --git a/src/Matthenning/EloquentApiFilter/EloquentApiFilter.php b/src/Matthenning/EloquentApiFilter/EloquentApiFilter.php index 55cf227..737049c 100644 --- a/src/Matthenning/EloquentApiFilter/EloquentApiFilter.php +++ b/src/Matthenning/EloquentApiFilter/EloquentApiFilter.php @@ -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') . '%'); @@ -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); } @@ -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; + } + } } \ No newline at end of file