From a6d7fe9fd6c9d6350d4bae0c8dcf02cadd23ccda Mon Sep 17 00:00:00 2001 From: Stefan Ninic Date: Wed, 27 Jan 2021 16:09:55 +0100 Subject: [PATCH] Allow multiple search criteria --- src/Builders/Builder.php | 158 +++++++++++++++++++-------------------- 1 file changed, 77 insertions(+), 81 deletions(-) diff --git a/src/Builders/Builder.php b/src/Builders/Builder.php index 1dc201d..f58acb3 100644 --- a/src/Builders/Builder.php +++ b/src/Builders/Builder.php @@ -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; + } } \ No newline at end of file