|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace PhpSchool\CliMenu\Dialogue; |
|
4
|
|
|
|
|
5
|
|
|
use PhpSchool\CliMenu\CliMenu; |
|
6
|
|
|
use PhpSchool\CliMenu\Exception\MenuNotOpenException; |
|
7
|
|
|
use PhpSchool\CliMenu\MenuStyle; |
|
8
|
|
|
use PhpSchool\CliMenu\Terminal\TerminalInterface; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* @author Aydin Hassan <[email protected]> |
|
12
|
|
|
*/ |
|
13
|
|
|
abstract class Dialogue |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* @var MenuStyle |
|
17
|
|
|
*/ |
|
18
|
|
|
protected $style; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @var CliMenu |
|
22
|
|
|
*/ |
|
23
|
|
|
protected $parentMenu; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @var TerminalInterface |
|
27
|
|
|
*/ |
|
28
|
|
|
protected $terminal; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var string $text |
|
32
|
|
|
*/ |
|
33
|
|
|
protected $text; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @var int |
|
37
|
|
|
*/ |
|
38
|
|
|
protected $x; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @var int |
|
42
|
|
|
*/ |
|
43
|
|
|
protected $y; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @param CliMenu $parentMenu |
|
47
|
|
|
* @param MenuStyle $menuStyle |
|
48
|
|
|
* @param TerminalInterface $terminal |
|
49
|
|
|
* @param string $text |
|
50
|
|
|
*/ |
|
51
|
|
|
public function __construct(CliMenu $parentMenu, MenuStyle $menuStyle, TerminalInterface $terminal, $text) |
|
52
|
|
|
{ |
|
53
|
|
|
$this->style = $menuStyle; |
|
54
|
|
|
$this->terminal = $terminal; |
|
55
|
|
|
$this->text = $text; |
|
56
|
|
|
$this->parentMenu = $parentMenu; |
|
57
|
|
|
|
|
58
|
|
|
$this->calculateCoordinates(); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @throws MenuNotOpenException |
|
63
|
|
|
*/ |
|
64
|
|
|
protected function assertMenuOpen() |
|
65
|
|
|
{ |
|
66
|
|
|
if (!$this->parentMenu->isOpen()) { |
|
67
|
|
|
throw new MenuNotOpenException; |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Calculate the co-ordinates to write the messages |
|
73
|
|
|
*/ |
|
74
|
|
|
protected function calculateCoordinates() |
|
75
|
|
|
{ |
|
76
|
|
|
//y |
|
77
|
|
|
$textLines = count(explode("\n", $this->text)) + 2; |
|
78
|
|
|
$this->y = ceil($this->parentMenu->getCurrentFrame()->count() / 2) - ceil($textLines / 2) + 1; |
|
|
|
|
|
|
79
|
|
|
|
|
80
|
|
|
//x |
|
81
|
|
|
$parentStyle = $this->parentMenu->getStyle(); |
|
82
|
|
|
$dialogueHalfLength = (mb_strlen($this->text) + ($this->style->getPadding() * 2)) / 2; |
|
83
|
|
|
$widthHalfLength = ceil($parentStyle->getWidth() / 2); |
|
84
|
|
|
$this->x = $widthHalfLength - $dialogueHalfLength; |
|
|
|
|
|
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Write an empty row |
|
89
|
|
|
*/ |
|
90
|
|
|
protected function emptyRow() |
|
91
|
|
|
{ |
|
92
|
|
|
$this->write( |
|
93
|
|
|
sprintf( |
|
94
|
|
|
"%s%s%s%s%s\n", |
|
95
|
|
|
$this->style->getUnselectedSetCode(), |
|
96
|
|
|
str_repeat(' ', $this->style->getPadding()), |
|
97
|
|
|
str_repeat(' ', mb_strlen($this->text)), |
|
98
|
|
|
str_repeat(' ', $this->style->getPadding()), |
|
99
|
|
|
$this->style->getUnselectedUnsetCode() |
|
100
|
|
|
) |
|
101
|
|
|
); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* Write some text at a particular column |
|
106
|
|
|
* |
|
107
|
|
|
* @param int $column |
|
108
|
|
|
* @param string $text |
|
109
|
|
|
*/ |
|
110
|
|
|
protected function write($text, $column = null) |
|
111
|
|
|
{ |
|
112
|
|
|
$this->terminal->moveCursorToColumn($column ?: $this->x); |
|
113
|
|
|
echo $text; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* @return MenuStyle |
|
118
|
|
|
*/ |
|
119
|
|
|
public function getStyle() |
|
120
|
|
|
{ |
|
121
|
|
|
return $this->style; |
|
122
|
|
|
} |
|
123
|
|
|
} |
|
124
|
|
|
|
This check looks for assignments to scalar types that may be of the wrong type.
To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.