MessageSelector::choose()   A
last analyzed

Complexity

Conditions 5
Paths 4

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 5

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 8
c 1
b 0
f 1
dl 0
loc 12
rs 9.6111
ccs 8
cts 8
cp 1
cc 5
nc 4
nop 2
crap 5
1
<?php
2
declare(strict_types=1);
3
4
namespace Nexendrie\Translation;
5
6
use Nexendrie\Utils\Intervals;
7
use Nette\Utils\Strings;
8
9
/**
10
 * MessageSelector
11
 *
12
 * @author Jakub Konečný
13
 */
14 1
final class MessageSelector implements IMessageSelector {
15
  public function isMultiChoice(string $message): bool {
16 1
    return is_string(Intervals::findInterval($message)) && str_contains($message, "|");
17
  }
18
  
19
  public function choose(string $message, int $count): string {
20 1
    if(!$this->isMultiChoice($message)) {
21 1
      return $message;
22
    }
23 1
    $variants = explode("|", $message);
24 1
    foreach($variants as $variant) {
25 1
      $interval = Intervals::findInterval($variant);
26 1
      if(is_string($interval) && Intervals::isInInterval($count, $interval)) {
27 1
        return Strings::trim((string) Strings::after($variant, $interval));
28
      }
29
    }
30 1
    return "";
31
  }
32
}
33
?>