Completed
Push — v4.0-dev ( b01e70...dabe54 )
by
unknown
01:56
created

Template::getContextItem()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 2
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace League\Plates;
4
5
/** Template value object */
6
final class Template
7
{
8
    public $name;
9
    public $data;
10
    public $context;
11
12
    public function __construct(
13
        $name,
14
        array $data = [],
15
        array $context = []
16
    ) {
17
        $this->name = $name;
18
        $this->data = $data;
19
        $this->context = $context;
20
    }
21
22
    public function addData(array $data) {
23
        $this->data = array_merge($this->data, $data);
24
        return $this;
25
    }
26
    public function addContext(array $context) {
27
        $this->context = array_merge($this->context, $context);
28
        return $this;
29
    }
30
    public function getContextItem($key, $default = null) {
31
        return array_key_exists($key, $this->context)
32
            ? $this->context[$key]
33
            : $default;
34
    }
35
36
    public function resolveName(callable $resolve_name) {
37
        return $resolve_name($this->name, $this->context);
38
    }
39
40
    public function resolveData(callable $resolve_data) {
41
        return $resolve_data($this->data, $this->context);
42
    }
43
44
    /** Create a new template based off of this current one */
45
    public function fork($name, array $data = [], array $context = []) {
46
        return new self(
47
            $name,
48
            array_merge($this->data, $data),
49
            array_merge($this->context, $context)
50
        );
51
    }
52
}
53