Completed
Pull Request — master (#40)
by Michael
03:22 queued 50s
created

StringUtil   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 3
lcom 0
cbo 0
dl 0
loc 35
rs 10
c 1
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A wordwrap() 0 14 2
A stripAnsiEscapeSequence() 0 4 1
1
<?php
2
3
namespace PhpSchool\CliMenu\Util;
4
5
/**
6
 * Class StringUtil
7
 *
8
 * @package PhpSchool\CliMenu\Util
9
 * @author Michael Woodward <[email protected]>
10
 */
11
class StringUtil
12
{
13
    /**
14
     * Minimal multi-byte wordwrap implementation
15
     * which also takes break length into consideration
16
     *
17
     * @param string $str
18
     * @param int $width
19
     * @param string $break
20
     * @return string
21
     */
22
    public static function wordwrap($str, $width, $break = "\n")
23
    {
24
        $length = 0;
25
        return implode(" ", array_map(function ($word) use (&$length, $width, $break) {
26
            $length += (mb_strlen($word) + 1);
27
28
            if ($length > $width) {
29
                $length = mb_strlen($break);
30
                return sprintf("%s%s", $break, $word);
31
            }
32
33
            return $word;
34
        }, explode(" ", $str)));
35
    }
36
37
    /**
38
     * @param string $str
39
     * @return string
40
     */
41
    public static function stripAnsiEscapeSequence($str)
42
    {
43
        return preg_replace('/\x1b[^m]*m/', '', $str);
44
    }
45
}
46