Skip to content

Commit 8d7e2f0

Browse files
committed
Add a helper class for creating filters
1 parent 94eefbb commit 8d7e2f0

File tree

4 files changed

+141
-1
lines changed

4 files changed

+141
-1
lines changed
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<?php
2+
3+
4+
namespace BigCommerce\ApiV3\Api;
5+
6+
7+
class AttributeFilter
8+
{
9+
public const GREATER_THAN_OR_EQUAL = 'min';
10+
public const LESS_THAN_OR_EQUAL = 'max';
11+
public const GREATER_THAN = 'greater';
12+
public const LESS_THAN = 'less';
13+
public const LIKE = 'like';
14+
public const IN = 'in';
15+
public const NOT_IN = 'not_in';
16+
17+
private static $validFilters = [
18+
self::GREATER_THAN_OR_EQUAL,
19+
self::LESS_THAN_OR_EQUAL,
20+
self::GREATER_THAN,
21+
self::LESS_THAN,
22+
self::LIKE,
23+
self::IN,
24+
self::NOT_IN,
25+
];
26+
27+
public static function in(string $attribute, array $values): array
28+
{
29+
return self::createFilterForMultiple($attribute, $values, self::IN);
30+
}
31+
32+
public static function greaterThanOrEqual(string $attribute, float $value): array
33+
{
34+
return self::createFilter($attribute, $value, self::GREATER_THAN_OR_EQUAL);
35+
}
36+
37+
public static function lessThanOrEqual(string $attribute, float $value): array
38+
{
39+
return self::createFilter($attribute, $value, self::LESS_THAN_OR_EQUAL);
40+
}
41+
42+
public static function greaterThan(string $attribute, float $value): array
43+
{
44+
return self::createFilter($attribute, $value, self::GREATER_THAN);
45+
}
46+
47+
public static function lessThan(string $attribute, float $value): array
48+
{
49+
return self::createFilter($attribute, $value, self::LESS_THAN);
50+
}
51+
52+
public static function like(string $attribute, string $value): array
53+
{
54+
return self::createFilter($attribute, $value, self::LIKE);
55+
}
56+
57+
public static function createFilter(string $attribute, string $value, string $filterType): array
58+
{
59+
if (!self::isValidFilter($filterType)) throw new \InvalidArgumentException("");
60+
61+
return ["$attribute:$filterType" => $value];
62+
}
63+
64+
public static function createFilterForMultiple(string $attribute, array $values, string $filterType): array
65+
{
66+
return self::createFilter($attribute, implode(',', $values), $filterType);
67+
}
68+
69+
private static function isValidFilter(string $filterType): bool
70+
{
71+
return in_array($filterType, self::$validFilters);
72+
}
73+
}

src/BigCommerce/Catalog/ProductsApi.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace BigCommerce\ApiV3\Catalog;
44

5+
use BigCommerce\ApiV3\Api\AttributeFilter;
56
use BigCommerce\ApiV3\Api\ResourceWithBatchUpdateApi;
67
use BigCommerce\ApiV3\Catalog\Products\ProductsSubResourceApi;
78
use BigCommerce\ApiV3\ResourceModels\Catalog\Product\Product;
@@ -76,7 +77,7 @@ public function batchDelete(array $productIds): bool
7677
$this->getClient()->getRestClient()->delete(
7778
$this->multipleResourcesEndpoint(),
7879
[
79-
RequestOptions::QUERY => ['id:in' => implode(',', $productIds)],
80+
RequestOptions::QUERY => AttributeFilter::in('id', $productIds),
8081
]
8182
);
8283

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
3+
namespace BigCommerce\Tests\Api;
4+
5+
use BigCommerce\ApiV3\Api\AttributeFilter;
6+
use PHPUnit\Framework\TestCase;
7+
8+
class AttributeFilterTest extends TestCase
9+
{
10+
public function testCanBuildInFilter(): void
11+
{
12+
$filter = AttributeFilter::in('id', [1,2,3]);
13+
$this->assertEquals(['id:in' => '1,2,3'], $filter);
14+
}
15+
16+
public function testCanBuildGreaterThanOrEqualFilter(): void
17+
{
18+
$filter = AttributeFilter::greaterThanOrEqual('price', 100);
19+
$this->assertEquals(['price:min' => 100], $filter);
20+
}
21+
22+
public function testCanBuildLessThanOrEqualFilter(): void
23+
{
24+
$filter = AttributeFilter::lessThanOrEqual('price', 100);
25+
$this->assertEquals(['price:max' => 100], $filter);
26+
}
27+
28+
public function testCanBuildLessThanFilter(): void
29+
{
30+
$filter = AttributeFilter::lessThan('price', 100);
31+
$this->assertEquals(['price:less' => 100], $filter);
32+
}
33+
34+
public function testCanBuildGreaterThanFilter(): void
35+
{
36+
$filter = AttributeFilter::greaterThan('price', 100);
37+
$this->assertEquals(['price:greater' => 100], $filter);
38+
}
39+
40+
public function testCanBuildLikeFilter(): void
41+
{
42+
$filter = AttributeFilter::like('sku', 'abc');
43+
$this->assertEquals(['sku:like' => 'abc'], $filter);
44+
}
45+
46+
public function testCannotBuildInvalidFilter(): void
47+
{
48+
$this->expectException(\InvalidArgumentException::class);
49+
50+
AttributeFilter::createFilter('something', '', 'somewhere');
51+
}
52+
}

tests/BigCommerce/Catalog/ProductsApiTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,18 @@ public function testCanGetAllPagesForProducts(): void
4141
$productsResponse = $this->getApi()->catalog()->products()->getAllPages();
4242
$this->assertEquals(2, $productsResponse->getPagination()->total);
4343
}
44+
45+
public function testCanBatchDeleteProducts(): void
46+
{
47+
$this->setReturnData('blank.json', 204);
48+
$response = $this->getApi()->catalog()->products()->batchDelete([1,2,3]);
49+
$this->assertTrue($response);
50+
}
51+
52+
public function testCanFailToDeleteBatchDeleteProducts(): void
53+
{
54+
$this->setReturnData(self::EMPTY_RESPONSE, 400);
55+
$response = $this->getApi()->catalog()->products()->batchDelete([1,2,3]);
56+
$this->assertFalse($response);
57+
}
4458
}

0 commit comments

Comments
 (0)