Html5MiniTemplate.php 3.22 KB
Newer Older
Dan Untenzu's avatar
Dan Untenzu committed
1
2
3
4
5
6
7
8
9
10
11
12
<?php

namespace Pixelbrackets\Html5MiniTemplate;

class Html5MiniTemplate {
    /**
     * The HTML5 template
     *
     * @var string
     */
    protected $markup = '';

13
14
15
16
17
18
    /**
     * Stylesheet preselection
     *
     * @var array
     */
    protected $listOfStylesheets = [
19
        'bootstrap' => 'https://cdn.jsdelivr.net/gh/twbs/bootstrap@4.3/dist/css/bootstrap.min.css',
20
        'example' => '/assets/css/stylesheet.css?v=1',
21
22
23
24
        'milligram' => 'https://cdn.jsdelivr.net/gh/milligram/milligram@1.3/dist/milligram.min.css',
        'mini.css' => 'https://cdn.jsdelivr.net/gh/Chalarangelo/mini.css@3.0/dist/mini-default.min.css',
        'mui' => 'https://cdn.jsdelivr.net/gh/muicss/mui@0.9/packages/cdn/css/mui.min.css',
        'picnic' => 'https://cdn.jsdelivr.net/gh/franciscop/picnic@6.4/releases/plugins.min.css',
25
        'skeleton' => 'https://cdn.jsdelivr.net/gh/dhg/Skeleton@2.0.4/css/skeleton.css',
26
27
    ];

28
29
30
31
32
33
34
    /**
     * The document title
     *
     * @var array
     */
    protected $title = '';

35
36
37
38
39
40
41
    /**
     * The linked stylesheet
     *
     * @var array
     */
    protected $stylesheet = 'example';

42
43
44
45
46
47
48
49
50
51
52
53
    /**
     * The documents main content
     *
     * @var string
     */
    protected $content = '';

    /**
     * Mini Template
     *
     * @return string
     */
Dan Untenzu's avatar
Dan Untenzu committed
54
55
56
57
58
    public function __construct()
    {
        $this->markup = file_get_contents(__DIR__ . '/../resources/template.html');
    }

59
60
61
    /**
     * Process all changes like custom content
     *
62
     * @param string $text Markup for document body
63
64
65
66
     * @return string
     */
    protected function parseMarkup() {
        $markup = $this->markup;
67

68
69
70
71
        if (false === empty($this->title)) {
            $markup = preg_replace('/HTML5 Example Page/', $this->title, $markup);
        }

72
73
74
75
76
77
        $stylesheet = '';
        if (false === empty($this->stylesheet)) {
            $stylesheet = '<link rel="stylesheet" href="' . ($this->listOfStylesheets[$this->stylesheet] ?? $this->stylesheet) . '">';
        }
        $markup = preg_replace('/<link rel="stylesheet" href="(.*?)">/', $stylesheet, $markup);

78
79
80
        if (false === empty($this->content)) {
            $markup = preg_replace('/<body>(.*?)<\/body>/is', '<body>' . $this->content . '</body>', $markup);
        }
81

82
83
84
85
86
87
88
89
        return $markup;
    }

    /**
     * Get markup of HTML5 document
     *
     * @return string
     */
Dan Untenzu's avatar
Dan Untenzu committed
90
    public function getMarkup() {
91
92
93
        return $this->parseMarkup();
    }

94
95
96
97
98
99
100
101
102
103
    /**
     * Set title
     *
     * @param string $title Set document title, description and header
     * @return void
     */
    public function setTitle($title) {
        $this->title = $title;
    }

104
105
106
107
108
109
110
111
112
113
114
115
    /**
     * Set stylesheet
     *
     * @param string $stylesheet Use keywords for one of the preselected stylesheets
     * (see $listOfStylesheets, eg. »skeleton«), an empty string to remove the
     * style tag or the URL to any other existing stylesheet (eg. »/styles.css«)
     * @return void
     */
    public function setStylesheet($stylesheet) {
        $this->stylesheet = $stylesheet;
    }

116
117
118
119
120
121
122
123
    /**
     * Set markup for document body
     *
     * @param string $text Markup for document body
     * @return void
     */
    public function setContent($text) {
        $this->content = $text;
Dan Untenzu's avatar
Dan Untenzu committed
124
125
    }
}