|
1
|
|
|
<?php |
|
2
|
|
|
namespace Sign; |
|
3
|
|
|
|
|
4
|
|
|
class Line { |
|
5
|
|
|
private $_length = 21; |
|
6
|
|
|
private $_text; |
|
7
|
|
|
private $_background = 'black'; |
|
8
|
|
|
private $_parent; |
|
9
|
|
|
private $_colours = array(); |
|
10
|
|
|
private $_params = array(); |
|
11
|
|
|
|
|
12
|
|
|
public function __construct($text, $parent) { |
|
13
|
|
|
$this->setText($text); |
|
14
|
|
|
$this->_parent = $parent; |
|
15
|
|
|
} |
|
16
|
|
|
|
|
17
|
|
|
public function setText($text) { |
|
18
|
|
|
// tokenise text |
|
19
|
|
|
preg_match_all('/\?+/', $text, $matches, PREG_OFFSET_CAPTURE); |
|
20
|
|
|
$this->_params = array(); |
|
21
|
|
|
foreach ($matches[0] as $key => $match) { |
|
22
|
|
|
$this->_params[$key] = array('param' => new Param(strlen($match[0])), 'offset' => $match[1]); |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
$this->_text = $text; |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
public function color($start, $end, $name) { |
|
29
|
|
|
return $this->colour($start, $end, $name); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
public function colour($start, $end, $name) { |
|
33
|
|
|
$this->_colours[] = array($start, $end, $name); |
|
34
|
|
|
return $this; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
public function end() { |
|
38
|
|
|
return $this->_parent; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
public function bind($position, $value) { |
|
42
|
|
|
$this->_params[$position]['param']->setValue($value); |
|
43
|
|
|
return $this; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
public function align($position, $type) { |
|
47
|
|
|
$this->_params[$position]['param']->align($type); |
|
48
|
|
|
return $this; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
private function _getColour($key) { |
|
52
|
|
|
foreach ($this->_colours as $colour) { |
|
53
|
|
|
if ($key >= $colour[0] && $key <= $colour[1]) { |
|
54
|
|
|
return $colour[2]; |
|
55
|
|
|
} |
|
56
|
|
|
} |
|
57
|
|
|
return 'red'; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
public function render() { |
|
61
|
|
|
$output = ''; |
|
62
|
|
|
$length = 0; |
|
63
|
|
|
|
|
64
|
|
|
foreach ($this->_params as $key => $param) { |
|
65
|
|
|
$p = $param['param']; |
|
66
|
|
|
if (!$p->bound()) continue; |
|
67
|
|
|
$this->_text = substr_replace($this->_text, $p->value(), $param['offset'], strlen($p->value())); |
|
68
|
|
|
} |
|
69
|
|
|
// apply colours |
|
70
|
|
|
foreach (str_split($this->_text) as $key => $char) { |
|
71
|
|
|
if ($length++ > $this->_length) break; |
|
72
|
|
|
$fore = $this->_getColour($key); |
|
73
|
|
|
$output .= $this->_char($char, $fore); |
|
74
|
|
|
} |
|
75
|
|
|
// add padding |
|
76
|
|
|
if ($length < $this->_length) { |
|
77
|
|
|
for ($i=$length; $i < $this->_length; $i++) { |
|
78
|
|
|
$output .= $this->_char(' ', null, $this->_background); |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
return $output; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
private function _char($char, $fore = 'red', $back = 'black') { |
|
85
|
|
|
return ColourMap::get($fore, $back).dechex(ord($char)); |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
|