Template::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
1
<?php namespace Wn\Generators\Template;
2
3
use Wn\Generators\Template\TemplateLoader;
4
5
6
class Template {
7
8
	protected $loader;
9
10
	protected $text;
11
12
	protected $data;
13
14
	protected $compiled;
15
16
	protected $dirty;
17
18
	public function __construct(TemplateLoader $loader, $text)
19
	{
20
		$this->loader = $loader;
21
		$this->text = $text;
22
		$this->compiled = '';
23
		$this->data = [];
24
		$this->dirty = true;
25
	}
26
27
	public function clean()
28
	{
29
		$this->data = [];
30
		$this->dirty = false;
31
		return $this;
32
	}
33
34
	public function with($data = [])
35
	{
36
		if($data)
0 ignored issues
show
Bug Best Practice introduced by
The expression $data of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
37
			$this->dirty = true;
38
		foreach ($data as $key => $value) {
39
			$this->data[$key] = $value;
40
		}
41
		return $this;
42
	}
43
44
	public function get()
45
	{
46
		if($this->dirty){
47
			$this->compile();
48
			$this->dirty = false;
49
		}
50
		return $this->compiled;
51
	}
52
53
	public function compile()
54
	{
55
		$this->compiled = $this->text;
56
		foreach($this->data as $key => $value){
57
			$this->compiled = str_replace('{{' . $key . '}}', $value, $this->compiled);
58
		}
59
		return $this;
60
	}	
61
62
}
63