From 1135c918bc388390e2f450b81acf55dcf47f059f Mon Sep 17 00:00:00 2001 From: Dominik Czulak Date: Fri, 9 Feb 2024 15:26:08 +0100 Subject: [PATCH] add category level attribute api support --- README.md | 3 +- src/Endpoint/CategoryEndpoint.php | 24 +++++++++++++ src/Factory/CategoryFactory.php | 40 +++++++++++++++++++-- src/Model/CategoryAttribute.php | 2 +- src/Model/CategoryLevelAttribute.php | 14 ++++++++ src/Model/CategoryLevelAttributeRequest.php | 27 ++++++++++++++ src/Model/CategoryLevelAttributeValue.php | 12 +++++++ tests/Endpoint/CategoryEndpointTest.php | 28 +++++++++++++++ 8 files changed, 145 insertions(+), 5 deletions(-) create mode 100644 src/Model/CategoryLevelAttribute.php create mode 100644 src/Model/CategoryLevelAttributeRequest.php create mode 100644 src/Model/CategoryLevelAttributeValue.php diff --git a/README.md b/README.md index bffc6dd..575670f 100644 --- a/README.md +++ b/README.md @@ -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 ``` diff --git a/src/Endpoint/CategoryEndpoint.php b/src/Endpoint/CategoryEndpoint.php index 37216ad..4def8db 100644 --- a/src/Endpoint/CategoryEndpoint.php +++ b/src/Endpoint/CategoryEndpoint.php @@ -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 { @@ -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); + } } diff --git a/src/Factory/CategoryFactory.php b/src/Factory/CategoryFactory.php index 37a3161..e8c001c 100644 --- a/src/Factory/CategoryFactory.php +++ b/src/Factory/CategoryFactory.php @@ -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; @@ -15,7 +17,7 @@ class CategoryFactory { /** - * @param mixed[] $data + * @param array $data */ public function createCategory(array $data): Category { @@ -34,7 +36,7 @@ public function createCategory(array $data): Category } /** - * @param mixed[] $data + * @param array $data */ public function createAttribute(array $data): CategoryAttribute { @@ -64,7 +66,7 @@ public function createAttribute(array $data): CategoryAttribute } /** - * @param mixed[] $data + * @param array $data */ public function createAttributeValue(array $data): CategoryAttributeValue { @@ -77,4 +79,36 @@ public function createAttributeValue(array $data): CategoryAttributeValue return $model; } + + /** + * @param array $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 $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; + } } diff --git a/src/Model/CategoryAttribute.php b/src/Model/CategoryAttribute.php index d0153ec..f79e2c7 100644 --- a/src/Model/CategoryAttribute.php +++ b/src/Model/CategoryAttribute.php @@ -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; diff --git a/src/Model/CategoryLevelAttribute.php b/src/Model/CategoryLevelAttribute.php new file mode 100644 index 0000000..b497f16 --- /dev/null +++ b/src/Model/CategoryLevelAttribute.php @@ -0,0 +1,14 @@ + + */ + public function jsonSerialize(): array + { + return [ + 'cat_id' => $this->categoryId, + 'attr_id' => $this->attributeId, + 'value_id' => $this->valueId + ]; + } +} diff --git a/src/Model/CategoryLevelAttributeValue.php b/src/Model/CategoryLevelAttributeValue.php new file mode 100644 index 0000000..b2e5b11 --- /dev/null +++ b/src/Model/CategoryLevelAttributeValue.php @@ -0,0 +1,12 @@ +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); + } }