Completed
Push — extensions ( 84a6ae )
by
unknown
22:00 queued 07:01
created

Template::get()   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 $attributes;
11
    public $reference;
12
    public $parent;
13
14
    public function __construct(
15
        $name,
16
        array $data = [],
17
        array $attributes = [],
18
        TemplateReference $ref = null,
19
        TemplateReference $parent = null
20
    ) {
21
        $this->name = $name;
22
        $this->data = $data;
23
        $this->attributes = $attributes;
24
        $this->reference = ($ref ?: new TemplateReference)->update($this);
25
        $this->parent = $parent;
26
    }
27
28
    public function with($key, $value) {
29
        return $this->withAddedAttributes([$key => $value]);
30
    }
31
    public function get($key, $default = null) {
32
        return array_key_exists($key, $this->attributes)
33
            ? $this->attributes[$key]
34
            : $default;
35
    }
36
37
    /** Returns the deferenced parent template */
38
    public function parent() {
39
        return $this->parent ? ($this->parent)() : null;
40
    }
41
42
    public function withName($name) {
43
        return new self($name, $this->data, $this->attributes, $this->reference, $this->parent);
44
    }
45
46
    public function withData(array $data) {
47
        return new self($this->name, $data, $this->attributes, $this->reference, $this->parent);
48
    }
49
50
    public function withAddedData(array $data) {
51
        return new self($this->name, array_merge($this->data, $data), $this->attributes, $this->reference, $this->parent);
52
    }
53
54
    public function withAttributes(array $attributes) {
55
        return new self($this->name, $this->data, $attributes, $this->reference, $this->parent);
56
    }
57
58
    public function withAddedAttributes(array $attributes) {
59
        return new self($this->name, $this->data, array_merge($this->attributes, $attributes), $this->reference, $this->parent);
60
    }
61
62
    public function __clone() {
63
        return new self($this->name, $this->data, $this->attributes, null, $this->parent);
64
    }
65
66
    /** Create a new template based off of this current one */
67
    public function fork($name, array $data = [], array $attributes = []) {
68
        return new self(
69
            $name,
70
            $data,
71
            $attributes,
72
            null,
73
            $this->reference
74
        );
75
    }
76
}
77