Completed
Push — master ( 737d23...df8264 )
by Joachim
04:33
created

Message::isUnicode()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 0
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 2
1
<?php declare(strict_types=1);
2
namespace Loevgaard\Linkmobility\ValueObject;
3
4
use Loevgaard\Linkmobility\GSM7\GSM7;
5
6
class Message extends StringValueObject
7
{
8
    /**
9
     * @var bool
10
     */
11
    private $gsm7;
12
13 1
    public function chunkCount() : int
14
    {
15 1
        $length = $this->length();
16 1
        $lengthThreshold = $this->isGsm7() ? 160 : 70;
17
18 1
        if ($length > $lengthThreshold) {
19 1
            $chunkDivisor = $this->isGsm7() ? 153 : 67;
20 1
            return (int)ceil($length / $chunkDivisor);
21
        }
22
23 1
        return 1;
24
    }
25
26 2
    public function length() : int
27
    {
28 2
        $length = mb_strlen($this->value);
29
30 2
        if (!$this->isGsm7()) {
31 2
            return $length;
32
        }
33
34 2
        $doubles = GSM7::DOUBLES;
35
36 2
        for ($i = 0; $i < $length; $i++) {
37 2
            $char = mb_substr($this->value, $i, 1);
38 2
            if (in_array($char, $doubles)) {
39 1
                $length++;
40
            }
41
        }
42
43 2
        return $length;
44
    }
45
46 2
    public function isGsm7() : bool
47
    {
48 2
        if (is_null($this->gsm7)) {
49 2
            $this->gsm7 = GSM7::isGSM7($this->value);
50
        }
51
52 2
        return $this->gsm7;
53
    }
54
55
    public function isUnicode() : bool
56
    {
57
        return !$this->isGsm7();
58
    }
59
}
60