Padding::padContent()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 8
cts 8
cp 1
rs 9.8666
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 2
1
<?php
2
3
namespace League\CLImate\TerminalObject\Dynamic;
4
5
class Padding extends DynamicTerminalObject
6
{
7
    /**
8
     * The length that lines should be padded to
9
     *
10
     * @var integer $length
11
     */
12
    protected $length = 0;
13
14
    /**
15
     * The character(s) that should be used to pad
16
     *
17
     * @var string $char
18
     */
19
    protected $char = '.';
20
21
22
    /**
23
     * If they pass in a padding character, set the char
24
     *
25
     * @param int $length
26
     * @param string $char
27
     */
28 16
    public function __construct($length = null, $char = null)
29
    {
30 16
        if ($length !== null) {
31 12
            $this->length($length);
32 12
        }
33
34 16
        if (is_string($char)) {
35
            $this->char($char);
36
        }
37 16
    }
38
39
    /**
40
     * Set the character(s) that should be used to pad
41
     *
42
     * @param string $char
43
     *
44
     * @return \League\CLImate\TerminalObject\Dynamic\Padding
45
     */
46 8
    public function char($char)
47
    {
48 8
        $this->char = $char;
49
50 8
        return $this;
51
    }
52
53
    /**
54
     * Set the length of the line that should be generated
55
     *
56
     * @param integer $length
57
     *
58
     * @return \League\CLImate\TerminalObject\Dynamic\Padding
59
     */
60 12
    public function length($length)
61
    {
62 12
        $this->length = $length;
63
64 12
        return $this;
65
    }
66
67
    /**
68
     * Get the length of the line based on the width of the terminal window
69
     *
70
     * @return integer
71
     */
72 16
    protected function getLength()
73
    {
74 16
        if (!$this->length) {
75 4
            $this->length = $this->util->width();
76 4
        }
77
78 16
        return $this->length;
79
    }
80
81
    /**
82
     * Pad the content with the characters
83
     *
84
     * @param string $content
85
     *
86
     * @return string
87
     */
88 16
    protected function padContent($content)
89
    {
90 16
        if (strlen($this->char) > 0) {
91 16
            $length = $this->getLength();
92 16
            $padding_length = ceil($length / mb_strlen($this->char));
93
94 16
            $padding = str_repeat($this->char, $padding_length);
95 16
            $content .= mb_substr($padding, 0, $length - mb_strlen($content));
96 16
        }
97
98 16
        return $content;
99
    }
100
101
    /**
102
     * Output the content and pad to the previously defined length
103
     *
104
     * @param string $content
105
     *
106
     * @return \League\CLImate\TerminalObject\Dynamic\Padding
107
     */
108 16
    public function label($content)
109
    {
110
        // Handle long labels by splitting them across several lines
111 16
        $lines = [];
112 16
        $stop = mb_strlen($content);
113
        $width = $this->util->width();
114 16
        for ($i = 0; $i < $stop; $i += $width) {
115 4
            $lines[] = mb_substr($content, $i, $width);
116 16
        }
117
        $content = array_pop($lines);
118 16
119 16
        foreach ($lines as $line) {
120
            $this->output->write($this->parser->apply($line));
121 16
        }
122 16
123
        $content = $this->padContent($content);
124 16
        $content = $this->parser->apply($content);
125
126
        $this->output->sameLine();
127
        $this->output->write($content);
128
129
        return $this;
130
    }
131
132 16
    /**
133
     * Output result
134 16
     *
135 16
     * @param string $content
136
     */
137
    public function result($content)
138
    {
139
        $this->output->write($this->parser->apply(' ' . $content));
140
    }
141
}
142