SplitterText::split()   C
last analyzed

Complexity

Conditions 8
Paths 9

Size

Total Lines 33
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 33
rs 5.3846
cc 8
eloc 25
nc 9
nop 2
1
<?php
2
3
namespace TTS\Helper;
4
5
class SplitterText extends AbstractHelper
6
{
7
    public function split($text, $numSymbols)
8
    {
9
        $lines = [];
10
        $char = 0;
11
        $lastPos = 0;
12
        $goodChar = 0;
13
14
        for ($i = 0; $i < mb_strlen($text); $i++) {
15
            $char++;
16
17
            if (mb_substr($text, $i, 1) == ' ') {
18
                $goodChar = $i;
19
            }
20
21
            if ($char > $numSymbols) {
22
                $lines[] = trim(mb_substr($text, $lastPos, $goodChar - $lastPos));
23
                $char = 0;
24
                $lastPos = $goodChar;
25
            } elseif (mb_substr($text, $i, 2) == '. ' && ($goodChar - $lastPos > 5)) {
26
                $goodChar = $i + 1;
27
                $lines[] = trim(mb_substr($text, $lastPos, $goodChar - $lastPos));
28
                $char = 0;
29
                $lastPos = $goodChar;
30
            } elseif (mb_substr($text, $i, 2) == '! ' && ($goodChar - $lastPos > 5)) {
31
                $goodChar = $i + 1;
32
                $lines[] = trim(mb_substr($text, $lastPos, $goodChar - $lastPos));
33
                $char = 0;
34
                $lastPos = $goodChar;
35
            }
36
        }
37
        $lines[] = trim(mb_substr($text, $lastPos, $lastPos + $char));
38
        return $lines;
39
    }
40
}
41