Template::withAddedAttributes()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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