diff --git a/src/CrowdinApiClient/Api/LabelApi.php b/src/CrowdinApiClient/Api/LabelApi.php new file mode 100644 index 00000000..746f3433 --- /dev/null +++ b/src/CrowdinApiClient/Api/LabelApi.php @@ -0,0 +1,90 @@ +_list($path, Label::class, $params); + } + + /** + * Get Label Info + * @link https://support.crowdin.com/api/v2/#operation/api.projects.labels.get API Documentation + * @link https://support.crowdin.com/enterprise/api/#operation/api.projects.labels.get API Documentation Enterprise + * + * @param int $projectId + * @param int $labelId + * @return Label|null + */ + public function get(int $projectId, int $labelId): ?Label + { + $path = sprintf('projects/%d/labels/%d', $projectId, $labelId); + return $this->_get($path, Label::class); + } + + /** + * Add Label + * @link https://support.crowdin.com/api/v2/#operation/api.projects.labels.post API Documentation + * @link https://support.crowdin.com/enterprise/api/#operation/api.projects.labels.post API Documentation Enterprise + * + * @param int $projectId + * @param array $data + * @internal string $data[title] required + * @return Label|null + */ + public function create(int $projectId, array $data): ?Label + { + $path = sprintf('projects/%d/labels', $projectId); + return $this->_create($path, Label::class, $data); + } + + /** + * Edit Label + * @link https://support.crowdin.com/api/v2/#operation/api.projects.labels.patch API Documentation + * @link https://support.crowdin.com/enterprise/api/#operation/api.projects.labels.patch API Documentation Enterprise + * + * @param Label $label + * @return Label|null + */ + public function update(Label $label): ?Label + { + $path = sprintf('projects/%d/labels/%d', $label->getProjectId(), $label->getId()); + return $this->_update($path, $label); + } + + /** + * Delete Label + * @link https://support.crowdin.com/api/v2/#operation/api.projects.labels.delete API Documentation + * @link https://support.crowdin.com/enterprise/api/#operation/api.projects.labels.delete API Documentation Enterprise + * + * @param int $projectId + * @param int $labelId + * @return mixed + */ + public function delete(int $projectId, int $labelId) + { + $path = sprintf('projects/%d/labels/%d', $projectId, $labelId); + return $this->_delete($path); + } +} diff --git a/src/CrowdinApiClient/Crowdin.php b/src/CrowdinApiClient/Crowdin.php index b2e019ed..fd5a28d8 100644 --- a/src/CrowdinApiClient/Crowdin.php +++ b/src/CrowdinApiClient/Crowdin.php @@ -23,6 +23,7 @@ * @property \CrowdinApiClient\Api\IssueApi issue * @property \CrowdinApiClient\Api\ScreenshotApi screenshot * @property \CrowdinApiClient\Api\DirectoryApi directory + * @property \CrowdinApiClient\Api\LabelApi label * @property \CrowdinApiClient\Api\GlossaryApi glossary * @property \CrowdinApiClient\Api\StringTranslationApi stringTranslation * @property \CrowdinApiClient\Api\StringCommentApi stringComment @@ -92,6 +93,7 @@ class Crowdin 'stringTranslation', 'stringComment', 'directory', + 'label', 'user', 'screenshot', 'file', @@ -117,6 +119,7 @@ class Crowdin 'stringTranslationApproval', 'stringComment', 'directory', + 'label', 'vendor', 'user', 'screenshot', diff --git a/src/CrowdinApiClient/Model/Label.php b/src/CrowdinApiClient/Model/Label.php new file mode 100644 index 00000000..a1954fc1 --- /dev/null +++ b/src/CrowdinApiClient/Model/Label.php @@ -0,0 +1,67 @@ +id = (integer)$this->getDataProperty('id'); + $this->projectId = (integer)$this->getDataProperty('projectId'); + $this->title = (string)$this->getDataProperty('title'); + } + + /** + * @return int + */ + public function getId(): int + { + return $this->id; + } + + /** + * @return int + */ + public function getProjectId(): int + { + return $this->projectId; + } + + /** + * @return string + */ + public function getTitle(): string + { + return $this->title; + } + + /** + * @param string $title + */ + public function setTitle(string $title): void + { + $this->title = $title; + } +} diff --git a/tests/CrowdinApiClient/Api/LabelApiTest.php b/tests/CrowdinApiClient/Api/LabelApiTest.php new file mode 100644 index 00000000..e302588c --- /dev/null +++ b/tests/CrowdinApiClient/Api/LabelApiTest.php @@ -0,0 +1,102 @@ +mockRequest([ + 'path' => '/projects/2/labels', + 'method' => 'get', + 'response' => '{ + "data": [ + { + "data": { + "id": 4, + "projectId": 2, + "title": "main" + } + } + ], + "pagination": [ + { + "offset": 0, + "limit": 0 + } + ] + }' + ]); + + $labels = $this->crowdin->label->list(2); + + $this->assertInstanceOf(ModelCollection::class, $labels); + $this->assertCount(1, $labels); + $this->assertInstanceOf(Label::class, $labels[0]); + $this->assertEquals(4, $labels[0]->getId()); + } + + public function testGetAndUpdate() + { + $this->mockRequestGet('/projects/2/labels/34', '{ + "data": { + "id": 34, + "projectId": 2, + "title": "develop-master" + } + }'); + + $label = $this->crowdin->label->get(2, 34); + + $this->assertInstanceOf(Label::class, $label); + $this->assertEquals(34, $label->getId()); + + $label->setTitle('edit-test'); + + $this->mockRequestPath('/projects/2/labels/34', '{ + "data": { + "id": 34, + "projectId": 2, + "name": "edit-test" + } + }'); + + $this->crowdin->label->update($label); + $this->assertInstanceOf(Label::class, $label); + $this->assertEquals(34, $label->getId()); + $this->assertEquals('edit-test', $label->getTitle()); + } + + public function testCreate() + { + $params = [ + 'title' => 'develop-master', + ]; + + $this->mockRequest([ + 'path' => '/projects/2/labels', + 'method' => 'post', + 'body' => $params, + 'response' => '{ + "data": { + "id": 34, + "projectId": 2, + "title": "develop-master" + } + }' + ]); + + $label = $this->crowdin->label->create(2, $params); + $this->assertInstanceOf(Label::class, $label); + $this->assertEquals(34, $label->getId()); + } + + public function testDelete() + { + $this->mockRequestDelete('/projects/2/labels/34'); + $this->crowdin->label->delete(2, 34); + } +} diff --git a/tests/CrowdinApiClient/CrowdinTest.php b/tests/CrowdinApiClient/CrowdinTest.php index d3a46df9..10ab72bc 100644 --- a/tests/CrowdinApiClient/CrowdinTest.php +++ b/tests/CrowdinApiClient/CrowdinTest.php @@ -81,6 +81,7 @@ public function getApiClassesProvider(): array ['task', \CrowdinApiClient\Api\TaskApi::class], ['screenshot', \CrowdinApiClient\Api\ScreenshotApi::class], ['directory', \CrowdinApiClient\Api\DirectoryApi::class], + ['label', \CrowdinApiClient\Api\LabelApi::class], ['glossary', \CrowdinApiClient\Api\GlossaryApi::class], ['stringTranslation', \CrowdinApiClient\Api\StringTranslationApi::class], ['user', \CrowdinApiClient\Api\UserApi::class], diff --git a/tests/CrowdinApiClient/Model/LabelTest.php b/tests/CrowdinApiClient/Model/LabelTest.php new file mode 100644 index 00000000..293a81f3 --- /dev/null +++ b/tests/CrowdinApiClient/Model/LabelTest.php @@ -0,0 +1,46 @@ + 4, + 'title' => 'main', + ]; + + /** + * @var Label + */ + public $label; + + public function testLoadData() + { + $this->label = new Label($this->data); + $this->checkData(); + } + + public function testSetData() + { + $this->label = new Label(); + $this->label->setTitle($this->data['title']); + + $this->assertEquals($this->data['title'], $this->label->getTitle()); + } + + public function checkData() + { + $this->assertEquals($this->data['id'], $this->label->getId()); + $this->assertEquals($this->data['title'], $this->label->getTitle()); + } +}