diff --git a/composer.json b/composer.json index 587c72c..93f2303 100644 --- a/composer.json +++ b/composer.json @@ -11,7 +11,7 @@ } ], "require": { - "php": "^8", + "php": "^8.1", "ext-json": "*", "ext-mbstring": "*", "symfony/http-client": "^5.4 || ^6" diff --git a/src/Client.php b/src/Client.php index 3e897dc..080c9df 100644 --- a/src/Client.php +++ b/src/Client.php @@ -96,11 +96,14 @@ private function throwOnError(array $data): void $errorResponse = $data['error_response'] ?? null; if ($errorResponse !== null) { + $subMessage = $errorResponse['sub_msg'] ?? null; + $subCode = $errorResponse['sub_code'] ?? null; + throw new AlibabaException( $errorResponse['msg'], (int) $errorResponse['code'], - $errorResponse['sub_msg'], - $errorResponse['sub_code'], + $subMessage, + $subCode, ); } } diff --git a/src/Endpoint/CategoryEndpoint.php b/src/Endpoint/CategoryEndpoint.php index 5531dd1..c920dae 100644 --- a/src/Endpoint/CategoryEndpoint.php +++ b/src/Endpoint/CategoryEndpoint.php @@ -9,7 +9,6 @@ use Kyto\Alibaba\Model\Category; use Kyto\Alibaba\Model\CategoryAttribute; use Kyto\Alibaba\Model\CategoryLevelAttribute; -use Kyto\Alibaba\Model\CategoryLevelAttributeRequest; class CategoryEndpoint { @@ -82,10 +81,11 @@ public function getLevelAttribute( string $attributeId, ?string $valueId = null ): CategoryLevelAttribute { - $attributeValueRequest = new CategoryLevelAttributeRequest(); - $attributeValueRequest->categoryId = $categoryId; - $attributeValueRequest->attributeId = $attributeId; - $attributeValueRequest->valueId = $valueId ?? '0'; + $attributeValueRequest = [ + 'cat_id' => $categoryId, + 'attr_id' => $attributeId, + 'value_id' => $valueId ?? '0' + ]; $data = $this->client->request([ 'method' => 'alibaba.icbu.category.level.attr.get', diff --git a/src/Enum/InputType.php b/src/Enum/InputType.php new file mode 100644 index 0000000..5c3ca3e --- /dev/null +++ b/src/Enum/InputType.php @@ -0,0 +1,12 @@ +subCode, $this->subMessage); + $subCodePart = $this->subCode !== null ? sprintf(' Sub-code: "%s".', $this->subCode) : null; + $subMessagePart = $this->subMessage !== null ? sprintf(' Sub-message: "%s".', $this->subMessage) : null; + $message = sprintf('%s.%s%s', $message, $subCodePart, $subMessagePart); + parent::__construct($message, $code, $previous); } diff --git a/src/Factory/CategoryFactory.php b/src/Factory/CategoryFactory.php index e8c001c..1764804 100644 --- a/src/Factory/CategoryFactory.php +++ b/src/Factory/CategoryFactory.php @@ -4,6 +4,9 @@ namespace Kyto\Alibaba\Factory; +use Kyto\Alibaba\Enum\InputType; +use Kyto\Alibaba\Enum\ShowType; +use Kyto\Alibaba\Enum\ValueType; use Kyto\Alibaba\Model\CategoryLevelAttribute; use Kyto\Alibaba\Model\CategoryLevelAttributeValue; use Kyto\Alibaba\Util\Formatter; @@ -46,9 +49,9 @@ public function createAttribute(array $data): CategoryAttribute $model->name = (string) $data['en_name']; $model->isRequired = (bool) $data['required']; - $model->inputType = (string) $data['input_type']; - $model->showType = (string) $data['show_type']; - $model->valueType = (string) $data['value_type']; + $model->inputType = InputType::from($data['input_type']); + $model->showType = ShowType::from($data['show_type']); + $model->valueType = ValueType::from($data['value_type']); $model->isSku = (bool) $data['sku_attribute']; $model->hasCustomizeImage = (bool) $data['customize_image']; diff --git a/src/Model/CategoryAttribute.php b/src/Model/CategoryAttribute.php index 7f67611..3b79432 100644 --- a/src/Model/CategoryAttribute.php +++ b/src/Model/CategoryAttribute.php @@ -4,18 +4,19 @@ namespace Kyto\Alibaba\Model; +use Kyto\Alibaba\Enum\InputType; +use Kyto\Alibaba\Enum\ShowType; +use Kyto\Alibaba\Enum\ValueType; + class CategoryAttribute { public string $id; public string $name; public bool $isRequired; - // TODO: change to enums once all values would be known - public string $inputType; // Known values: single_select, multi_select, input - - // Known values: list_box (single_select), check_box (multi_select), input (input), group_table (single_select) - public string $showType; - public string $valueType; // Known values: string, number + public InputType $inputType; + public ShowType $showType; + public ValueType $valueType; public bool $isSku; public bool $hasCustomizeImage; diff --git a/src/Model/CategoryLevelAttributeRequest.php b/src/Model/CategoryLevelAttributeRequest.php deleted file mode 100644 index b26588f..0000000 --- a/src/Model/CategoryLevelAttributeRequest.php +++ /dev/null @@ -1,27 +0,0 @@ - - */ - public function jsonSerialize(): array - { - return [ - 'cat_id' => $this->categoryId, - 'attr_id' => $this->attributeId, - 'value_id' => $this->valueId - ]; - } -} diff --git a/tests/Endpoint/CategoryEndpointTest.php b/tests/Endpoint/CategoryEndpointTest.php index a65077f..2cbf359 100644 --- a/tests/Endpoint/CategoryEndpointTest.php +++ b/tests/Endpoint/CategoryEndpointTest.php @@ -10,7 +10,6 @@ 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; diff --git a/tests/Factory/CategoryFactoryTest.php b/tests/Factory/CategoryFactoryTest.php index fd63659..46bf5f0 100644 --- a/tests/Factory/CategoryFactoryTest.php +++ b/tests/Factory/CategoryFactoryTest.php @@ -4,10 +4,15 @@ namespace Kyto\Alibaba\Tests\Factory; +use Kyto\Alibaba\Enum\InputType; +use Kyto\Alibaba\Enum\ShowType; +use Kyto\Alibaba\Enum\ValueType; use Kyto\Alibaba\Factory\CategoryFactory; use Kyto\Alibaba\Model\Category; use Kyto\Alibaba\Model\CategoryAttribute; use Kyto\Alibaba\Model\CategoryAttributeValue; +use Kyto\Alibaba\Model\CategoryLevelAttribute; +use Kyto\Alibaba\Model\CategoryLevelAttributeValue; use PHPUnit\Framework\TestCase; class CategoryFactoryTest extends TestCase @@ -145,9 +150,9 @@ public function createAttributeDataProvider(): array $model->id = '1'; $model->name = 'Example'; $model->isRequired = true; - $model->inputType = 'single_select'; - $model->showType = 'list_box'; - $model->valueType = 'string'; + $model->inputType = InputType::SINGLE_SELECT; + $model->showType = ShowType::LIST_BOX; + $model->valueType = ValueType::STRING; $model->isSku = false; $model->hasCustomizeImage = false; $model->hasCustomizeValue = false; @@ -188,9 +193,9 @@ public function createAttributeDataProvider(): array $model->id = '1'; $model->name = 'Example'; $model->isRequired = true; - $model->inputType = 'input'; - $model->showType = 'input'; - $model->valueType = 'number'; + $model->inputType = InputType::INPUT; + $model->showType = ShowType::INPUT; + $model->valueType = ValueType::NUMBER; $model->isSku = false; $model->hasCustomizeImage = false; $model->hasCustomizeValue = false; @@ -253,4 +258,91 @@ public function createAttributeValueDataProvider(): array return $cases; } + + /** + * @dataProvider createLevelAttributeDataProvider + * @param mixed[] $data + */ + public function testCreateLevelAttribute(array $data, CategoryLevelAttribute $expected): void + { + $actual = $this->categoryFactory->createLevelAttribute($data); + self::assertEquals($expected, $actual); + } + + public function createLevelAttributeDataProvider(): \Generator + { + $data = [ + 'property_id' => '123', + 'property_en_name' => 'someName', + 'values' => '{}' + ]; + + $expected = new CategoryLevelAttribute(); + $expected->id = '123'; + $expected->name = 'someName'; + $expected->values = []; + + yield ['no values' => $data, $expected]; + + $data = [ + 'property_id' => '123', + 'property_en_name' => 'someName', + 'values' => '[{"id":"1","name":"valueNoLeaf"},{"id":2,"name":"valueIsLeaf","leaf":true}]' + ]; + + $levelValueNoLeaf = new CategoryLevelAttributeValue(); + $levelValueNoLeaf->id = '1'; + $levelValueNoLeaf->name = 'valueNoLeaf'; + $levelValueNoLeaf->isLeaf = false; + + $levelValueIsLeaf = new CategoryLevelAttributeValue(); + $levelValueIsLeaf->id = '2'; + $levelValueIsLeaf->name = 'valueIsLeaf'; + $levelValueIsLeaf->isLeaf = true; + + $expected = new CategoryLevelAttribute(); + $expected->id = '123'; + $expected->name = 'someName'; + $expected->values = [$levelValueNoLeaf, $levelValueIsLeaf]; + + yield ['with values' => $data, $expected]; + } + + /** + * @dataProvider createLevelAttributeValueDataProvider + * @param mixed[] $data + */ + public function testCreateLevelAttributeValue(array $data, CategoryLevelAttributeValue $expected): void + { + $actual = $this->categoryFactory->createLevelAttributeValue($data); + self::assertEquals($expected, $actual); + } + + public function createLevelAttributeValueDataProvider(): \Generator + { + $data = [ + "id" => "1", + "name" => "valueNoLeaf" + ]; + + $expected = new CategoryLevelAttributeValue(); + $expected->name = 'valueNoLeaf'; + $expected->id = '1'; + $expected->isLeaf = false; + + yield ['no leaf' => $data, $expected]; + + $data = [ + "id" => "1", + "name" => "valueIsLeaf", + "leaf" => true + ]; + + $expected = new CategoryLevelAttributeValue(); + $expected->name = 'valueIsLeaf'; + $expected->id = '1'; + $expected->isLeaf = true; + + yield ['is leaf' => $data, $expected]; + } }