Skip to content

Commit

Permalink
Implement Catalog/Categories/GetCategoryTree
Browse files Browse the repository at this point in the history
  • Loading branch information
jswift committed Aug 26, 2020
1 parent 8d7e2f0 commit 48ae50a
Show file tree
Hide file tree
Showing 5 changed files with 161 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/BigCommerce/Catalog/CategoriesApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,15 @@
use BigCommerce\ApiV3\ResourceModels\Catalog\Category\Category;
use BigCommerce\ApiV3\ResponseModels\Category\CategoriesResponse;
use BigCommerce\ApiV3\ResponseModels\Category\CategoryResponse;
use BigCommerce\ApiV3\ResponseModels\Category\CategoryTreeResponse;
use GuzzleHttp\RequestOptions;

class CategoriesApi extends ResourceApi
{
private const RESOURCE_NAME = 'categories';
private const CATEGORIES_ENDPOINT = 'catalog/categories';
private const CATEGORY_ENDPOINT = 'catalog/categories/%d';
private const CATEGORY_TREE_ENDPOINT = 'catalog/categories/tree';

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

public function getCategoryTree(): CategoryTreeResponse
{
$response = $this->getClient()->getRestClient()->get(
self::CATEGORY_TREE_ENDPOINT
);

return new CategoryTreeResponse($response);
}

public function getAllPages(array $filter = []): CategoriesResponse
{
return CategoriesResponse::buildFromAllPages(function ($page) use ($filter) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php


namespace BigCommerce\ApiV3\ResourceModels\Catalog\Category;


use BigCommerce\ApiV3\ResourceModels\ResourceModel;
use stdClass;

class CategoryTreeBranch extends ResourceModel
{
public int $id;
public int $parent_id;
public string $name;
public bool $is_visible;
public string $url;

/**
* @var CategoryTreeBranch[]
*/
public array $children = [];

public function __construct(?stdClass $optionObject = null)
{
foreach ($optionObject->children as $categoryData) {
$this->children[] = $categoryData instanceof CategoryTreeBranch ? $categoryData : new CategoryTreeBranch($categoryData);
}

unset($optionObject->children);
parent::__construct($optionObject);
}
}
37 changes: 37 additions & 0 deletions src/BigCommerce/ResponseModels/Category/CategoryTreeResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php


namespace BigCommerce\ApiV3\ResponseModels\Category;

use BigCommerce\ApiV3\ResourceModels\Catalog\Category\CategoryTreeBranch;
use Psr\Http\Message\ResponseInterface;

class CategoryTreeResponse
{
/**
* @var CategoryTreeBranch[]
*/
private array $categoryTree;

public function __construct(ResponseInterface $response)
{
$body = $response->getBody();
$rawData = json_decode($body);
$this->decodeResponseData($rawData);
}

private function decodeResponseData($rawData): void
{
$this->categoryTree = array_map(function (\stdClass $c) {
return new CategoryTreeBranch($c);
}, $rawData->data);
}

/**
* @return CategoryTreeBranch[]
*/
public function getTree(): array
{
return $this->categoryTree;
}
}
79 changes: 79 additions & 0 deletions tests/BigCommerce/Catalog/CategoriesApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace BigCommerce\Tests\Catalog;

use BigCommerce\ApiV3\Catalog\Categories\CategoryImageApi;
use BigCommerce\ApiV3\ResourceModels\Catalog\Category\CategoryTreeBranch;
use BigCommerce\Tests\BigCommerceApiTest;

class CategoriesApiTest extends BigCommerceApiTest
Expand Down Expand Up @@ -43,4 +44,82 @@ public function testCanGetAllCategoryPages(): void
$this->assertEquals(6, $categoriesResponse->getPagination()->total);
$this->assertCount(6, $categoriesResponse->getCategories());
}

public function testCanGetCategoryTree(): void
{
$this->setReturnData('catalog__categories__get_tree.json');

$expectedTree = [
new CategoryTreeBranch((object)[
"id" => 23,
"parent_id" => 0,
"name" => "Shop All",
"is_visible" => true,
"url" => "/shop-all/",
"children" => [],
]),
new CategoryTreeBranch((object)[
"id" => 18,
"parent_id" => 0,
"name" => "Bath",
"is_visible" => true,
"url" => "/bath/",
"children" => [
new CategoryTreeBranch((object)[
"id" => 24,
"parent_id" => 18,
"name" => "Small Baths",
"is_visible" => true,
"url" => "/bath/small-baths/",
"children" => [
new CategoryTreeBranch((object)[
"id" => 25,
"parent_id" => 24,
"name" => "Small Red Baths",
"is_visible" => true,
"url" => "/bath/small-baths/small-red-baths/",
"children" => []
])
]
])
]
]),
new CategoryTreeBranch((object)[
"id" => 19,
"parent_id" => 0,
"name" => "Garden",
"is_visible" => true,
"url" => "/garden/",
"children" => []
]),
new CategoryTreeBranch((object)[
"id" => 21,
"parent_id" => 0,
"name" => "Kitchen",
"is_visible" => true,
"url" => "/kitchen/",
"children" => []
]),
new CategoryTreeBranch((object)[
"id" => 20,
"parent_id" => 0,
"name" => "Publications",
"is_visible" => true,
"url" => "/publications/",
"children" => []
]),
new CategoryTreeBranch((object)[
"id" => 22,
"parent_id" => 0,
"name" => "Utility",
"is_visible" => true,
"url" => "/utility/",
"children" => []
])
];

$response = $this->getApi()->catalog()->categories()->getCategoryTree()->getTree();

$this->assertEquals($expectedTree, $response);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +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 comments on commit 48ae50a

Please sign in to comment.