-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from kg-bot/improved_filtering
Added where, limit and page methods
- Loading branch information
Showing
3 changed files
with
279 additions
and
147 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,172 @@ | ||
<?php | ||
|
||
|
||
namespace Rackbeat\Traits; | ||
|
||
|
||
trait ApiFiltering | ||
{ | ||
|
||
/** @var int[] */ | ||
protected $wheres = [ | ||
'limit' => 1000, | ||
'page' => 1 | ||
]; | ||
|
||
/** | ||
* @param array $filters | ||
* | ||
* @return string | ||
*/ | ||
protected function parseFilters( $filters = [] ) { | ||
|
||
foreach ( $filters as $filter ) { | ||
call_user_func_array( [ $this, 'where' ], array_values( $filter ) ); | ||
} | ||
|
||
|
||
$urlFilters = ''; | ||
|
||
if ( count( $this->wheres ) > 0 ) { | ||
$i = 1; | ||
|
||
$urlFilters .= '?'; | ||
|
||
foreach ( $this->wheres as $key => $filter ) { | ||
|
||
if ( ! is_array( $filter ) ) { | ||
$sign = '='; | ||
$value = $filter; | ||
} else { | ||
[ $sign, $value ] = $filter; | ||
} | ||
|
||
$urlFilters .= $key . $sign . urlencode( $value ); | ||
|
||
if ( count( $this->wheres ) > $i ) { | ||
|
||
$urlFilters .= '&'; | ||
} | ||
|
||
$i++; | ||
} | ||
} | ||
|
||
return $urlFilters; | ||
} | ||
|
||
/** | ||
* Search for resources that only match your criteria, this method can be used in multiple ways | ||
* Example 1: | ||
* ->where('name', 'Test') | ||
* | ||
* Example 2: | ||
* ->where('name', '=', 'Test') | ||
* | ||
* Example 3: | ||
* ->where('is_active') | ||
* | ||
* If you use third example it will be sent as `is_active=true` | ||
* | ||
* @param string $key | ||
* @param string|null $operator | ||
* @param mixed $value | ||
* | ||
* @return $this | ||
*/ | ||
public function where( string $key, string $operator = null, $value = null ): self { | ||
if ( func_num_args() === 1 ) { | ||
$value = true; | ||
|
||
$operator = '='; | ||
} | ||
|
||
if ( func_num_args() === 2 ) { | ||
$value = $operator; | ||
|
||
$operator = '='; | ||
} | ||
|
||
$this->wheres[ $key ] = [ $operator, $value ]; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* How many resources we should load (max 1000, min 1) | ||
* | ||
* @param int $limit | ||
* | ||
* @return $this | ||
*/ | ||
public function limit( $limit = 1000 ): self { | ||
$this->wheres['limit'] = $limit; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* @param int $page | ||
* | ||
* @return $this | ||
*/ | ||
public function page( $page = 1 ): self { | ||
$this->wheres['page'] = $page; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Set the return fields that you want | ||
* | ||
* @param array $fields | ||
* | ||
* @return $this | ||
*/ | ||
public function fields( array $fields = [] ): self { | ||
$this->wheres['fields'] = implode( ',', $fields ); | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* What should be expanded (loaded) in relation to requested resource (only field_values for now) | ||
* | ||
* @param string $expand | ||
* | ||
* @return $this | ||
*/ | ||
public function expand( string $expand = 'field_values' ): self { | ||
$this->wheres['expand'] = $expand; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Search for exact field match, if you need to use starting_with match use ->field($id, $value) | ||
* | ||
* @param int $id | ||
* @param $value | ||
* | ||
* @return $this | ||
*/ | ||
public function fieldEq( int $id, $value ): self { | ||
$this->wheres[ 'field_eq[' . $id . ']' ] = $value; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Search by field, this is not equal search so it can return more field, use ->fieldEq($id, $value) if you want to search for exact field | ||
* | ||
* @param int $id | ||
* @param $value | ||
* | ||
* @return $this | ||
*/ | ||
public function field( int $id, $value ): self { | ||
$this->wheres[ 'field[' . $id . ']' ] = $value; | ||
|
||
return $this; | ||
} | ||
} |
Oops, something went wrong.