Skip to content

Commit 7496972

Browse files
fix: update PHP SDK code style to fix failing workflow (#689)
Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> Co-authored-by: Max Prilutskiy <maks.prilutskiy@gmail.com>
1 parent 72a37c5 commit 7496972

File tree

2 files changed

+246
-128
lines changed

2 files changed

+246
-128
lines changed

php/sdk/src/LingoDotDevEngine.php

Lines changed: 124 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,13 @@
11
<?php
2+
/**
3+
* PHP SDK for Lingo.dev
4+
*
5+
* @category Localization
6+
* @package Lingodotdev\Sdk
7+
* @author Lingo.dev Team <hi@lingo.dev>
8+
* @license MIT
9+
* @link https://lingo.dev
10+
*/
211

312
namespace Lingodotdev\Sdk;
413

@@ -10,18 +19,28 @@
1019
* LingoDotDevEngine class for interacting with the LingoDotDev API
1120
* A powerful localization engine that supports various content types including
1221
* plain text, objects, and chat sequences.
22+
*
23+
* @category Localization
24+
* @package Lingodotdev\Sdk
25+
* @author Lingo.dev Team <hi@lingo.dev>
26+
* @license MIT
27+
* @link https://lingo.dev
1328
*/
1429
class LingoDotDevEngine
1530
{
1631
/**
17-
* @var array Configuration options for the Engine
32+
* Configuration options for the Engine
33+
*
34+
* @var array
1835
*/
1936
protected $config;
2037

2138
/**
22-
* @var Client HTTP client
39+
* HTTP client for API requests
40+
*
41+
* @var Client
2342
*/
24-
private $httpClient;
43+
private $_httpClient;
2544

2645
/**
2746
* Create a new LingoDotDevEngine instance
@@ -30,11 +49,13 @@ class LingoDotDevEngine
3049
*/
3150
public function __construct(array $config = [])
3251
{
33-
$this->config = array_merge([
52+
$this->config = array_merge(
53+
[
3454
'apiUrl' => 'https://engine.lingo.dev',
3555
'batchSize' => 25,
3656
'idealBatchItemSize' => 250
37-
], $config);
57+
], $config
58+
);
3859

3960
if (!isset($this->config['apiKey'])) {
4061
throw new \InvalidArgumentException('API key is required');
@@ -52,40 +73,43 @@ public function __construct(array $config = [])
5273
throw new \InvalidArgumentException('Ideal batch item size must be an integer between 1 and 2500');
5374
}
5475

55-
$this->httpClient = new Client([
76+
$this->_httpClient = new Client(
77+
[
5678
'base_uri' => $this->config['apiUrl'],
5779
'headers' => [
5880
'Content-Type' => 'application/json; charset=utf-8',
5981
'Authorization' => 'Bearer ' . $this->config['apiKey']
6082
]
61-
]);
83+
]
84+
);
6285
}
6386

6487
/**
6588
* Localize content using the Lingo.dev API
6689
*
67-
* @param array $payload The content to be localized
68-
* @param array $params Localization parameters including source/target locales and fast mode option
69-
* @param callable|null $progressCallback Optional callback function to report progress (0-100)
70-
* @return array Localized content
90+
* @param array $payload The content to be localized
91+
* @param array $params Localization parameters including source/target locales and fast mode option
92+
* @param callable|null $progressCallback Optional callback function to report progress (0-100)
93+
*
94+
* @return array Localized content
7195
* @internal
7296
*/
73-
protected function _localizeRaw(array $payload, array $params, callable $progressCallback = null): array
97+
protected function localizeRaw(array $payload, array $params, callable $progressCallback = null): array
7498
{
7599
if (!isset($params['targetLocale'])) {
76100
throw new \InvalidArgumentException('Target locale is required');
77101
}
78102

79-
$chunkedPayload = $this->extractPayloadChunks($payload);
103+
$chunkedPayload = $this->_extractPayloadChunks($payload);
80104
$processedPayloadChunks = [];
81105

82-
$workflowId = $this->createId();
106+
$workflowId = $this->_createId();
83107

84108
for ($i = 0; $i < count($chunkedPayload); $i++) {
85109
$chunk = $chunkedPayload[$i];
86110
$percentageCompleted = round((($i + 1) / count($chunkedPayload)) * 100);
87111

88-
$processedPayloadChunk = $this->localizeChunk(
112+
$processedPayloadChunk = $this->_localizeChunk(
89113
$params['sourceLocale'] ?? null,
90114
$params['targetLocale'],
91115
['data' => $chunk, 'reference' => $params['reference'] ?? null],
@@ -106,17 +130,19 @@ protected function _localizeRaw(array $payload, array $params, callable $progres
106130
/**
107131
* Localize a single chunk of content
108132
*
109-
* @param string|null $sourceLocale Source locale
110-
* @param string $targetLocale Target locale
111-
* @param array $payload Payload containing the chunk to be localized
112-
* @param string $workflowId Workflow ID
113-
* @param bool $fast Whether to use fast mode
133+
* @param string|null $sourceLocale Source locale
134+
* @param string $targetLocale Target locale
135+
* @param array $payload Payload containing the chunk to be localized
136+
* @param string $workflowId Workflow ID
137+
* @param bool $fast Whether to use fast mode
138+
*
114139
* @return array Localized chunk
115140
*/
116-
private function localizeChunk(?string $sourceLocale, string $targetLocale, array $payload, string $workflowId, bool $fast): array
141+
private function _localizeChunk(?string $sourceLocale, string $targetLocale, array $payload, string $workflowId, bool $fast): array
117142
{
118143
try {
119-
$response = $this->httpClient->post('/i18n', [
144+
$response = $this->_httpClient->post(
145+
'/i18n', [
120146
'json' => [
121147
'params' => [
122148
'workflowId' => $workflowId,
@@ -129,7 +155,8 @@ private function localizeChunk(?string $sourceLocale, string $targetLocale, arra
129155
'data' => $payload['data'],
130156
'reference' => $payload['reference']
131157
]
132-
]);
158+
]
159+
);
133160

134161
$jsonResponse = json_decode($response->getBody()->getContents(), true);
135162

@@ -149,10 +176,11 @@ private function localizeChunk(?string $sourceLocale, string $targetLocale, arra
149176
/**
150177
* Extract payload chunks based on the ideal chunk size
151178
*
152-
* @param array $payload The payload to be chunked
179+
* @param array $payload The payload to be chunked
180+
*
153181
* @return array An array of payload chunks
154182
*/
155-
private function extractPayloadChunks(array $payload): array
183+
private function _extractPayloadChunks(array $payload): array
156184
{
157185
$result = [];
158186
$currentChunk = [];
@@ -168,12 +196,11 @@ private function extractPayloadChunks(array $payload): array
168196
$currentChunk[$key] = $value;
169197
$currentChunkItemCount++;
170198

171-
$currentChunkSize = $this->countWordsInRecord($currentChunk);
199+
$currentChunkSize = $this->_countWordsInRecord($currentChunk);
172200

173-
if (
174-
$currentChunkSize > $this->config['idealBatchItemSize'] ||
175-
$currentChunkItemCount >= $this->config['batchSize'] ||
176-
$i === count($keys) - 1
201+
if ($currentChunkSize > $this->config['idealBatchItemSize']
202+
|| $currentChunkItemCount >= $this->config['batchSize']
203+
|| $i === count($keys) - 1
177204
) {
178205
$result[] = $currentChunk;
179206
$currentChunk = [];
@@ -187,21 +214,22 @@ private function extractPayloadChunks(array $payload): array
187214
/**
188215
* Count words in a record or array
189216
*
190-
* @param mixed $payload The payload to count words in
217+
* @param mixed $payload The payload to count words in
218+
*
191219
* @return int The total number of words
192220
*/
193-
private function countWordsInRecord($payload): int
221+
private function _countWordsInRecord($payload): int
194222
{
195223
if (is_array($payload)) {
196224
$count = 0;
197225
foreach ($payload as $item) {
198-
$count += $this->countWordsInRecord($item);
226+
$count += $this->_countWordsInRecord($item);
199227
}
200228
return $count;
201229
} elseif (is_object($payload)) {
202230
$count = 0;
203231
foreach ((array)$payload as $item) {
204-
$count += $this->countWordsInRecord($item);
232+
$count += $this->_countWordsInRecord($item);
205233
}
206234
return $count;
207235
} elseif (is_string($payload)) {
@@ -216,52 +244,71 @@ private function countWordsInRecord($payload): int
216244
*
217245
* @return string Unique ID
218246
*/
219-
private function createId(): string
247+
private function _createId(): string
220248
{
221249
return bin2hex(random_bytes(8));
222250
}
223251

224252
/**
225253
* Localize a typical PHP array or object
226254
*
227-
* @param array $obj The object to be localized (strings will be extracted and translated)
228-
* @param array $params Localization parameters:
229-
* - sourceLocale: The source language code (e.g., 'en')
230-
* - targetLocale: The target language code (e.g., 'es')
231-
* - fast: Optional boolean to enable fast mode (faster but potentially lower quality)
232-
* @param callable|null $progressCallback Optional callback function to report progress (0-100)
255+
* @param array $obj The object to be localized (strings will be extracted and translated)
256+
* @param array $params Localization parameters:
257+
* - sourceLocale: The
258+
* source language code
259+
* (e.g., 'en') -
260+
* targetLocale: The target
261+
* language code (e.g.,
262+
* 'es') - fast: Optional
263+
* boolean to enable fast
264+
* mode (faster but
265+
* potentially lower
266+
* quality)
267+
* @param callable|null $progressCallback Optional callback function to report progress (0-100)
233268
* @return array A new object with the same structure but localized string values
234269
*/
235270
public function localizeObject(array $obj, array $params, callable $progressCallback = null): array
236271
{
237-
return $this->_localizeRaw($obj, $params, $progressCallback);
272+
return $this->localizeRaw($obj, $params, $progressCallback);
238273
}
239274

240275
/**
241276
* Localize a single text string
242277
*
243-
* @param string $text The text string to be localized
244-
* @param array $params Localization parameters:
245-
* - sourceLocale: The source language code (e.g., 'en')
246-
* - targetLocale: The target language code (e.g., 'es')
247-
* - fast: Optional boolean to enable fast mode (faster for bigger batches)
248-
* @param callable|null $progressCallback Optional callback function to report progress (0-100)
278+
* @param string $text The text string to be localized
279+
* @param array $params Localization parameters:
280+
* - sourceLocale: The
281+
* source language code
282+
* (e.g., 'en') -
283+
* targetLocale: The target
284+
* language code (e.g.,
285+
* 'es') - fast: Optional
286+
* boolean to enable fast
287+
* mode (faster for bigger
288+
* batches)
289+
* @param callable|null $progressCallback Optional callback function to report progress (0-100)
249290
* @return string The localized text string
250291
*/
251292
public function localizeText(string $text, array $params, callable $progressCallback = null): string
252293
{
253-
$response = $this->_localizeRaw(['text' => $text], $params, $progressCallback);
294+
$response = $this->localizeRaw(['text' => $text], $params, $progressCallback);
254295
return $response['text'] ?? '';
255296
}
256297

257298
/**
258299
* Localize a text string to multiple target locales
259300
*
260-
* @param string $text The text string to be localized
261-
* @param array $params Localization parameters:
262-
* - sourceLocale: The source language code (e.g., 'en')
263-
* - targetLocales: An array of target language codes (e.g., ['es', 'fr'])
264-
* - fast: Optional boolean to enable fast mode (for bigger batches)
301+
* @param string $text The text string to be localized
302+
* @param array $params Localization parameters:
303+
* - sourceLocale: The
304+
* source language code
305+
* (e.g., 'en') -
306+
* targetLocales: An array
307+
* of target language codes
308+
* (e.g., ['es', 'fr']) -
309+
* fast: Optional boolean
310+
* to enable fast mode (for
311+
* bigger batches)
265312
* @return array An array of localized text strings
266313
*/
267314
public function batchLocalizeText(string $text, array $params): array
@@ -276,11 +323,13 @@ public function batchLocalizeText(string $text, array $params): array
276323

277324
$responses = [];
278325
foreach ($params['targetLocales'] as $targetLocale) {
279-
$responses[] = $this->localizeText($text, [
326+
$responses[] = $this->localizeText(
327+
$text, [
280328
'sourceLocale' => $params['sourceLocale'],
281329
'targetLocale' => $targetLocale,
282330
'fast' => $params['fast'] ?? false
283-
]);
331+
]
332+
);
284333
}
285334

286335
return $responses;
@@ -289,12 +338,19 @@ public function batchLocalizeText(string $text, array $params): array
289338
/**
290339
* Localize a chat sequence while preserving speaker names
291340
*
292-
* @param array $chat Array of chat messages, each with 'name' and 'text' properties
293-
* @param array $params Localization parameters:
294-
* - sourceLocale: The source language code (e.g., 'en')
295-
* - targetLocale: The target language code (e.g., 'es')
296-
* - fast: Optional boolean to enable fast mode (faster but potentially lower quality)
297-
* @param callable|null $progressCallback Optional callback function to report progress (0-100)
341+
* @param array $chat Array of chat messages, each with 'name' and 'text' properties
342+
* @param array $params Localization parameters:
343+
* - sourceLocale: The
344+
* source language code
345+
* (e.g., 'en') -
346+
* targetLocale: The target
347+
* language code (e.g.,
348+
* 'es') - fast: Optional
349+
* boolean to enable fast
350+
* mode (faster but
351+
* potentially lower
352+
* quality)
353+
* @param callable|null $progressCallback Optional callback function to report progress (0-100)
298354
* @return array Array of localized chat messages with preserved structure
299355
*/
300356
public function localizeChat(array $chat, array $params, callable $progressCallback = null): array
@@ -307,7 +363,7 @@ public function localizeChat(array $chat, array $params, callable $progressCallb
307363
$payload["chat_{$index}"] = $message['text'];
308364
}
309365

310-
$localized = $this->_localizeRaw($payload, $params, $progressCallback);
366+
$localized = $this->localizeRaw($payload, $params, $progressCallback);
311367

312368
$result = [];
313369
foreach ($localized as $key => $value) {
@@ -324,15 +380,17 @@ public function localizeChat(array $chat, array $params, callable $progressCallb
324380
/**
325381
* Detect the language of a given text
326382
*
327-
* @param string $text The text to analyze
383+
* @param string $text The text to analyze
328384
* @return string Promise resolving to a locale code (e.g., 'en', 'es', 'fr')
329385
*/
330386
public function recognizeLocale(string $text): string
331387
{
332388
try {
333-
$response = $this->httpClient->post('/recognize', [
389+
$response = $this->_httpClient->post(
390+
'/recognize', [
334391
'json' => ['text' => $text]
335-
]);
392+
]
393+
);
336394

337395
$jsonResponse = json_decode($response->getBody()->getContents(), true);
338396
return $jsonResponse['locale'];

0 commit comments

Comments
 (0)