aboutsummaryrefslogtreecommitdiff
path: root/src/bdrem/Renderer.php
blob: 6612447fb74204e88ed6c1c5d88bf4fdd2bbe8c0 (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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
<?php
/**
 * Part of bdrem
 *
 * PHP version 5
 *
 * @category  Tools
 * @package   Bdrem
 * @author    Christian Weiske <cweiske@cweiske.de>
 * @copyright 2014 Christian Weiske
 * @license   http://www.gnu.org/licenses/agpl.html GNU AGPL v3
 * @link      http://cweiske.de/bdrem.htm
 */
namespace bdrem;

/**
 * Base event renderer
 *
 * @category  Tools
 * @package   Bdrem
 * @author    Christian Weiske <cweiske@cweiske.de>
 * @copyright 2014 Christian Weiske
 * @license   http://www.gnu.org/licenses/agpl.html GNU AGPL v3
 * @version   Release: @package_version@
 * @link      http://cweiske.de/bdrem.htm
 */
abstract class Renderer
{
    /**
     * HTTP content type of output
     * @var string
     */
    protected $httpContentType = null;

    /**
     * Call the renderer and output the rendering result to shell or browser
     *
     * @param array $arEvents Event objects to render
     *
     * @return void
     */
    public function renderAndOutput($arEvents)
    {
        if (PHP_SAPI != 'cli' && $this->httpContentType !== null) {
            header('Content-type: ' . $this->httpContentType);
        }
        echo $this->render($arEvents);
    }

    /**
     * Do something when there are no events to render
     *
     * @return void
     */
    public function handleStopOnEmpty()
    {
    }

    /**
     * Display the events in some way
     *
     * @param array $arEvents Events to display
     *
     * @return string Event representation
     */
    abstract public function render($arEvents);

    /**
     * Converts the given date string according to the user's locale setting.
     *
     * @param string $dateStr Date in format YYYY-MM-DD
     *
     * @return string Formatted date
     */
    protected function getLocalDate($dateStr)
    {
        if ($dateStr[0] != '?') {
            return strftime('%x', strtotime($dateStr));
        }

        $dateStr = str_replace('????', '1899', $dateStr);
        return str_replace(
            array('1899', '99'),
            array('????', '??'),
            strftime('%x', strtotime($dateStr))
        );
    }
}
?>