Completed
Pull Request — master (#63)
by Robert
03:15
created

AsciiArtItem::getArtLength()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace PhpSchool\CliMenu\MenuItem;
4
5
use Assert\Assertion;
6
use PhpSchool\CliMenu\MenuStyle;
7
8
/**
9
 * Class AsciiArtItem
10
 *
11
 * @package PhpSchool\CliMenu\MenuItem
12
 * @author Michael Woodward <[email protected]>
13
 */
14
class AsciiArtItem implements MenuItemInterface
15
{
16
    /**
17
     * Possible positions of the ascii art
18
     */
19
    const POSITION_CENTER = 'center';
20
    const POSITION_LEFT   = 'left';
21
    const POSITION_RIGHT  = 'right';
22
    
23
    /**
24
     * @var string
25
     */
26
    private $text;
27
28
    /**
29
     * @var string
30
     */
31
    private $position;
32
33
    /**
34
     * @var int
35
     */
36
    private $artLength;
37
38
    /**
39
     * @param string $text
40
     * @param string $position
41
     */
42
    public function __construct($text, $position = self::POSITION_CENTER)
43
    {
44
        Assertion::string($text);
45
        Assertion::inArray($position, [self::POSITION_CENTER, self::POSITION_RIGHT, self::POSITION_LEFT]);
46
        
47
        $this->text      = $text;
48
        $this->position  = $position;
49
        $this->artLength = max(array_map('mb_strlen', explode("\n", $text)));
50
    }
51
52
    /**
53
     * The output text for the item
54
     *
55
     * @param MenuStyle $style
56
     * @param bool $selected
57
     * @return array
58
     */
59
    public function getRows(MenuStyle $style, $selected = false)
60
    {
61
        return array_map(function ($row) use ($style) {
62
            $length = mb_strlen($row);
63
64
            $padding = $style->getContentWidth() - $length;
65
66
            switch ($this->position) {
67
                case self::POSITION_LEFT:
68
                    return $row;
69
                    break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
70
                case self::POSITION_RIGHT:
71
                    $row = rtrim($row);
72
                    $padding = $padding - ($this->artLength - mb_strlen($row));
73
                    $row = sprintf('%s%s', str_repeat(' ', $padding), $row);
74
                    break;
75
                case self::POSITION_CENTER:
76
                default:
77
                    $row = rtrim($row);
78
                    $padding = $padding - ($this->artLength - mb_strlen($row));
79
                    $left = ceil($padding/2);
80
                    $right = $padding - $left;
81
                    $row = sprintf('%s%s%s', str_repeat(' ', $left), $row, str_repeat(' ', $right));
82
                    break;
83
            }
84
85
            return $row;
86
        }, explode("\n", $this->text));
87
    }
88
89
    /**
90
     * Can the item be selected
91
     *
92
     * @return bool
93
     */
94
    public function canSelect()
95
    {
96
        return false;
97
    }
98
99
    /**
100
     * Execute the items callable if required
101
     *
102
     * @return void
103
     */
104
    public function getSelectAction()
105
    {
106
        return;
107
    }
108
109
    /**
110
     * Return the raw string of text
111
     *
112
     * @return string
113
     */
114
    public function getText()
115
    {
116
        return $this->text;
117
    }
118
119
    /**
120
     * Return the length of the art
121
     *
122
     * @return int
123
     */
124
    public function getArtLength()
125
    {
126
        return $this->artLength;
127
    }
128
129
    /**
130
     * Whether or not the menu item is showing the menustyle extra value
131
     *
132
     * @return bool
133
     */
134
    public function showsItemExtra()
135
    {
136
        return false;
137
    }
138
139
    /**
140
     * Enable showing item extra
141
     *
142
     */
143
    public function showItemExtra()
144
    {
145
        //noop
146
    }
147
148
    /**
149
     * Disable showing item extra
150
     *
151
     */
152
    public function hideItemExtra()
153
    {
154
        //noop
155
    }
156
}
157