Skip to content

Commit

Permalink
Fixed get() when not having any filters applied
Browse files Browse the repository at this point in the history
  • Loading branch information
kg-bot committed Apr 2, 2020
1 parent 6a460c3 commit e311bff
Showing 1 changed file with 12 additions and 78 deletions.
90 changes: 12 additions & 78 deletions src/Builders/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@

namespace KgBot\Magento\Builders;

use Illuminate\Support\Collection;
use KgBot\Magento\Exceptions\MagentoClientException;
use KgBot\Magento\Exceptions\MagentoRequestException;
use KgBot\Magento\Utils\Model;
use KgBot\Magento\Utils\Request;

Expand All @@ -19,15 +22,17 @@ class Builder
protected $model;
protected $request;

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

/**
* @param array $filters
*
* @return \Illuminate\Support\Collection|Model[]
* @return Collection|Model[]
* @throws MagentoClientException
* @throws MagentoRequestException
*/
public function get( $filters = [] )
{
Expand All @@ -37,28 +42,23 @@ public function get( $filters = [] )

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

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

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

$i = 0;

foreach ( $filters as $filter ) {
$urlFilters = '?searchCriteria';
foreach ($filters as $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' ];

$i++;
}
}

Expand All @@ -70,7 +70,7 @@ protected function parseResponse( $response )
$fetchedItems = collect( $response->items );
$items = collect( [] );

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


/** @var Model $model */
Expand Down Expand Up @@ -123,70 +123,4 @@ public function setEntity( $new_entity )

return $this->entity;
}

private function escapeFilter( $variable )
{
$escapedStrings = [
"$",
'(',
')',
'*',
'[',
']',
',',
];
$urlencodedStrings = [
'+',
' ',
];
foreach ( $escapedStrings as $escapedString ) {

$variable = str_replace( $escapedString, '$' . $escapedString, $variable );
}
foreach ( $urlencodedStrings as $urlencodedString ) {

$variable = str_replace( $urlencodedString, urlencode( $urlencodedString ), $variable );
}

return $variable;
}

private function switchComparison( $comparison )
{
switch ( $comparison ) {
case '=':
case '==':
$newComparison = '$eq:';
break;
case '!=':
$newComparison = '$ne:';
break;
case '>':
$newComparison = '$gt:';
break;
case '>=':
$newComparison = '$gte:';
break;
case '<':
$newComparison = '$lt:';
break;
case '<=':
$newComparison = '$lte:';
break;
case 'like':
$newComparison = '$like:';
break;
case 'in':
$newComparison = '$in:';
break;
case '!in':
$newComparison = '$nin:';
break;
default:
$newComparison = "${$comparison}:";
break;
}

return $newComparison;
}
}

0 comments on commit e311bff

Please sign in to comment.