Skip to content

Commit

Permalink
Merge pull request #3 from kg-bot/multi_search
Browse files Browse the repository at this point in the history
Allow multiple search criteria
  • Loading branch information
kg-bot committed Jan 27, 2021
2 parents ee7fd8f + a6d7fe9 commit 16f7480
Showing 1 changed file with 77 additions and 81 deletions.
158 changes: 77 additions & 81 deletions src/Builders/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,111 +18,107 @@

class Builder
{
protected $entity;
/** @var Model */
protected $model;
protected $request;
protected $entity;
/** @var Model */
protected $model;
protected $request;

public function __construct(Request $request )
{
$this->request = $request;
}
public function __construct( Request $request ) {
$this->request = $request;
}

/**
* @param array $filters
*
* @return Collection|Model[]
* @throws MagentoClientException
* @throws MagentoRequestException
*/
public function get( $filters = [] )
{
$urlFilters = $this->parseFilters( $filters );
/**
* @param array $filters
*
* @return Collection|Model[]
* @throws MagentoClientException
* @throws MagentoRequestException
*/
public function get( $filters = [] ) {
$urlFilters = $this->parseFilters( $filters );

return $this->request->handleWithExceptions( function () use ( $urlFilters ) {
return $this->request->handleWithExceptions( function () use ( $urlFilters ) {

$response = $this->request->client->get( "{$this->entity}{$urlFilters}" );
$responseData = json_decode( (string) $response->getBody() );
$response = $this->request->client->get( "{$this->entity}{$urlFilters}" );
$responseData = json_decode( (string) $response->getBody() );

return $this->parseResponse($responseData);
} );
}
return $this->parseResponse( $responseData );
} );
}

protected function parseFilters( $filters )
{
$urlFilters = '?searchCriteria';
if (count($filters) > 0) {
foreach ($filters as $filter) {
protected function parseFilters( $filters ) {
$urlFilters = '?searchCriteria';
$count = count( $filters );
if ( $count > 0 ) {
foreach ( $filters as $index => $filter ) {

$urlFilters .= '[filter_groups][0][filters][0][field]=' . $filter['field'];
$urlFilters .= '&searchCriteria';
$urlFilters .= '[filter_groups][0][filters][0][value]=' . $filter['value'];
$urlFilters .= '&searchCriteria';
$urlFilters .= '[filter_groups][0][filters][0][condition_type]=' . $filter['condition_type'];
}
} else {
$urlFilters .= '=[]';
}
$conditionType = $filter['condition_type'] ?? 'eq';

return $urlFilters;
}
$urlFilters .= "[filter_groups][{$index}][filters][0][field]={$filter['field']}";
$urlFilters .= '&searchCriteria';
$urlFilters .= "[filter_groups][{$index}][filters][0][value]={$filter['value']}";
$urlFilters .= '&searchCriteria';
$urlFilters .= "[filter_groups][{$index}][filters][0][condition_type]={$conditionType}";
$urlFilters .= ( $count > 1 && ( $index < $count - 1 ) ) ? '&searchCriteria' : '';
}
} else {
$urlFilters .= '=[]';
}

protected function parseResponse( $response )
{
$fetchedItems = collect( $response->items );
$items = collect( [] );
return $urlFilters;
}

foreach ($fetchedItems as $index => $item ) {
protected function parseResponse( $response ) {
$fetchedItems = collect( $response->items );
$items = collect( [] );

foreach ( $fetchedItems as $index => $item ) {

/** @var Model $model */
$model = new $this->model( $this->request, $item );

$items->push( $model );
/** @var Model $model */
$model = new $this->model( $this->request, $item );

}
$items->push( $model );

return $items;
}
}

public function find( $id )
{
return $this->request->handleWithExceptions( function () use ( $id ) {
return $items;
}

$response = $this->request->client->get( "{$this->entity}/{$id}" );
$responseData = json_decode( (string) $response->getBody() );
public function find( $id ) {
return $this->request->handleWithExceptions( function () use ( $id ) {

return new $this->model( $this->request, $responseData );
} );
}
$response = $this->request->client->get( "{$this->entity}/{$id}" );
$responseData = json_decode( (string) $response->getBody() );

public function create( $data )
{
$data = [
Str::singular( $this->entity ) => $data,
];
return new $this->model( $this->request, $responseData );
} );
}

return $this->request->handleWithExceptions( function () use ( $data ) {
public function create( $data ) {
$data = [
Str::singular( $this->entity ) => $data,
];

$response = $this->request->client->post( "{$this->entity}", [
'json' => $data,
] );
return $this->request->handleWithExceptions( function () use ( $data ) {

$responseData = json_decode( (string) $response->getBody() );
$response = $this->request->client->post( "{$this->entity}", [
'json' => $data,
] );

return new $this->model( $this->request, $responseData );
} );
}
$responseData = json_decode( (string) $response->getBody() );

public function getEntity()
{
return $this->entity;
}
return new $this->model( $this->request, $responseData );
} );
}

public function setEntity( $new_entity )
{
$this->entity = $new_entity;
public function getEntity() {
return $this->entity;
}

return $this->entity;
}
public function setEntity( $new_entity ) {
$this->entity = $new_entity;

return $this->entity;
}
}

0 comments on commit 16f7480

Please sign in to comment.