-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #85 from uintaam/labels_api
Add labels API
- Loading branch information
Showing
6 changed files
with
309 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |