1
|
|
|
<?php namespace Cerbero\Workflow\Console\Drawing; |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Geometry applied to the drawing. |
5
|
|
|
* |
6
|
|
|
* @author Andrea Marco Sartori |
7
|
|
|
*/ |
8
|
|
|
class Geometry { |
9
|
|
|
|
10
|
|
|
const BORDER_WIDTH = 1; |
11
|
|
|
|
12
|
|
|
const MIN_SPACE_FROM_BORDER_X = 1; |
13
|
|
|
|
14
|
|
|
const MIN_SPACE_FROM_BORDER_Y = 0; |
15
|
|
|
|
16
|
|
|
const ARROW_WIDTH = 1; |
17
|
|
|
|
18
|
|
|
const MIN_PIPE_LENGTH = 8; |
19
|
|
|
|
20
|
|
|
const SPACE_FROM_ARROW = 1; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @author Andrea Marco Sartori |
24
|
|
|
* @var string $core The name of the core. |
25
|
|
|
*/ |
26
|
|
|
protected $core; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @author Andrea Marco Sartori |
30
|
|
|
* @var array $pipes List of pipes. |
31
|
|
|
*/ |
32
|
|
|
protected $pipes; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @author Andrea Marco Sartori |
36
|
|
|
* @var integer $nesting Nesting level. |
37
|
|
|
*/ |
38
|
|
|
protected $nesting = 0; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Set the name of the core. |
42
|
|
|
* |
43
|
|
|
* @author Andrea Marco Sartori |
44
|
|
|
* @param string $core |
45
|
|
|
* @return void |
46
|
|
|
*/ |
47
|
|
|
public function setCore($core) |
48
|
|
|
{ |
49
|
|
|
$this->core = $core; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Set the pipes. |
54
|
|
|
* |
55
|
|
|
* @author Andrea Marco Sartori |
56
|
|
|
* @param array $pipes |
57
|
|
|
* @return void |
58
|
|
|
*/ |
59
|
|
|
public function setPipes(array $pipes) |
60
|
|
|
{ |
61
|
|
|
$this->pipes = $pipes; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Calculate the half width of the drawing. |
66
|
|
|
* |
67
|
|
|
* @author Andrea Marco Sartori |
68
|
|
|
* @param boolean $up |
69
|
|
|
* @return integer |
70
|
|
|
*/ |
71
|
|
|
public function getHalfWidth($up = false) |
72
|
|
|
{ |
73
|
|
|
$number = $this->getTotalWidth(); |
74
|
|
|
|
75
|
|
|
return $this->roundHalf($number, $up); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Round the half of a number, either up or down. |
80
|
|
|
* |
81
|
|
|
* @author Andrea Marco Sartori |
82
|
|
|
* @param integer $number |
83
|
|
|
* @param boolean $up |
84
|
|
|
* @return integer |
85
|
|
|
*/ |
86
|
|
|
private function roundHalf($number, $up) |
87
|
|
|
{ |
88
|
|
|
$round = $up ? 'ceil' : 'floor'; |
89
|
|
|
|
90
|
|
|
return $round($number / 2); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Calculate the total width of the drawing. |
95
|
|
|
* |
96
|
|
|
* @author Andrea Marco Sartori |
97
|
|
|
* @return integer |
98
|
|
|
*/ |
99
|
|
|
protected function getTotalWidth() |
100
|
|
|
{ |
101
|
|
|
$borders = (static::BORDER_WIDTH + static::MIN_SPACE_FROM_BORDER_X) * 2; |
102
|
|
|
|
103
|
|
|
if(empty($this->pipes)) |
104
|
|
|
{ |
105
|
|
|
return $borders + $this->getCoreLength(); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
$borders *= count($this->pipes); |
109
|
|
|
|
110
|
|
|
$name = ($this->getLongestPipeLength() + static::SPACE_FROM_ARROW) * 2; |
111
|
|
|
|
112
|
|
|
return $borders + $name + static::ARROW_WIDTH; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Calculate the length of the core name. |
117
|
|
|
* |
118
|
|
|
* @author Andrea Marco Sartori |
119
|
|
|
* @return integer |
120
|
|
|
*/ |
121
|
|
|
protected function getCoreLength() |
122
|
|
|
{ |
123
|
|
|
return strlen($this->core); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Calculate the length of the longest pipe name. |
128
|
|
|
* |
129
|
|
|
* @author Andrea Marco Sartori |
130
|
|
|
* @return integer |
131
|
|
|
*/ |
132
|
|
|
protected function getLongestPipeLength() |
133
|
|
|
{ |
134
|
|
|
if(empty($this->pipes)) return 0; |
135
|
|
|
|
136
|
|
|
return array_reduce($this->pipes, function($carry, $pipe) |
137
|
|
|
{ |
138
|
|
|
return strlen($pipe) > $carry ? strlen($pipe) : $carry; |
139
|
|
|
|
140
|
|
|
}, static::MIN_PIPE_LENGTH); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Retrieve the spaced pipe and method pair. |
145
|
|
|
* |
146
|
|
|
* @author Andrea Marco Sartori |
147
|
|
|
* @param string $pipe |
148
|
|
|
* @param string $arrow |
149
|
|
|
* @param string $method |
150
|
|
|
* @return string |
151
|
|
|
*/ |
152
|
|
|
public function getSpacedPipe($pipe, $arrow, $method) |
153
|
|
|
{ |
154
|
|
|
$left = $this->getSpacesByWord($pipe); |
155
|
|
|
|
156
|
|
|
$arrow = $this->addSpacesToArrow($arrow); |
157
|
|
|
|
158
|
|
|
$right = $this->getSpacesByWord($method); |
159
|
|
|
|
160
|
|
|
return $left.$pipe.$arrow.$method.$right; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* Retrieve the blank spaces close to a word. |
165
|
|
|
* |
166
|
|
|
* @author Andrea Marco Sartori |
167
|
|
|
* @param string $word |
168
|
|
|
* @return string |
169
|
|
|
*/ |
170
|
|
|
protected function getSpacesByWord($word) |
171
|
|
|
{ |
172
|
|
|
$length = $this->getSideBordersLength() + static::SPACE_FROM_ARROW + static::ARROW_WIDTH; |
173
|
|
|
|
174
|
|
|
$extra = $this->getHalfWidth(true) - $length - strlen($word); |
175
|
|
|
|
176
|
|
|
return $extra > 0 ? str_repeat(' ', $extra) : ''; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* Retrieve the length of the borders of a side. |
181
|
|
|
* |
182
|
|
|
* @author Andrea Marco Sartori |
183
|
|
|
* @return integer |
184
|
|
|
*/ |
185
|
|
|
protected function getSideBordersLength() |
186
|
|
|
{ |
187
|
|
|
$border = (static::BORDER_WIDTH + static::MIN_SPACE_FROM_BORDER_X); |
188
|
|
|
|
189
|
|
|
return $border * $this->nesting; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* Add spaces around the given arrow. |
194
|
|
|
* |
195
|
|
|
* @author Andrea Marco Sartori |
196
|
|
|
* @param string $arrow |
197
|
|
|
* @return string |
198
|
|
|
*/ |
199
|
|
|
protected function addSpacesToArrow($arrow) |
200
|
|
|
{ |
201
|
|
|
$spaces = str_repeat(' ', static::SPACE_FROM_ARROW); |
202
|
|
|
|
203
|
|
|
return "{$spaces}{$arrow}{$spaces}"; |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* Retrieve the left borders formatted with the given border. |
208
|
|
|
* |
209
|
|
|
* @author Andrea Marco Sartori |
210
|
|
|
* @param string $border |
211
|
|
|
* @return string |
212
|
|
|
*/ |
213
|
|
|
public function getLeftBordersWith($border) |
214
|
|
|
{ |
215
|
|
|
$border = str_repeat($border, static::BORDER_WIDTH); |
216
|
|
|
|
217
|
|
|
$space = str_repeat(' ', static::MIN_SPACE_FROM_BORDER_X); |
218
|
|
|
|
219
|
|
|
return str_repeat("{$border}{$space}", $this->nesting); |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* Retrieve the right borders formatted with the given border. |
224
|
|
|
* |
225
|
|
|
* @author Andrea Marco Sartori |
226
|
|
|
* @param string $border |
227
|
|
|
* @return string |
228
|
|
|
*/ |
229
|
|
|
public function getRightBordersWith($border) |
230
|
|
|
{ |
231
|
|
|
$space = str_repeat(' ', static::MIN_SPACE_FROM_BORDER_X); |
232
|
|
|
|
233
|
|
|
$border = str_repeat($border, static::BORDER_WIDTH); |
234
|
|
|
|
235
|
|
|
return str_repeat("{$space}{$border}", $this->nesting); |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
/** |
239
|
|
|
* Increase the nesting level. |
240
|
|
|
* |
241
|
|
|
* @author Andrea Marco Sartori |
242
|
|
|
* @return void |
243
|
|
|
*/ |
244
|
|
|
public function increaseNesting() |
245
|
|
|
{ |
246
|
|
|
$this->nesting++; |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
/** |
250
|
|
|
* Calculate the width of the drawing without the borders. |
251
|
|
|
* |
252
|
|
|
* @author Andrea Marco Sartori |
253
|
|
|
* @return integer |
254
|
|
|
*/ |
255
|
|
|
public function getWidthButBorders() |
256
|
|
|
{ |
257
|
|
|
return $this->getTotalWidth() - $this->getBordersLength(); |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
/** |
261
|
|
|
* Calculate the length of the borders. |
262
|
|
|
* |
263
|
|
|
* @author Andrea Marco Sartori |
264
|
|
|
* @return integer |
265
|
|
|
*/ |
266
|
|
|
protected function getBordersLength() |
267
|
|
|
{ |
268
|
|
|
return $this->getSideBordersLength() * 2; |
269
|
|
|
} |
270
|
|
|
|
271
|
|
|
/** |
272
|
|
|
* Retrieve the spaced core name. |
273
|
|
|
* |
274
|
|
|
* @author Andrea Marco Sartori |
275
|
|
|
* @return string |
276
|
|
|
*/ |
277
|
|
|
public function getSpacedCore() |
278
|
|
|
{ |
279
|
|
|
$left = $this->getSpacesByCore(); |
280
|
|
|
|
281
|
|
|
$right = $this->getSpacesByCore(true); |
282
|
|
|
|
283
|
|
|
return $left.$this->core.$right; |
284
|
|
|
} |
285
|
|
|
|
286
|
|
|
/** |
287
|
|
|
* Retrieve the blank spaces close to the core. |
288
|
|
|
* |
289
|
|
|
* @author Andrea Marco Sartori |
290
|
|
|
* @param boolean $up |
291
|
|
|
* @return string |
292
|
|
|
*/ |
293
|
|
|
protected function getSpacesByCore($up = false) |
294
|
|
|
{ |
295
|
|
|
$free = $this->getTotalWidth() - $this->getBordersLength() - $this->getCoreLength(); |
296
|
|
|
|
297
|
|
|
return $free < 1 ? '' : str_repeat(' ', $this->roundHalf($free, $up)); |
298
|
|
|
} |
299
|
|
|
|
300
|
|
|
/** |
301
|
|
|
* Decrease the nesting level. |
302
|
|
|
* |
303
|
|
|
* @author Andrea Marco Sartori |
304
|
|
|
* @return void |
305
|
|
|
*/ |
306
|
|
|
public function decreaseNesting() |
307
|
|
|
{ |
308
|
|
|
$this->nesting--; |
309
|
|
|
} |
310
|
|
|
|
311
|
|
|
} |
312
|
|
|
|