Skip to content

Commit de34186

Browse files
committed
[FEATURE] ✨ Enable method chaining
Refactor all methods to enable fluent interfaces.
1 parent 1f1de51 commit de34186

File tree

4 files changed

+37
-15
lines changed

4 files changed

+37
-15
lines changed

README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -73,13 +73,13 @@ Mirror https://github.com/pixelbrackets/html5-mini-template
7373
file_put_contents('/var/www/example/index.html', $template->getMarkup());
7474
```
7575

76-
1. Wrap a message, set a custom stylesheet URL,
77-
set a title, output the document
76+
1. Wrap a message, set your own stylesheet URL, set a title,
77+
output the document
7878
```php
79-
$template = new \Pixelbrackets\Html5MiniTemplate\Html5MiniTemplate();
80-
$template->setStylesheet('/assets/styles.css');
81-
$template->setTitle('Index');
82-
$template->setContent('<h1>TOC</h1><ul><li>a</li><li>b</li></ul>');
79+
$template = (new \Pixelbrackets\Html5MiniTemplate\Html5MiniTemplate())
80+
->setStylesheet('/assets/styles.css')
81+
->setTitle('Index')
82+
->setContent('<h1>TOC</h1><ul><li>a</li><li>b</li></ul>');
8383
echo $template->getMarkup();
8484
```
8585

src/Html5MiniTemplate.php

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -139,11 +139,12 @@ public static function getTemplate()
139139
* Set title
140140
*
141141
* @param string $title Set document title, description and header
142-
* @return void
142+
* @return Html5MiniTemplate
143143
*/
144144
public function setTitle($title)
145145
{
146146
$this->title = $title;
147+
return $this;
147148
}
148149

149150
/**
@@ -168,11 +169,12 @@ protected function getTitle()
168169
* @param string $stylesheet Use keywords for one of the preselected stylesheets
169170
* (see $listOfStylesheets, eg. »skeleton«), an empty string to remove the
170171
* style tag or the URL to any other existing stylesheet (eg. »/styles.css«)
171-
* @return void
172+
* @return Html5MiniTemplate
172173
*/
173174
public function setStylesheet($stylesheet)
174175
{
175176
$this->stylesheet = $stylesheet;
177+
return $this;
176178
}
177179

178180
/**
@@ -185,11 +187,12 @@ public function setStylesheet($stylesheet)
185187
* its content rendered inline in a STYLE-tag.
186188
*
187189
* @param string $mode Set render mode, either Html5MiniTemplate::STYLE_LINK or Html5MiniTemplate::STYLE_INLINE
188-
* @return void
190+
* @return Html5MiniTemplate
189191
*/
190192
public function setStylesheetMode($mode)
191193
{
192194
$this->stylesheetMode = $mode;
195+
return $this;
193196
}
194197

195198
/**
@@ -233,21 +236,23 @@ protected function getStylesheetContent()
233236
* Set any additional metadata like metatags or link references.
234237
*
235238
* @param string $metadata Additional metadata for document head
236-
* @return void
239+
* @return Html5MiniTemplate
237240
*/
238241
public function setAdditionalMetadata($metadata)
239242
{
240243
$this->additionalMetadata = $metadata;
244+
return $this;
241245
}
242246

243247
/**
244248
* Set markup for document body
245249
*
246250
* @param string $text Markup for document body
247-
* @return void
251+
* @return Html5MiniTemplate
248252
*/
249253
public function setContent($text)
250254
{
251255
$this->content = $text;
256+
return $this;
252257
}
253258
}

tests/status-page.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,13 @@
22

33
require __DIR__ . '/../vendor/autoload.php';
44

5-
$template = new \Pixelbrackets\Html5MiniTemplate\Html5MiniTemplate();
6-
$template->setStylesheet('skeleton');
7-
$template->setContent('<h1>Status</h1><p>All Systems Operational</p>');
8-
echo $template->getMarkup();
5+
//$template = new \Pixelbrackets\Html5MiniTemplate\Html5MiniTemplate();
6+
//$template->setStylesheet('skeleton');
7+
//$template->setContent('<h1>Status</h1><p>All Systems Operational</p>');
8+
//echo $template->getMarkup();
9+
10+
$markup = (new \Pixelbrackets\Html5MiniTemplate\Html5MiniTemplate())
11+
->setStylesheet('skeleton')
12+
->setContent('<h1>Status</h1><p>All Systems Operational</p>')
13+
->getMarkup();
14+
echo $markup;

tests/unit/Html5MiniTemplateTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,4 +121,15 @@ public function testAdditionalMetadata()
121121
$document = $template->getMarkup();
122122
$this->assertStringContainsString('canonical', $document);
123123
}
124+
125+
public function testFluentInterface()
126+
{
127+
$document = (new \Pixelbrackets\Html5MiniTemplate\Html5MiniTemplate())
128+
->setStylesheet('/style.css')
129+
->setContent('Hello World')
130+
->setTitle('Index')
131+
->setAdditionalMetadata('<link rel="canonical" href="https://html5example.com/">')
132+
->getMarkup();
133+
$this->assertNotEmpty($document);
134+
}
124135
}

0 commit comments

Comments
 (0)