ConvertHelper_WordWrapper::wrapText()   B
last analyzed

Complexity

Conditions 8
Paths 8

Size

Total Lines 47
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 25
dl 0
loc 47
rs 8.4444
c 0
b 0
f 0
cc 8
nc 8
nop 1
1
<?php
2
/**
3
 * File containing the {@see AppUtils\ConvertHelper_WordWrapper} class.
4
 * 
5
 * @package Application Utils
6
 * @subpackage ConvertHelper
7
 * @see AppUtils\ConvertHelper_WordWrapper
8
 */
9
10
declare(strict_types=1);
11
12
namespace AppUtils;
13
14
/**
15
 * Wordwrap class that is used to wordwrap texts.
16
 * 
17
 * @package Application Utils
18
 * @subpackage ConvertHelper
19
 * @see https://stackoverflow.com/a/4988494/2298192
20
 */
21
class ConvertHelper_WordWrapper implements Interface_Optionable
22
{
23
    use Traits_Optionable;
24
    
25
    public function __construct()
26
    {
27
        
28
    }
29
    
30
    public function getDefaultOptions() : array
31
    {
32
        return array(
33
            'width' => 75,
34
            'break' => "\n",
35
            'cut' => false
36
        );
37
    }
38
    
39
    public function setLineWidth(int $width) : ConvertHelper_WordWrapper
40
    {
41
        $this->setOption('width', $width);
42
        return $this;
43
    }
44
    
45
    public function getLineWidth() : int
46
    {
47
        return $this->getIntOption('width');
48
    }
49
    
50
    public function setBreakCharacter(string $char) : ConvertHelper_WordWrapper
51
    {
52
        $this->setOption('break', $char);
53
        return $this;
54
    }
55
    
56
    public function getBreakCharacter() : string
57
    {
58
        return $this->getStringOption('break');
59
    }
60
    
61
    public function isCuttingEnabled() : bool
62
    {
63
        return $this->getBoolOption('cut');
64
    }
65
    
66
    public function setCuttingEnabled(bool $enabled=true) : ConvertHelper_WordWrapper
67
    {
68
        $this->setOption('cut', $enabled);
69
        return $this;
70
    }
71
    
72
    public function wrapText(string $text) : string
73
    {
74
        $break = $this->getBreakCharacter();
75
        $width = $this->getLineWidth();
76
        $cut = $this->isCuttingEnabled();
77
        
78
        $lines = explode($break, $text);
79
        
80
        foreach ($lines as &$line)
81
        {
82
            $line = rtrim($line);
83
            if (mb_strlen($line) <= $width) {
84
                continue;
85
            }
86
            
87
            $words = explode(' ', $line);
88
            $line = '';
89
            $actual = '';
90
            foreach ($words as $word)
91
            {
92
                if (mb_strlen($actual.$word) <= $width)
93
                {
94
                    $actual .= $word.' ';
95
                }
96
                else
97
                {
98
                    if ($actual != '') {
99
                        $line .= rtrim($actual).$break;
100
                    }
101
                    
102
                    $actual = $word;
103
                    if ($cut)
104
                    {
105
                        while (mb_strlen($actual) > $width) {
106
                            $line .= mb_substr($actual, 0, $width).$break;
107
                            $actual = mb_substr($actual, $width);
108
                        }
109
                    }
110
                    
111
                    $actual .= ' ';
112
                }
113
            }
114
            
115
            $line .= trim($actual);
116
        }
117
        
118
        return implode($break, $lines);
119
    }
120
}
121