aboutsummaryrefslogtreecommitdiff
path: root/src/bdrem/Renderer/Console.php
blob: 2cd185f4ba43e4308fb0410a3f14d85e18df0a7f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
<?php
namespace bdrem;

class Renderer_Console
{
    public function render($arEvents)
    {
        $s  = "Days Age Name                                     Event                Date\n";
        $s .= "---- --- ---------------------------------------- -------------------- ----------\n";
        foreach ($arEvents as $event) {
            $s .= sprintf(
                "%3d %4s %s %s %s\n",
                $event->days,
                $event->age,
                $this->str_pad($event->title, 40),
                $this->str_pad($event->type, 20),
                $event->date
            );
        }
        return $s;
    }

    public function str_pad(
        $input, $pad_length, $pad_string = ' ', $pad_type = STR_PAD_RIGHT
    ) {
        $l = mb_strlen($input, 'utf-8');
        if ($l >= $pad_length) {
            return $input;
        }

        $p = str_repeat($pad_string, $pad_length - $l);
        if ($pad_type == STR_PAD_RIGHT) {
            return $input . $p;
        } else {
            return $p . $input;
        }
    }
}
?>