Skip to content

Commit

Permalink
add category level attribute api support
Browse files Browse the repository at this point in the history
  • Loading branch information
Dominik-Czulak committed Feb 9, 2024
1 parent aad3929 commit 1135c91
Show file tree
Hide file tree
Showing 8 changed files with 145 additions and 5 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ facade
│ └─ new - Obtain new session token
├─ category/ - Category endpoint
│ ├─ get - Get product listing category
│ └─ getAttributes - Get system-defined attributes based on category ID
│ ├─ getAttributes - Get system-defined attributes based on category ID
│ └─ getLevelAttribute - Get next-level attribute based on category, attribute and value ID (e.g. car_model values)
└─ product/ - Product endpoint
└─ getGroup - Get product group
```
Expand Down
24 changes: 24 additions & 0 deletions src/Endpoint/CategoryEndpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
use Kyto\Alibaba\Factory\CategoryFactory;
use Kyto\Alibaba\Model\Category;
use Kyto\Alibaba\Model\CategoryAttribute;
use Kyto\Alibaba\Model\CategoryLevelAttribute;
use Kyto\Alibaba\Model\CategoryLevelAttributeRequest;

class CategoryEndpoint
{
Expand Down Expand Up @@ -68,4 +70,26 @@ public function getAttributes(string $categoryId): array

return $result;
}

/**
* Get next-level attribute based on category, attribute and optionally level attribute value ID.
* @link https://developer.alibaba.com/en/doc.htm?spm=a2728.12183079.k2mwm9fd.1.4b3630901WuQWY#?docType=2&docId=48659
*
* @param ?string $valueId provide null to fetch first level
*/
public function getLevelAttribute(string $categoryId, string $attributeId, ?string $valueId = null): CategoryLevelAttribute
{
$attributeValueRequest = new CategoryLevelAttributeRequest();
$attributeValueRequest->categoryId = $categoryId;
$attributeValueRequest->attributeId = $attributeId;
$attributeValueRequest->valueId = $valueId ?? '0';

$data = $this->client->request([
'method' => 'alibaba.icbu.category.level.attr.get',
'attribute_value_request' => json_encode($attributeValueRequest)
]);

$attribute = $data['alibaba_icbu_category_level_attr_get_response']['result_list'];
return $this->categoryFactory->createLevelAttribute($attribute);
}
}
40 changes: 37 additions & 3 deletions src/Factory/CategoryFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace Kyto\Alibaba\Factory;

use Kyto\Alibaba\Model\CategoryLevelAttribute;
use Kyto\Alibaba\Model\CategoryLevelAttributeValue;
use Kyto\Alibaba\Util\Formatter;
use Kyto\Alibaba\Model\Category;
use Kyto\Alibaba\Model\CategoryAttribute;
Expand All @@ -15,7 +17,7 @@
class CategoryFactory
{
/**
* @param mixed[] $data
* @param array<string, mixed> $data
*/
public function createCategory(array $data): Category
{
Expand All @@ -34,7 +36,7 @@ public function createCategory(array $data): Category
}

/**
* @param mixed[] $data
* @param array<string, mixed> $data
*/
public function createAttribute(array $data): CategoryAttribute
{
Expand Down Expand Up @@ -64,7 +66,7 @@ public function createAttribute(array $data): CategoryAttribute
}

/**
* @param mixed[] $data
* @param array<string, mixed> $data
*/
public function createAttributeValue(array $data): CategoryAttributeValue
{
Expand All @@ -77,4 +79,36 @@ public function createAttributeValue(array $data): CategoryAttributeValue

return $model;
}

/**
* @param array<string, mixed> $data
*/
public function createLevelAttribute(array $data): CategoryLevelAttribute
{
$model = new CategoryLevelAttribute();

$model->id = (string) $data['property_id'];
$model->name = (string) $data['property_en_name'];

$model->values = [];
$decodedValues = json_decode($data['values'], true);
foreach ($decodedValues as $value) {
$model->values[] = $this->createLevelAttributeValue($value);
}

return $model;
}

/**
* @param array<string, mixed> $data
*/
public function createLevelAttributeValue(array $data): CategoryLevelAttributeValue
{
$model = new CategoryLevelAttributeValue();
$model->name = (string) $data['name'];
$model->id = (string) $data['id'];
$model->isLeaf = isset($data['leaf']);

return $model;
}
}
2 changes: 1 addition & 1 deletion src/Model/CategoryAttribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class CategoryAttribute

// TODO: change to enums once all values would be known
public string $inputType; // Known values: single_select, multi_select, input
public string $showType; // Known values: list_box (single_select), check_box (multi_select), input (input)
public string $showType; // Known values: list_box (single_select), check_box (multi_select), input (input), group_table (single_select)
public string $valueType; // Known values: string, number

public bool $isSku;
Expand Down
14 changes: 14 additions & 0 deletions src/Model/CategoryLevelAttribute.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

declare(strict_types=1);

namespace Kyto\Alibaba\Model;

class CategoryLevelAttribute
{
public string $id;
public string $name;

/** @var CategoryLevelAttributeValue[] */
public array $values;
}
27 changes: 27 additions & 0 deletions src/Model/CategoryLevelAttributeRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

declare(strict_types=1);

namespace Kyto\Alibaba\Model;

/**
* The actual request needs it as a json, so use json_encode() on this model before you use it in a request.
*/
class CategoryLevelAttributeRequest implements \JsonSerializable
{
public string $categoryId;
public string $attributeId;
public string $valueId;

/**
* @return array<string, string>
*/
public function jsonSerialize(): array
{
return [
'cat_id' => $this->categoryId,
'attr_id' => $this->attributeId,
'value_id' => $this->valueId
];
}
}
12 changes: 12 additions & 0 deletions src/Model/CategoryLevelAttributeValue.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

namespace Kyto\Alibaba\Model;

class CategoryLevelAttributeValue
{
public string $id;
public string $name;
public bool $isLeaf;
}
28 changes: 28 additions & 0 deletions tests/Endpoint/CategoryEndpointTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
use Kyto\Alibaba\Factory\CategoryFactory;
use Kyto\Alibaba\Model\Category;
use Kyto\Alibaba\Model\CategoryAttribute;
use Kyto\Alibaba\Model\CategoryLevelAttribute;
use Kyto\Alibaba\Model\CategoryLevelAttributeRequest;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;

Expand Down Expand Up @@ -98,4 +100,30 @@ public function testGetAttributes(): void
$actual = $this->categoryEndpoint->getAttributes($id);
self::assertSame($result, $actual);
}

public function testGetLevelAttributes(): void
{
$attributeValueRequestBody = '{"cat_id":"1","attr_id":"1","value_id":"0"}';
$levelAttribute = ['LevelAttribute'];
$data = ['alibaba_icbu_category_level_attr_get_response' => ['result_list' => $levelAttribute]];

$this->client
->expects(self::once())
->method('request')
->with([
'method' => 'alibaba.icbu.category.level.attr.get',
'attribute_value_request' => $attributeValueRequestBody,
])
->willReturn($data);

$result = new CategoryLevelAttribute();

$this->categoryFactory
->expects(self::once())
->method('createLevelAttribute')
->willReturn($result);

$actual = $this->categoryEndpoint->getLevelAttribute('1', '1', null);
self::assertSame($result, $actual);
}
}

0 comments on commit 1135c91

Please sign in to comment.