Translator   A
last analyzed

Complexity

Total Complexity 19

Size/Duplication

Total Lines 102
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 5

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 102
ccs 40
cts 40
cp 1
c 0
b 0
f 0
wmc 19
lcom 2
cbo 5
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A getLang() 0 3 1
A setLang() 0 3 1
A getUntranslated() 0 3 1
A extractDomainAndMessage() 0 6 2
A multiLevelTrans() 0 10 3
A multiChoiceTrans() 0 10 4
A logUntranslatedMessage() 0 3 1
B translate() 0 21 5
1
<?php
2
declare(strict_types=1);
3
4
namespace Nexendrie\Translation;
5
6
use Nette\Utils\Arrays,
7
    Nette\Utils\Strings,
8
    Nette\Localization\ITranslator,
9
    Nexendrie\Translation\Loaders\ILoader,
10
    Nexendrie\Utils\Intervals;
11
12
/**
13
 * Translator
14
 *
15
 * @author Jakub Konečný
16
 * @property string $lang
17
 * @property-read string[] $untranslated
18
 * @method void onUntranslated(string $message)
19
 */
20 1
class Translator implements ITranslator {
21 1
  use \Nette\SmartObject;
22
  
23
  /** @var ILoader */
24
  protected $loader;
25
  /** @var string[] */
26
  protected $untranslated = [];
27
  /** @var callable[] */
28
  public $onUntranslated = [];
29
  
30
  public function __construct(ILoader $loader) {
31 1
    $this->loader = $loader;
32 1
  }
33
  
34
  public function getLang(): string {
0 ignored issues
show
Coding Style introduced by Jakub Konečný
Expected 1 space after closing parenthesis; found 0
Loading history...
35 1
    return $this->loader->getLang();
36
  }
37
  
38
  public function setLang(string $lang) {
39 1
    $this->loader->setLang($lang);
40 1
  }
41
  
42
  /**
43
   * @return string[]
44
   */
45
  public function getUntranslated(): array {
0 ignored issues
show
Coding Style introduced by Jakub Konečný
Expected 1 space after closing parenthesis; found 0
Loading history...
46 1
    return $this->untranslated;
47
  }
48
  
49
  /**
50
   * @param string $message
51
   * @return string[]
52
   */
53
  protected function extractDomainAndMessage(string $message): array {
0 ignored issues
show
Coding Style introduced by Jakub Konečný
Expected 1 space after closing parenthesis; found 0
Loading history...
54 1
    if(!Strings::contains($message, ".")) {
55 1
      return ["messages", $message];
56
    }
57 1
    return explode(".", $message, 2);
58
  }
59
  
60
  /**
61
   * Translate multi-level message
62
   */
63
  protected function multiLevelTrans(array $message, array $texts): string {
0 ignored issues
show
Coding Style introduced by Jakub Konečný
Expected 1 space after closing parenthesis; found 0
Loading history...
64 1
    $text = $texts;
65 1
    foreach($message as $part) {
66 1
      $text = Arrays::get($text, $part, "");
67 1
      if($text === "") {
68 1
        break;
69
      }
70
    }
71 1
    return $text;
72
  }
73
  
74
  /**
75
   * Choose correct variant of message depending on $count
76
   */
77
  protected function multiChoiceTrans(string $message, int $count): string {
0 ignored issues
show
Coding Style introduced by Jakub Konečný
Expected 1 space after closing parenthesis; found 0
Loading history...
78 1
    $variants = explode("|", $message);
79 1
    foreach($variants as $variant) {
80 1
      $interval = Intervals::findInterval($variant);
81 1
      if(is_string($interval) AND Intervals::isInInterval($count, $interval)) {
82 1
        return Strings::trim(Strings::after($variant, $interval));
83
      }
84
    }
85 1
    return "";
86
  }
87
  
88
  public function logUntranslatedMessage(string $message): void {
0 ignored issues
show
Coding Style introduced by Jakub Konečný
Expected 1 space after closing parenthesis; found 0
Loading history...
89 1
    $this->untranslated[] = $message;
90 1
  }
91
  
92
  /**
93
   * Translate the string
94
   *
95
   * @param string $message
96
   * @param int $count
97
   * @param array $params
98
   * @return string
99
   */
100
  public function translate($message, $count = 0, $params = []) {
101 1
    list($domain, $m) = $this->extractDomainAndMessage($message);
102 1
    $texts = Arrays::get($this->loader->getTexts(), $domain, []);
0 ignored issues
show
Coding Style introduced by Jakub Konečný
Equals sign not aligned with surrounding assignments; expected 12 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
103 1
    $parts = explode(".", $m);
0 ignored issues
show
Coding Style introduced by Jakub Konečný
Equals sign not aligned with surrounding assignments; expected 12 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
104 1
    if(count($parts) === 1) {
105 1
      $parts = [$m];
106
    }
107 1
    $text = $this->multiLevelTrans($parts, $texts);
108 1
    if($text === "") {
109 1
      $this->onUntranslated($message);
110 1
      return $message;
111
    }
112 1
    if(is_string(Intervals::findInterval($text))) {
113 1
      $text = $this->multiChoiceTrans($text, $count);
114
    }
115 1
    $params["count"] = $count;
116 1
    foreach($params as $key => $value) {
117 1
      $text = str_replace("%$key%", $value, $text);
118
    }
119 1
    return $text;
120
  }
121
}
122
?>