1 | <?php |
||
5 | class Visualizer |
||
6 | { |
||
7 | /** |
||
8 | * Options used in configuring the visualization. |
||
9 | * |
||
10 | * - int width: |
||
11 | * Determines the output size of the visualization |
||
12 | * Note: This controls the width of the bars only. |
||
13 | * |
||
14 | * @var array |
||
15 | */ |
||
16 | private $options; |
||
17 | |||
18 | /** |
||
19 | * Create a new visualizer. |
||
20 | * |
||
21 | * @param array $options |
||
22 | */ |
||
23 | public function __construct(array $options = []) |
||
27 | |||
28 | /** |
||
29 | * Builds a string to visualize one or more |
||
30 | * periods and/or collections in a more |
||
31 | * human readable / parsable manner. |
||
32 | * |
||
33 | * Keys are used as identifiers in the output |
||
34 | * and the periods are represented with bars. |
||
35 | * |
||
36 | * This visualizer is capable of generating |
||
37 | * output like the following: |
||
38 | * |
||
39 | * A [========] |
||
40 | * B [==] |
||
41 | * C [=====] |
||
42 | * CURRENT [===============] |
||
43 | * OVERLAP [=] [==] [=] |
||
44 | * |
||
45 | * @param array|Period[]|PeriodCollection[] $blocks |
||
46 | * @return string |
||
47 | */ |
||
48 | public function visualize(array $blocks): string |
||
65 | |||
66 | /** |
||
67 | * Build a 2D table such that: |
||
68 | * - There's one row for every block. |
||
69 | * - There's one column for every unit of width. |
||
70 | * - Each cell is true when a period is active for that unit. |
||
71 | * - Each cell is false when a period is not active for that unit. |
||
72 | * |
||
73 | * @param array $blocks |
||
74 | * @return array |
||
75 | */ |
||
76 | private function matrix(array $blocks): array |
||
97 | |||
98 | /** |
||
99 | * Get the start / end coordinates for a given period. |
||
100 | * |
||
101 | * @param Period $period |
||
102 | * @param Period $bounds |
||
103 | * @param int $width |
||
104 | * @return array |
||
105 | */ |
||
106 | private function coords(Period $period, Period $bounds, int $width): array |
||
126 | |||
127 | /** |
||
128 | * Populate a row with true values |
||
129 | * where periods are active. |
||
130 | * |
||
131 | * @param array $row |
||
132 | * @param Period $period |
||
133 | * @param Period $bounds |
||
134 | * @return array |
||
135 | */ |
||
136 | private function populateRow(array $row, Period $period, Period $bounds): array |
||
150 | |||
151 | /** |
||
152 | * Get the bounds encompassing all visualized periods. |
||
153 | * |
||
154 | * @param array $blocks |
||
155 | * @return Period|null |
||
156 | */ |
||
157 | private function bounds(array $blocks): ?Period |
||
173 | |||
174 | /** |
||
175 | * Turn a series of true/false values into bars |
||
176 | * representing the start/end of periods. |
||
177 | * |
||
178 | * @param array $row |
||
179 | * @return string |
||
180 | */ |
||
181 | private function toBars(array $row): string |
||
221 | } |
||
222 |
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: