SnippetBuilder::build()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 7
dl 0
loc 13
ccs 8
cts 8
cp 1
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 2
1
<?php
2
/**
3
 * Class for building snippets from external templates
4
 *
5
 * @file      SnippetBuilder.php
6
 *
7
 * PHP version 8.0+
8
 *
9
 * @author    Yancharuk Alexander <alex at itvault dot info>
10
 * @copyright © 2012-2021 Alexander Yancharuk
11
 * @date      2015-08-17 17:32
12
 * @license   The BSD 3-Clause License
13
 *            <https://tldrlegal.com/license/bsd-3-clause-license-(revised)>
14
 */
15
16
namespace Veles\Tools;
17
18
/**
19
 * Class SnippetBuilder
20
 *
21
 * @author  Yancharuk Alexander <alex at itvault dot info>
22
 */
23
class SnippetBuilder
24
{
25
	protected $variables = [];
26
27 2
	public function __construct($vars)
28
	{
29 2
		foreach ($vars as $var_name => $value) {
30 2
			$this->variables[$var_name] = $value;
31
		}
32
	}
33
34
	/**
35
	 * Build html using variables and given template
36
	 *
37
	 * @param string $path
38
	 *
39
	 * @return string
40
	 */
41 2
	public function build($path)
42
	{
43 2
		foreach ($this->variables as $var_name => $value) {
44 2
			$$var_name = $value;
45
		}
46
47 2
		ob_start();
48
		/** @noinspection PhpIncludeInspection */
49 2
		include $path;
50 2
		$output = ob_get_contents();
51 2
		ob_end_clean();
52
53 2
		return $output;
54
	}
55
}
56