Completed
Pull Request — master (#183)
by Aydin
03:07
created

AsciiArtItem::showItemExtra()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 0
nc 1
nop 0
dl 0
loc 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace PhpSchool\CliMenu\MenuItem;
4
5
use Assert\Assertion;
6
use PhpSchool\CliMenu\MenuStyle;
7
8
/**
9
 * @author Michael Woodward <[email protected]>
10
 */
11
class AsciiArtItem implements MenuItemInterface
12
{
13
    /**
14
     * Possible positions of the ascii art
15
     */
16
    const POSITION_CENTER = 'center';
17
    const POSITION_LEFT   = 'left';
18
    const POSITION_RIGHT  = 'right';
19
20
    /**
21
     * @var string
22
     */
23
    private $text;
24
25
    /**
26
     * @var string
27
     */
28
    private $position;
29
30
    /**
31
     * @var string
32
     */
33
    private $alternateText;
34
35
    /**
36
     * @var int
37
     */
38
    private $artLength;
39
40
    public function __construct(string $text, string $position = self::POSITION_CENTER, string $alt = '')
41
    {
42
        Assertion::inArray($position, [self::POSITION_CENTER, self::POSITION_RIGHT, self::POSITION_LEFT]);
43
44
        $this->setText($text);
45
        $this->position  = $position;
46
        $this->alternateText = $alt;
47
    }
48
49
    /**
50
     * The output text for the item
51
     */
52
    public function getRows(MenuStyle $style, bool $selected = false) : array
53
    {
54
        if ($this->artLength > $style->getContentWidth()) {
55
            $alternate = new StaticItem($this->alternateText);
56
            return $alternate->getRows($style, false);
57
        }
58
59
        $padding = $style->getContentWidth() - $this->artLength;
60
61
        return array_map(function ($row) use ($padding) {
62
            switch ($this->position) {
63
                case self::POSITION_LEFT:
64
                    break;
65
                case self::POSITION_RIGHT:
66
                    $row = sprintf('%s%s', str_repeat(' ', $padding), $row);
67
                    break;
68
                case self::POSITION_CENTER:
69
                default:
70
                    $left = (int) ceil($padding / 2);
71
                    $row = sprintf('%s%s', str_repeat(' ', $left), $row);
72
                    break;
73
            }
74
75
            return $row;
76
        }, explode("\n", $this->text));
77
    }
78
79
    /**
80
     * Can the item be selected
81
     */
82
    public function canSelect() : bool
83
    {
84
        return false;
85
    }
86
87
    /**
88
     * Execute the items callable if required
89
     */
90
    public function getSelectAction() : ?callable
91
    {
92
        return null;
93
    }
94
95
    /**
96
     * Return the raw string of text
97
     */
98
    public function getText() : string
99
    {
100
        return $this->text;
101
    }
102
103
    /**
104
     * Set the raw string of text
105
     */
106
    public function setText(string $text) : void
107
    {
108
        $this->text = implode("\n", array_map(function (string $line) {
109
            return rtrim($line, ' ');
110
        }, explode("\n", $text)));
111
112
        $this->calculateArtLength();
113
    }
114
115
    /**
116
     * Calculate the length of the art
117
     */
118
    private function calculateArtLength() : void
119
    {
120
        $this->artLength = (int) max(array_map('mb_strlen', explode("\n", $this->text)));
121
    }
122
123
    /**
124
     * Return the length of the art
125
     */
126
    public function getArtLength() : int
127
    {
128
        return $this->artLength;
129
    }
130
131
    public function getPosition() : string
132
    {
133
        return $this->position;
134
    }
135
136
    public function getAlternateText() : string
137
    {
138
        return $this->alternateText;
139
    }
140
141
    /**
142
     * Whether or not the menu item is showing the menustyle extra value
143
     */
144
    public function showsItemExtra() : bool
145
    {
146
        return false;
147
    }
148
149
    /**
150
     * Enable showing item extra
151
     */
152
    public function showItemExtra() : void
153
    {
154
        //noop
155
    }
156
157
    /**
158
     * Disable showing item extra
159
     */
160
    public function hideItemExtra() : void
161
    {
162
        //noop
163
    }
164
}
165