Skip to content

Commit

Permalink
Merge pull request #7 from kg-bot/improved_filtering
Browse files Browse the repository at this point in the history
Improved filtering
  • Loading branch information
kg-bot committed Oct 9, 2020
2 parents b2954b5 + b8e7290 commit 6edef16
Showing 1 changed file with 81 additions and 9 deletions.
90 changes: 81 additions & 9 deletions src/Traits/ApiFiltering.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
namespace Rackbeat\Traits;


use Rackbeat\Utils\Model;

trait ApiFiltering
{

Expand All @@ -18,7 +20,7 @@ trait ApiFiltering
*
* @return string
*/
protected function parseFilters( $filters = [] ) {
protected function parseFilters( array $filters = [] ): string {

foreach ( $filters as $filter ) {
call_user_func_array( [ $this, 'where' ], array_values( $filter ) );
Expand Down Expand Up @@ -99,8 +101,8 @@ public function where( string $key, string $operator = null, $value = null ): se
*
* @return $this
*/
public function limit( $limit = 1000 ): self {
$this->wheres['limit'] = $limit;
public function limit( int $limit = 1000 ): self {
$this->where( 'limit', $limit );

return $this;
}
Expand All @@ -110,8 +112,8 @@ public function limit( $limit = 1000 ): self {
*
* @return $this
*/
public function page( $page = 1 ): self {
$this->wheres['page'] = $page;
public function page( int $page = 1 ): self {
$this->where( 'page', $page );

return $this;
}
Expand All @@ -124,7 +126,7 @@ public function page( $page = 1 ): self {
* @return $this
*/
public function fields( array $fields = [] ): self {
$this->wheres['fields'] = implode( ',', $fields );
$this->where( 'fields', implode( ',', $fields ) );

return $this;
}
Expand All @@ -137,7 +139,7 @@ public function fields( array $fields = [] ): self {
* @return $this
*/
public function expand( string $expand = 'field_values' ): self {
$this->wheres['expand'] = $expand;
$this->where( 'expand', $expand );

return $this;
}
Expand All @@ -151,7 +153,7 @@ public function expand( string $expand = 'field_values' ): self {
* @return $this
*/
public function fieldEq( int $id, $value ): self {
$this->wheres[ 'field_eq[' . $id . ']' ] = $value;
$this->where( 'field_eq[' . $id . ']', $value );

return $this;
}
Expand All @@ -165,8 +167,78 @@ public function fieldEq( int $id, $value ): self {
* @return $this
*/
public function field( int $id, $value ): self {
$this->wheres[ 'field[' . $id . ']' ] = $value;
$this->where( 'field[' . $id . ']', $value );

return $this;
}

/**
* @param string $orderBy
*
* @return $this
*/
public function orderBy( string $orderBy ): self {
$this->where( 'order_by', $orderBy );

return $this;
}

/**
* @param string $direction
*
* @return $this
*/
public function orderDirection( string $direction = 'DESC' ): self {
$this->where( 'order_direction', $direction );

return $this;
}

/**
* @return $this
*/
public function orderDesc(): self {
$this->orderDirection( 'DESC' );

return $this;
}

/**
* @return $this
*/
public function orderAsc(): self {
$this->orderDirection( 'ASC' );

return $this;
}

/**
* Get first item ordered by desired field (or created_at by default)
*
* @param string $orderBy
*
* @return Model|null
*/
public function first( string $orderBy = 'created_at' ): ?Model {
$this->limit( 1 );
$this->orderBy( $orderBy );
$this->orderAsc();

return $this->get()->first();
}

/**
* Get last created item ordered by desired field (or created_at by default)
*
* @param string $orderBy
*
* @return Model|null
*/
public function last( string $orderBy = 'created_at' ): ?Model {
$this->limit( 1 );
$this->orderBy( $orderBy );
$this->orderDesc();

return $this->get()->first();
}
}

0 comments on commit 6edef16

Please sign in to comment.