Skip to content

Commit 48ae50a

Browse files
committed
Implement Catalog/Categories/GetCategoryTree
1 parent 8d7e2f0 commit 48ae50a

File tree

5 files changed

+161
-0
lines changed

5 files changed

+161
-0
lines changed

src/BigCommerce/Catalog/CategoriesApi.php

+12
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,15 @@
88
use BigCommerce\ApiV3\ResourceModels\Catalog\Category\Category;
99
use BigCommerce\ApiV3\ResponseModels\Category\CategoriesResponse;
1010
use BigCommerce\ApiV3\ResponseModels\Category\CategoryResponse;
11+
use BigCommerce\ApiV3\ResponseModels\Category\CategoryTreeResponse;
12+
use GuzzleHttp\RequestOptions;
1113

1214
class CategoriesApi extends ResourceApi
1315
{
1416
private const RESOURCE_NAME = 'categories';
1517
private const CATEGORIES_ENDPOINT = 'catalog/categories';
1618
private const CATEGORY_ENDPOINT = 'catalog/categories/%d';
19+
private const CATEGORY_TREE_ENDPOINT = 'catalog/categories/tree';
1720

1821
public function image(): CategoryImageApi
1922
{
@@ -30,6 +33,15 @@ public function getAll(array $filters = [], int $page = 1, int $limit = 250): Ca
3033
return new CategoriesResponse($this->getAllResources($filters, $page, $limit));
3134
}
3235

36+
public function getCategoryTree(): CategoryTreeResponse
37+
{
38+
$response = $this->getClient()->getRestClient()->get(
39+
self::CATEGORY_TREE_ENDPOINT
40+
);
41+
42+
return new CategoryTreeResponse($response);
43+
}
44+
3345
public function getAllPages(array $filter = []): CategoriesResponse
3446
{
3547
return CategoriesResponse::buildFromAllPages(function ($page) use ($filter) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
4+
namespace BigCommerce\ApiV3\ResourceModels\Catalog\Category;
5+
6+
7+
use BigCommerce\ApiV3\ResourceModels\ResourceModel;
8+
use stdClass;
9+
10+
class CategoryTreeBranch extends ResourceModel
11+
{
12+
public int $id;
13+
public int $parent_id;
14+
public string $name;
15+
public bool $is_visible;
16+
public string $url;
17+
18+
/**
19+
* @var CategoryTreeBranch[]
20+
*/
21+
public array $children = [];
22+
23+
public function __construct(?stdClass $optionObject = null)
24+
{
25+
foreach ($optionObject->children as $categoryData) {
26+
$this->children[] = $categoryData instanceof CategoryTreeBranch ? $categoryData : new CategoryTreeBranch($categoryData);
27+
}
28+
29+
unset($optionObject->children);
30+
parent::__construct($optionObject);
31+
}
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
4+
namespace BigCommerce\ApiV3\ResponseModels\Category;
5+
6+
use BigCommerce\ApiV3\ResourceModels\Catalog\Category\CategoryTreeBranch;
7+
use Psr\Http\Message\ResponseInterface;
8+
9+
class CategoryTreeResponse
10+
{
11+
/**
12+
* @var CategoryTreeBranch[]
13+
*/
14+
private array $categoryTree;
15+
16+
public function __construct(ResponseInterface $response)
17+
{
18+
$body = $response->getBody();
19+
$rawData = json_decode($body);
20+
$this->decodeResponseData($rawData);
21+
}
22+
23+
private function decodeResponseData($rawData): void
24+
{
25+
$this->categoryTree = array_map(function (\stdClass $c) {
26+
return new CategoryTreeBranch($c);
27+
}, $rawData->data);
28+
}
29+
30+
/**
31+
* @return CategoryTreeBranch[]
32+
*/
33+
public function getTree(): array
34+
{
35+
return $this->categoryTree;
36+
}
37+
}

tests/BigCommerce/Catalog/CategoriesApiTest.php

+79
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace BigCommerce\Tests\Catalog;
44

55
use BigCommerce\ApiV3\Catalog\Categories\CategoryImageApi;
6+
use BigCommerce\ApiV3\ResourceModels\Catalog\Category\CategoryTreeBranch;
67
use BigCommerce\Tests\BigCommerceApiTest;
78

89
class CategoriesApiTest extends BigCommerceApiTest
@@ -43,4 +44,82 @@ public function testCanGetAllCategoryPages(): void
4344
$this->assertEquals(6, $categoriesResponse->getPagination()->total);
4445
$this->assertCount(6, $categoriesResponse->getCategories());
4546
}
47+
48+
public function testCanGetCategoryTree(): void
49+
{
50+
$this->setReturnData('catalog__categories__get_tree.json');
51+
52+
$expectedTree = [
53+
new CategoryTreeBranch((object)[
54+
"id" => 23,
55+
"parent_id" => 0,
56+
"name" => "Shop All",
57+
"is_visible" => true,
58+
"url" => "/shop-all/",
59+
"children" => [],
60+
]),
61+
new CategoryTreeBranch((object)[
62+
"id" => 18,
63+
"parent_id" => 0,
64+
"name" => "Bath",
65+
"is_visible" => true,
66+
"url" => "/bath/",
67+
"children" => [
68+
new CategoryTreeBranch((object)[
69+
"id" => 24,
70+
"parent_id" => 18,
71+
"name" => "Small Baths",
72+
"is_visible" => true,
73+
"url" => "/bath/small-baths/",
74+
"children" => [
75+
new CategoryTreeBranch((object)[
76+
"id" => 25,
77+
"parent_id" => 24,
78+
"name" => "Small Red Baths",
79+
"is_visible" => true,
80+
"url" => "/bath/small-baths/small-red-baths/",
81+
"children" => []
82+
])
83+
]
84+
])
85+
]
86+
]),
87+
new CategoryTreeBranch((object)[
88+
"id" => 19,
89+
"parent_id" => 0,
90+
"name" => "Garden",
91+
"is_visible" => true,
92+
"url" => "/garden/",
93+
"children" => []
94+
]),
95+
new CategoryTreeBranch((object)[
96+
"id" => 21,
97+
"parent_id" => 0,
98+
"name" => "Kitchen",
99+
"is_visible" => true,
100+
"url" => "/kitchen/",
101+
"children" => []
102+
]),
103+
new CategoryTreeBranch((object)[
104+
"id" => 20,
105+
"parent_id" => 0,
106+
"name" => "Publications",
107+
"is_visible" => true,
108+
"url" => "/publications/",
109+
"children" => []
110+
]),
111+
new CategoryTreeBranch((object)[
112+
"id" => 22,
113+
"parent_id" => 0,
114+
"name" => "Utility",
115+
"is_visible" => true,
116+
"url" => "/utility/",
117+
"children" => []
118+
])
119+
];
120+
121+
$response = $this->getApi()->catalog()->categories()->getCategoryTree()->getTree();
122+
123+
$this->assertEquals($expectedTree, $response);
124+
}
46125
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"data":[{"id":23,"parent_id":0,"name":"Shop All","is_visible":true,"url":"\/shop-all\/","children":[]},{"id":18,"parent_id":0,"name":"Bath","is_visible":true,"url":"\/bath\/","children":[{"id":24,"parent_id":18,"name":"Small Baths","is_visible":true,"url":"\/bath\/small-baths\/","children":[{"id":25,"parent_id":24,"name":"Small Red Baths","is_visible":true,"url":"\/bath\/small-baths\/small-red-baths\/","children":[]}]}]},{"id":19,"parent_id":0,"name":"Garden","is_visible":true,"url":"\/garden\/","children":[]},{"id":21,"parent_id":0,"name":"Kitchen","is_visible":true,"url":"\/kitchen\/","children":[]},{"id":20,"parent_id":0,"name":"Publications","is_visible":true,"url":"\/publications\/","children":[]},{"id":22,"parent_id":0,"name":"Utility","is_visible":true,"url":"\/utility\/","children":[]}],"meta":{}}

0 commit comments

Comments
 (0)