Completed
Push — master ( 306131...5909ec )
by Aydin
06:33 queued 03:57
created

AsciiArtItem::calculateArtLength()   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
 * @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 = 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 = 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
    /**
132
     * Whether or not the menu item is showing the menustyle extra value
133
     */
134
    public function showsItemExtra() : bool
135
    {
136
        return false;
137
    }
138
139
    /**
140
     * Enable showing item extra
141
     */
142
    public function showItemExtra() : void
143
    {
144
        //noop
145
    }
146
147
    /**
148
     * Disable showing item extra
149
     */
150
    public function hideItemExtra() : void
151
    {
152
        //noop
153
    }
154
}
155