Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(api): added endpoint for batch source string editing #198

Merged
merged 7 commits into from
Nov 15, 2024
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@ coverage.xml
phpdoc
phpDocumentor.phar
.phpunit.result.cache
/.ddev
/.vscode
18 changes: 18 additions & 0 deletions src/CrowdinApiClient/Api/SourceStringApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,4 +92,22 @@ public function delete(int $projectId, int $stringId)
$path = sprintf('projects/%d/strings/%d', $projectId, $stringId);
return $this->_delete($path);
}

/**
* String Batch Operations
* @link https://support.crowdin.com/developer/api/v2/#tag/Source-Strings/operation/api.projects.strings.batchPatch API Documentation
* @link https://support.crowdin.com/developer/enterprise/api/v2/#tag/Source-Strings/operation/api.projects.strings.batchPatch API Documentation Enterprise
*
* @param int $projectId
* @param array $data
* string $data[op] required Patch operation to perform (replace, add, remove)<br>
* string <json-pointer> $data[path] required
* value $data[value] required object, string, int, or bool
* @return mixed
*/
public function batchOperations(int $projectId, array $data)
{
$path = sprintf('projects/%d/strings', $projectId);
return $this->_patch($path, SourceString::class, $data);
}
}
66 changes: 66 additions & 0 deletions tests/CrowdinApiClient/Api/SourceStringApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,4 +155,70 @@ public function testDelete()

$this->crowdin->sourceString->delete(2, 2814);
}

public function testBatch()
{
$this->mockRequest([
'path' => '/projects/2/strings',
'method' => 'patch',
'response' => '{
DE-dylan-lauzon marked this conversation as resolved.
Show resolved Hide resolved
"data":[
{
"data":{
"id":2814,
"projectId": 2,
"branchId":null,
"identifier":"a.b.c",
"text":"new added string",
"type":"text",
"context":"a.b.c\ncontext for new string",
"maxLength":0,
"isHidden":false,
"isDuplicate":false,
"masterStringId":null,
"hasPlurals":false,
"isIcu":false,
"labelIds":[],
"webUrl":"https://example.crowdin.com/editor/1/all/en-pl?filter=basic&value=0&view=comfortable#2",
"createdAt":"2024-11-13T16:56:18+00:00",
"updatedAt":null,
"fileId":48,
"directoryId":null,
"revision":1
}
}
]
}'
]);

$batchResult = $this->crowdin->sourceString->batchOperations(2, [
[
'op' => 'replace',
'path' => '/2814/isHidden',
'value' => true
],
[
'op' => 'replace',
'path' => '/2814/context',
'value' => 'some context'
],
[
'op' => 'add',
'path' => '/-',
'value' => [
'text' => 'new added string',
'identifier' => 'a.b.c',
'context' => 'context for new string',
'fileId' => 8,
'isHidden' => false
]
],
[
'op' => 'remove',
'path' => '/2814'
]
]);

$this->assertInstanceOf(SourceString::class, $batchResult);
}
}