Skip to content

Commit

Permalink
Update getErrors() method
Browse files Browse the repository at this point in the history
Now the method can return not only error texts, but also templates of error texts for external translation.
  • Loading branch information
MioVisman committed Feb 24, 2021
1 parent 9f4da8b commit fa93c5e
Showing 1 changed file with 15 additions and 9 deletions.
24 changes: 15 additions & 9 deletions Parserus.php
Original file line number Diff line number Diff line change
Expand Up @@ -1374,23 +1374,29 @@ protected function stripEmptyTags_(string $mask, int $id): bool
*
* @param array $lang Массив строк шаблонов описания ошибок
* @param array $errors Массив, который дополняется ошибками
* @param bool $retTpl Флаг возрата результата в виде массива с шаблоном в первом элементе
*
* @return array
*/
public function getErrors(array $lang = [], array $errors = []): array
public function getErrors(array $lang = [], array $errors = [], bool $retTpl = false): array
{
foreach ($this->errors as $args) {
$err = array_shift($args);
$key = array_key_first($args);
$err = $args[$key];
$text = $lang[$err] ?? ($this->defLang[$err] ?? 'Unknown error');

if (isset($lang[$err])) {
$text = $lang[$err];
} elseif (isset($this->defLang[$err])) {
$text = $this->defLang[$err];
if ($retTpl) {
$args[$key] = $text;
$errors[] = $args;
} else {
$text = 'Unknown error';
$errors[] = vsprintf(
$text,
array_map(
[$this, 'e'],
array_slice($args, 1)
)
);
}

$errors[] = vsprintf($text, array_map([$this, 'e'], $args));
}

return $errors;
Expand Down

0 comments on commit fa93c5e

Please sign in to comment.