Completed
Push — master ( e4e762...910c9d )
by Harry
14s
created

Wrapper::chunk()   C

Complexity

Conditions 7
Paths 9

Size

Total Lines 28
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 28
rs 6.7272
c 0
b 0
f 0
cc 7
eloc 15
nc 9
nop 1
1
<?php
2
3
namespace Graze\BufferedConsole\Wrap;
4
5
use Graze\BufferedConsole\Terminal\DimensionsInterface;
6
7
class Wrapper
8
{
9
    /** @var int */
10
    private $width = DimensionsInterface::DEFAULT_WIDTH;
11
12
    /**
13
     * Wrapper constructor.
14
     *
15
     * @param int $width
16
     */
17
    public function __construct($width)
18
    {
19
        $this->width = $width;
20
    }
21
22
    /**
23
     * @return int
24
     */
25
    public function getWidth()
26
    {
27
        return $this->width;
28
    }
29
30
    /**
31
     * @param int $width
32
     *
33
     * @return $this
34
     */
35
    public function setWidth($width)
36
    {
37
        $this->width = $width;
38
        return $this;
39
    }
40
41
    /**
42
     * @param string[] $input
43
     *
44
     * @return string[]
45
     */
46 View Code Duplication
    public function wrap(array $input)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
47
    {
48
        $out = [];
49
        foreach ($input as $line) {
50
            foreach ($this->chunk($line) as $new) {
51
                $out[] = $new;
52
            }
53
        }
54
        return $out;
55
    }
56
57
    /**
58
     * @param string[] $input
59
     *
60
     * @return string[]
61
     */
62 View Code Duplication
    public function trim(array $input)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
63
    {
64
        $out = [];
65
        foreach ($input as $line) {
66
            $chunk = $this->chunk($line);
67
            $out[] = reset($chunk);
68
        }
69
        return $out;
70
    }
71
72
    /**
73
     * @param string $line
74
     *
75
     * @return string[]
76
     */
77
    private function chunk($line)
78
    {
79
        if (mb_strlen($line) <= $this->width) {
80
            return [$line];
81
        }
82
        $stripped = strip_tags($line);
83
        $offset = 0;
84
        $out = [];
85
86
        // create a stripped tags version of the string
87
        // loop through both and only move the counter if both characters are equal
88
        // yield when we get to <width> for the stripped tags version
89
        for ($i = 0, $j = 0; $i <= mb_strlen($stripped); $j++) {
90
            if (mb_substr($stripped, $i, 1) === mb_substr($line, $j, 1)) {
91
                if ($i >= $this->width && $i % $this->width == 0) {
92
                    $out[] = mb_substr($line, $offset, $j - $offset);
93
                    $offset = $j;
94
                }
95
                $i++;
96
            }
97
        }
98
99
        // return any remaining entries
100
        if ($offset != $j - 1) {
101
            $out[] = mb_substr($line, $offset);
102
        }
103
        return $out;
104
    }
105
}
106