Skip to content

Commit

Permalink
Added fields, expand, field and fieldEq filters, limit for all() is a…
Browse files Browse the repository at this point in the history
…lways 1000
  • Loading branch information
kg-bot committed Oct 9, 2020
1 parent 4f667b6 commit 76fea8b
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/Builders/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ protected function populateModelsFromResponse( $response ) {
*/
public function all( $filters = [] ) {
$page = 1;
$this->limit( 1000 );

$items = collect();

Expand Down
70 changes: 69 additions & 1 deletion src/Traits/ApiFiltering.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ trait ApiFiltering
protected function parseFilters( $filters = [] ) {

foreach ( $filters as $filter ) {
$this->where( array_values( $filter ) );
call_user_func_array( [ $this, 'where' ], array_values( $filter ) );
}


Expand Down Expand Up @@ -56,6 +56,18 @@ protected function parseFilters( $filters = [] ) {
}

/**
* 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
Expand All @@ -81,6 +93,8 @@ public function where( string $key, string $operator = null, $value = null ): se
}

/**
* How many resources we should load (max 1000, min 1)
*
* @param int $limit
*
* @return $this
Expand All @@ -101,4 +115,58 @@ public function page( $page = 1 ): self {

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;
}
}

0 comments on commit 76fea8b

Please sign in to comment.