Skip to content

Commit

Permalink
Merge pull request #85 from uintaam/labels_api
Browse files Browse the repository at this point in the history
Add labels API
  • Loading branch information
andrii-bodnar authored Nov 29, 2021
2 parents ab5ef43 + 77f4044 commit 58d88c8
Show file tree
Hide file tree
Showing 6 changed files with 309 additions and 0 deletions.
90 changes: 90 additions & 0 deletions src/CrowdinApiClient/Api/LabelApi.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?php

namespace CrowdinApiClient\Api;

use CrowdinApiClient\Model\Label;
use CrowdinApiClient\ModelCollection;

/**
* Class LabelApi
* @package Crowdin\Api
*/
class LabelApi extends AbstractApi
{
/**
* List Labels
* @link https://support.crowdin.com/api/v2/#operation/api.projects.labels.getMany API Documentation
* @link https://support.crowdin.com/enterprise/api/#operation/api.projects.labels.getMany API Documentation Enterprise
*
* @param int $projectId
* @param array $params
* @internal integer $params[limit]
* @internal integer $params[offset]
* @return ModelCollection
*/
public function list(int $projectId, array $params = []): ModelCollection
{
$path = sprintf('projects/%d/labels', $projectId);
return $this->_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);
}
}
3 changes: 3 additions & 0 deletions src/CrowdinApiClient/Crowdin.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -92,6 +93,7 @@ class Crowdin
'stringTranslation',
'stringComment',
'directory',
'label',
'user',
'screenshot',
'file',
Expand All @@ -117,6 +119,7 @@ class Crowdin
'stringTranslationApproval',
'stringComment',
'directory',
'label',
'vendor',
'user',
'screenshot',
Expand Down
67 changes: 67 additions & 0 deletions src/CrowdinApiClient/Model/Label.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php

namespace CrowdinApiClient\Model;

/**
* Class Label
*
* @package Crowdin\Model
*/
class Label extends BaseModel
{
/**
* @var integer
*/
protected $id;

/**
* @var integer
*/
protected $projectId;

/**
* @var string
*/
protected $title;

public function __construct(array $data = [])
{
parent::__construct($data);

$this->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;
}
}
102 changes: 102 additions & 0 deletions tests/CrowdinApiClient/Api/LabelApiTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
<?php

namespace CrowdinApiClient\Tests\Api;

use CrowdinApiClient\Model\Label;
use CrowdinApiClient\ModelCollection;

class LabelApiTest extends AbstractTestApi
{
public function testList()
{
$this->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);
}
}
1 change: 1 addition & 0 deletions tests/CrowdinApiClient/CrowdinTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand Down
46 changes: 46 additions & 0 deletions tests/CrowdinApiClient/Model/LabelTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

namespace CrowdinApiClient\Tests\Model;

use CrowdinApiClient\Model\Label;
use PHPUnit\Framework\TestCase;

/**
* Class LabelTest
* @package Crowdin\Tests\Model
*/
class LabelTest extends TestCase
{
/**
* @var array
*/
public $data = [
'id' => 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());
}
}

0 comments on commit 58d88c8

Please sign in to comment.