Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions src/CrowdinApiClient/Api/TranslationMemoryApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,32 @@ public function createSegment(int $tmId, array $data): ?TranslationMemorySegment
return $this->_create($path, TranslationMemorySegment::class, $data);
}

/**
* TM Segment Batch Operations
* @link https://developer.crowdin.com/api/v2/#operation/api.tms.segments.patchBatch API Documentation
* @link https://developer.crowdin.com/enterprise/api/v2/#operation/api.tms.segments.patchBatch API Documentation Enterprise
*
* @param int $tmId
* @param array $data JSON Patch array with operations (add, remove, replace)
* @return ModelCollection
*/
public function batchOperations(int $tmId, array $data): ModelCollection
{
$path = sprintf('tms/%d/segments', $tmId);

$options = [
'body' => json_encode($data),
'headers' => $this->getHeaders(),
];

return $this->client->apiRequest(
'patch',
$path,
new ResponseModelListDecorator(TranslationMemorySegment::class),
$options
);
}

/**
* Delete TM Segment
* @link https://developer.crowdin.com/api/v2/#operation/api.tms.segments.delete API Documentation
Expand Down
74 changes: 74 additions & 0 deletions tests/CrowdinApiClient/Api/TranslationMemoryApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,80 @@ public function testCreateSegment()
$this->assertEquals(4, $segment->getId());
}

public function testBatchOperations()
{
$data = [
[
'op' => 'add',
'path' => '/-',
'value' => [
'records' => [
[
'languageId' => 'uk',
'text' => 'Перекладений текст',
],
],
],
],
[
'op' => 'add',
'path' => '/4/records/-',
'value' => [
'languageId' => 'it',
'text' => 'Ciao, mondo!',
],
],
[
'op' => 'remove',
'path' => '/5',
],
[
'op' => 'remove',
'path' => '/4/records/1',
],
[
'op' => 'replace',
'path' => '/4/records/1/text',
'value' => 'Updated translation text',
],
];

$this->mockRequest([
'path' => '/tms/4/segments',
'method' => 'patch',
'body' => json_encode($data),
'response' => json_encode([
'data' => [
[
'data' => [
'id' => 4,
'records' => [
[
'id' => 1,
'languageId' => 'uk',
'text' => 'Updated translation text',
'usageCount' => 13,
'createdBy' => 1,
'updatedBy' => 1,
'createdAt' => '2019-09-16T13:48:04+00:00',
'updatedAt' => '2019-09-16T13:48:04+00:00',
],
],
],
],
],
]),
]);

$segments = $this->crowdin->translationMemory->batchOperations(4, $data);

$this->assertInstanceOf(ModelCollection::class, $segments);
$this->assertCount(1, $segments);
$this->assertInstanceOf(TranslationMemorySegment::class, $segments[0]);
$this->assertEquals(4, $segments[0]->getId());
$this->assertEquals('Updated translation text', $segments[0]->getRecords()[0]->getText());
}

public function testDeleteSegment()
{
$this->mockRequestDelete('/tms/4/segments/1');
Expand Down
Loading