StringLength::arrayOfStrLens()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
namespace League\CLImate\TerminalObject\Helper;
4
5
trait StringLength
6
{
7
    /**
8
     * Tags the should not be ultimately considered
9
     * when calculating any string lengths
10
     *
11
     * @var array $ignore_tags
12
     */
13
    protected $ignore_tags = [];
14
15
    /**
16
     * Set the ignore tags property
17
     */
18 104
    protected function setIgnoreTags()
19
    {
20 104
        if (!count($this->ignore_tags)) {
21 104
            $this->ignore_tags = array_keys($this->parser->tags->all());
22 104
        }
23 104
    }
24
25
    /**
26
     * Determine the length of the string without any tags
27
     *
28
     * @param  string  $str
29
     *
30
     * @return integer
31
     */
32 104
    protected function lengthWithoutTags($str)
33
    {
34 104
        $this->setIgnoreTags();
35
36 104
        return mb_strwidth($this->withoutTags($str), 'UTF-8');
37
    }
38
39
    /**
40
     * Get the string without the tags that are to be ignored
41
     *
42
     * @param  string $str
43
     *
44
     * @return string
45
     */
46 104
    protected function withoutTags($str)
47
    {
48 104
        $this->setIgnoreTags();
49
50 104
        return str_replace($this->ignore_tags, '', $str);
51
    }
52
53
    /**
54
     * Apply padding to a string
55
     *
56
     * @param string $str
57
     * @param string $final_length
58
     * @param string $padding_side
59
     *
60
     * @return string
61
     */
62 88
    protected function pad($str, $final_length, $padding_side = 'right')
63
    {
64 88
        $padding = $final_length - $this->lengthWithoutTags($str);
65
66 88
        if ($padding_side == 'left') {
67
            return str_repeat(' ', $padding) . $str;
68
        }
69
70 88
        return $str . str_repeat(' ', $padding);
71
    }
72
73
    /**
74
     * Apply padding to an array of strings
75
     *
76
     * @param  array $arr
77
     * @param  integer $final_length
78
     * @param string $padding_side
79
     *
80
     * @return array
81
     */
82 28
    protected function padArray($arr, $final_length, $padding_side = 'right')
83
    {
84 28
        foreach ($arr as $key => $value) {
85 28
            $arr[$key] = $this->pad($value, $final_length, $padding_side);
86 28
        }
87
88 28
        return $arr;
89
    }
90
91
    /**
92
     * Find the max string length in an array
93
     *
94
     * @param array $arr
95
     * @return int
96
     */
97 52
    protected function maxStrLen(array $arr)
98
    {
99 52
        return max($this->arrayOfStrLens($arr));
100
    }
101
102
    /**
103
     * Get an array of the string lengths from an array of strings
104
     *
105
     * @param array $arr
106
     * @return array
107
     */
108 80
    protected function arrayOfStrLens(array $arr)
109
    {
110 80
        return array_map([$this, 'lengthWithoutTags'], $arr);
111
    }
112
}
113