3131class LingoDotDevEngine
3232{
3333 /**
34- * Configuration options for the Engine
34+ * Configuration options for the Engine.
3535 *
36- * @var array
36+ * @var array<string, mixed>
3737 */
3838 protected $ config ;
3939
4040 /**
41- * HTTP client for API requests
41+ * HTTP client for API requests.
4242 *
4343 * @var Client
4444 */
4545 private $ _httpClient ;
4646
4747 /**
48- * Build an engine instance with your API key and optional batch settings .
48+ * Build an engine with your API key and optional batching limits .
4949 *
50- * Provide at least ['apiKey' => '...']. You may override :
51- * - 'apiUrl' with a valid base URL for the service (defaults to https://engine.lingo.dev).
52- * - 'batchSize' with an integer between 1 and 250 to control items sent per request.
53- * - 'idealBatchItemSize' with an integer between 1 and 2500 to cap words per request.
54- * Invalid values trigger \InvalidArgumentException.
50+ * @param array<string, mixed> $config Configuration options :
51+ * - 'apiKey' (string, required): Your API token
52+ * - 'apiUrl' (string): API base URL (default: https://engine.lingo.dev)
53+ * - 'batchSize' (int): Records per request, 1-250 (default: 25)
54+ * - 'idealBatchItemSize' (int): Max words per request, 1-2500 (default: 250)
5555 *
56- * @param array $config HTTP client credentials and optional batching overrides.
57- *
58- * @throws \InvalidArgumentException When the API key is missing or a value fails validation.
56+ * @throws \InvalidArgumentException When API key is missing or values fail validation
5957 */
6058 public function __construct (array $ config = [])
6159 {
@@ -95,13 +93,14 @@ public function __construct(array $config = [])
9593 }
9694
9795 /**
98- * Localize content using the Lingo.dev API
99- *
100- * @param array $payload The content to be localized
101- * @param array $params Localization parameters including source/target locales and fast mode option
102- * @param callable|null $progressCallback Optional callback function to report progress (0-100)
103- *
104- * @return array Localized content
96+ * Localize content using the Lingo.dev API.
97+ *
98+ * @param array<string, mixed> $payload Content to translate, structured as key-value pairs
99+ * @param array<string, mixed> $params Translation configuration options
100+ * @param callable(int, mixed, mixed): void|null $progressCallback Callback invoked with completion percentage (0-100), current chunk, and processed chunk
101+ *
102+ * @return array<string, mixed> Translated content maintaining original structure
103+ *
105104 * @internal
106105 */
107106 protected function localizeRaw (array $ payload , array $ params , ?callable $ progressCallback = null ): array
@@ -149,15 +148,18 @@ protected function localizeRaw(array $payload, array $params, ?callable $progres
149148 }
150149
151150 /**
152- * Localize a single chunk of content
153- *
154- * @param string|null $sourceLocale Source locale
155- * @param string $targetLocale Target locale
156- * @param array $payload Payload containing the chunk to be localized
157- * @param string $workflowId Workflow ID
158- * @param bool $fast Whether to use fast mode
159- *
160- * @return array Localized chunk
151+ * Localize a single chunk of content.
152+ *
153+ * @param string|null $sourceLocale Language code of the original text (e.g., 'en', 'es'), null for auto-detection
154+ * @param string $targetLocale Language code to translate into (e.g., 'fr', 'de')
155+ * @param array<string, mixed> $payload Content chunk with optional reference data for context
156+ * @param string $workflowId Unique identifier for tracking related translation requests
157+ * @param bool $fast Enable faster translation at potential quality tradeoff
158+ *
159+ * @return array<string, mixed> Translated chunk maintaining original structure
160+ *
161+ * @throws \InvalidArgumentException When reference is not an array
162+ * @throws \RuntimeException When API request fails
161163 */
162164 private function _localizeChunk (?string $ sourceLocale , string $ targetLocale , array $ payload , string $ workflowId , bool $ fast ): array
163165 {
@@ -214,11 +216,11 @@ private function _localizeChunk(?string $sourceLocale, string $targetLocale, arr
214216 }
215217
216218 /**
217- * Extract payload chunks based on the ideal chunk size
218- *
219- * @param array $payload The payload to be chunked
220- *
221- * @return array An array of payload chunks
219+ * Extract payload chunks based on the ideal chunk size.
220+ *
221+ * @param array<string, mixed> $payload The payload to be chunked
222+ *
223+ * @return array<int, array<string, mixed>> Array of payload chunks
222224 */
223225 private function _extractPayloadChunks (array $ payload ): array
224226 {
@@ -252,11 +254,11 @@ private function _extractPayloadChunks(array $payload): array
252254 }
253255
254256 /**
255- * Count words in a record or array
256- *
257+ * Count words in a record or array.
258+ *
257259 * @param mixed $payload The payload to count words in
258- *
259- * @return int The total number of words
260+ *
261+ * @return int Total number of words
260262 */
261263 private function _countWordsInRecord ($ payload ): int
262264 {
@@ -280,8 +282,8 @@ private function _countWordsInRecord($payload): int
280282 }
281283
282284 /**
283- * Generate a unique ID
284- *
285+ * Generate a unique ID.
286+ *
285287 * @return string Unique ID
286288 */
287289 private function _createId (): string
@@ -290,24 +292,20 @@ private function _createId(): string
290292 }
291293
292294 /**
293- * Localize every string in a nested array while keeping the array structure.
294- *
295- * Require $params['targetLocale'] with the desired language code. Optionally
296- * pass:
297- * - 'sourceLocale' with the current language code (string or null).
298- * - 'fast' with a boolean forwarded to the API.
299- * - 'reference' with an array of supplemental data forwarded unchanged.
300- * The optional callback receives (int $progress, array $chunk, array $localizedChunk)
301- * for each batch the engine submits.
295+ * Localize every string in a nested array while keeping its shape intact.
302296 *
303- * @param array $obj Input data whose string entries should be localized.
304- * @param array $params Request parameters for locales and API options.
305- * @param callable|null $progressCallback Progress hook fired per batch.
297+ * @param array<string, mixed> $obj Nested data structure containing text to translate
298+ * @param array<string, mixed> $params Parameters:
299+ * - 'targetLocale' (string, required): Language code to translate into (e.g., 'es', 'fr')
300+ * - 'sourceLocale' (string|null): Language code of original text, null for auto-detection
301+ * - 'fast' (bool): Trade translation quality for speed
302+ * - 'reference' (array): Context or glossary terms to guide translation
303+ * @param callable(int, mixed, mixed): void|null $progressCallback Invoked per batch with (percentage complete, current batch, translated batch)
306304 *
307- * @return array Localized data mirroring the original structure.
305+ * @return array<string, mixed> Translated data preserving original structure and non-text values
308306 *
309- * @throws \InvalidArgumentException When required params or reference data are invalid.
310- * @throws \RuntimeException When the API rejects or fails to process the request.
307+ * @throws \InvalidArgumentException When required params or reference data are invalid
308+ * @throws \RuntimeException When API rejects or fails to process the request
311309 */
312310 public function localizeObject (array $ obj , array $ params , ?callable $ progressCallback = null ): array
313311 {
@@ -323,24 +321,20 @@ public function localizeObject(array $obj, array $params, ?callable $progressCal
323321 }
324322
325323 /**
326- * Translate a single string and return the localized text.
324+ * Localize a single string and return the translated text.
327325 *
328- * Set $params['targetLocale'] to the destination language code. You may
329- * also provide :
330- * - 'sourceLocale' with the existing language code (string or null).
331- * - 'fast' with a boolean forwarded to the API.
332- * - 'reference' with an array of supplemental data forwarded unchanged.
333- * The optional callback receives the completion percentage (0-100) for each
334- * processed batch.
326+ * @param string $text Text content to translate
327+ * @param array<string, mixed> $params Parameters :
328+ * - 'targetLocale' (string, required): Language code to translate into (e.g., 'es', 'fr')
329+ * - 'sourceLocale' (string|null): Language code of original text, null for auto-detection
330+ * - 'fast' (bool): Prioritize speed over translation quality
331+ * - 'reference' (array): Context, terminology, or style guidelines for translation
332+ * @param callable(int): void|null $progressCallback Called with completion percentage (0-100) during processing
335333 *
336- * @param string $text Source text to localize.
337- * @param array $params Request parameters for locales and API options.
338- * @param callable|null $progressCallback Progress hook fired with an integer percentage.
334+ * @return string Translated text, or empty string if translation unavailable
339335 *
340- * @return string Localized text (empty string if the API omits the field).
341- *
342- * @throws \InvalidArgumentException When required params are missing or invalid.
343- * @throws \RuntimeException When the API rejects or fails to process the request.
336+ * @throws \InvalidArgumentException When required params are missing or invalid
337+ * @throws \RuntimeException When API rejects or fails to process the request
344338 */
345339 public function localizeText (string $ text , array $ params , ?callable $ progressCallback = null ): string
346340 {
@@ -358,20 +352,18 @@ public function localizeText(string $text, array $params, ?callable $progressCal
358352 }
359353
360354 /**
361- * Translate a string into several languages and return the results in order.
362- *
363- * Expect $params['sourceLocale'] with the language code of the input text
364- * and $params['targetLocales'] with an array of destination language codes.
365- * Optional 'fast' flags are forwarded to each {@see localizeText()} call.
366- * Any failure from an individual request surfaces immediately.
355+ * Localize a string into multiple languages and return texts in order.
367356 *
368- * @param string $text Source text to localize.
369- * @param array $params Request parameters describing the source and target languages.
357+ * @param string $text Text content to translate into multiple languages
358+ * @param array<string, mixed> $params Parameters:
359+ * - 'sourceLocale' (string, required): Language code of the original text (e.g., 'en')
360+ * - 'targetLocales' (string[], required): Array of language codes to translate into (e.g., ['es', 'fr', 'de'])
361+ * - 'fast' (bool): Apply speed optimization to all translations
370362 *
371- * @return array List of localized strings matching the order of target locales.
363+ * @return string[] Array of translated texts in same order as targetLocales parameter
372364 *
373- * @throws \InvalidArgumentException When required params are missing or invalid.
374- * @throws \RuntimeException When an individual localization request fails.
365+ * @throws \InvalidArgumentException When required params are missing or invalid
366+ * @throws \RuntimeException When an individual localization request fails
375367 */
376368 public function batchLocalizeText (string $ text , array $ params ): array
377369 {
@@ -398,25 +390,20 @@ public function batchLocalizeText(string $text, array $params): array
398390 }
399391
400392 /**
401- * Localize a chat transcript while keeping speaker names untouched .
393+ * Localize a chat transcript while preserving speaker names.
402394 *
403- * Each entry in $chat must provide 'name' and 'text'. Supply
404- * $params['targetLocale'] with the destination language code. Optionally
405- * pass:
406- * - 'sourceLocale' with the current language code (string or null).
407- * - 'fast' with a boolean forwarded to the API.
408- * - 'reference' with an array of supplemental data forwarded unchanged.
409- * The optional callback receives the completion percentage (0-100) for each
410- * processed batch.
395+ * @param array<int, array{name: string, text: string}> $chat Conversation history with speaker names and their messages
396+ * @param array<string, mixed> $params Parameters:
397+ * - 'targetLocale' (string, required): Language code to translate messages into (e.g., 'es', 'fr')
398+ * - 'sourceLocale' (string|null): Language of original messages, null for auto-detection
399+ * - 'fast' (bool): Optimize for speed over translation quality
400+ * - 'reference' (array): Conversation context or domain-specific terminology
401+ * @param callable(int): void|null $progressCallback Called with completion percentage (0-100) during processing
411402 *
412- * @param array $chat Ordered list of chat message arrays with 'name' and 'text'.
413- * @param array $params Request parameters for locales and API options.
414- * @param callable|null $progressCallback Progress hook fired with an integer percentage.
403+ * @return array<int, array{name: string, text: string}> Translated messages keeping original speaker names unchanged
415404 *
416- * @return array Localized chat messages with original names preserved.
417- *
418- * @throws \InvalidArgumentException When chat entries or params are invalid.
419- * @throws \RuntimeException When the API rejects or fails to process the request.
405+ * @throws \InvalidArgumentException When chat entries or params are invalid
406+ * @throws \RuntimeException When API rejects or fails to process the request
420407 */
421408 public function localizeChat (array $ chat , array $ params , ?callable $ progressCallback = null ): array
422409 {
@@ -448,17 +435,14 @@ public function localizeChat(array $chat, array $params, ?callable $progressCall
448435 }
449436
450437 /**
451- * Ask the API to identify the locale of the provided text.
452- *
453- * Whitespace-only strings are rejected. On success the API's 'locale'
454- * field is returned directly.
438+ * Identify the locale of the provided text.
455439 *
456- * @param string $text Text whose locale should be recognised.
440+ * @param string $text Sample text for language detection (longer text improves accuracy)
457441 *
458- * @return string Locale code provided by the API.
442+ * @return string ISO language code detected by the API (e.g., 'en', 'es', 'zh')
459443 *
460- * @throws \InvalidArgumentException When the input text is blank after trimming.
461- * @throws \RuntimeException When the API response is invalid or the request fails.
444+ * @throws \InvalidArgumentException When input text is blank after trimming
445+ * @throws \RuntimeException When API response is invalid or request fails
462446 */
463447 public function recognizeLocale (string $ text ): string
464448 {
0 commit comments