AbstractTemplate   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 107
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 0
dl 0
loc 107
rs 10
c 0
b 0
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A __invoke() 0 6 1
A assignMany() 0 8 2
A assignOne() 0 4 1
A isAssigned() 0 4 1
A render() 0 8 1
A reset() 0 4 1
A __toString() 0 4 1
getContent() 0 1 ?
A getValueByKeyOrNull() 0 6 2
A getVariables() 0 4 1
1
<?php
2
/**
3
 * @author stev leibelt <[email protected]>
4
 * @since 2015-10-01
5
 */
6
namespace Net\Bazzline\Component\Template;
7
8
use InvalidArgumentException;
9
use RuntimeException;
10
11
abstract class AbstractTemplate implements TemplateInterface
12
{
13
    /** @var array */
14
    private $variables;
15
16
    /**
17
     * @param array $variables
18
     */
19
    public function __construct($variables = array())
20
    {
21
        $this->assignMany($variables, false);
22
    }
23
24
    /**
25
     * @param array $variables
26
     * @return string
27
     */
28
    public function __invoke($variables = array())
29
    {
30
        $this->assignMany($variables);
31
32
        return $this->render();
33
    }
34
35
    /**
36
     * @param array $variables
37
     * @param bool $mergeWithExisting
38
     */
39
    public function assignMany(array $variables, $mergeWithExisting = true)
40
    {
41
        if ($mergeWithExisting) {
42
            $this->variables = array_merge($this->variables, $variables);
43
        } else {
44
            $this->variables = $variables;
45
        }
46
    }
47
48
    /**
49
     * @param string $key
50
     * @param mixed $value
51
     */
52
    public function assignOne($key, $value)
53
    {
54
        $this->assignMany(array($key => $value));
55
    }
56
57
    /**
58
     * @param string $key
59
     * @return bool
60
     */
61
    public function isAssigned($key)
62
    {
63
        return (isset($this->variables[$key]));
64
    }
65
66
    /**
67
     * @return string
68
     * @throws RuntimeException
69
     */
70
    public function render()
71
    {
72
        $content    = $this->getContent();
73
        $variables  = $this->getVariables();
74
        $keys       = array_keys($variables);
75
76
        return str_replace($keys, $variables, $content);
77
    }
78
79
    public function reset()
80
    {
81
        $this->assignMany(array(), false);
82
    }
83
84
    /**
85
     * @return string
86
     * @throws RuntimeException
87
     */
88
    public function __toString()
89
    {
90
        return $this->render();
91
    }
92
93
    /**
94
     * @return string
95
     * @throws RuntimeException
96
     */
97
    abstract protected function getContent();
98
99
    /**
100
     * @param string $key
101
     * @return null|mixed
102
     */
103
    protected function getValueByKeyOrNull($key)
104
    {
105
        return ($this->isAssigned($key))
106
            ? $this->variables[$key]
107
            : null;
108
    }
109
110
    /**
111
     * @return array
112
     */
113
    protected function getVariables()
114
    {
115
        return $this->variables;
116
    }
117
}
118