Completed
Push — version-4-wip ( 38f335 )
by Craig
03:27
created

Padding::getLength()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 5
cts 5
cp 1
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
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 24
    public function __construct($length = null, $char = null)
29
    {
30 24
        if ($length !== null) {
31 16
            $this->length($length);
32 8
        }
33
34 24
        if (is_string($char)) {
35
            $this->char($char);
36
        }
37 24
    }
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 16
    public function length($length)
61
    {
62 16
        $this->length = $length;
63
64 16
        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 24
    protected function getLength()
73
    {
74 24
        if (!$this->length) {
75 8
            $this->length = $this->util->width();
76 4
        }
77
78 24
        return $this->length;
79
    }
80
81
    /**
82
     * Pad the content with the characters
83
     *
84
     * @param string $content
85
     *
86
     * @return string
87
     */
88 24
    protected function padContent($content)
89
    {
90 24
        if (strlen($this->char) > 0) {
91 24
            $length = $this->getLength();
92 24
            $padding_length = ceil($length / mb_strlen($this->char));
93
94 24
            $padding = str_repeat($this->char, $padding_length);
95 24
            $content .= mb_substr($padding, 0, $length - mb_strlen($content));
96 12
        }
97
98 24
        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 24
    public function label($content)
109
    {
110
        // Handle long labels by splitting them across several lines
111 24
        $lines = [];
112 24
        $stop = mb_strlen($content);
113 24
        $width = $this->util->width();
114 24
        for ($i = 0; $i < $stop; $i += $width) {
115 24
            $lines[] = mb_substr($content, $i, $width);
116 12
        }
117 24
        $content = array_pop($lines);
118
119 24
        foreach ($lines as $line) {
120 8
            $this->output->write($this->parser->apply($line));
121 12
        }
122
123 24
        $content = $this->padContent($content);
124 24
        $content = $this->parser->apply($content);
125
126 24
        $this->output->sameLine();
127 24
        $this->output->write($content);
128
129 24
        return $this;
130
    }
131
132
    /**
133
     * Output result
134
     *
135
     * @param string $content
136
     */
137 24
    public function result($content)
138
    {
139 24
        $this->output->write($this->parser->apply(' ' . $content));
140 24
    }
141
}
142