View::__toString()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 2
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
namespace Helix\Site;
4
5
use ArrayAccess;
6
use DateTimeInterface;
7
8
/**
9
 * Renders a template with data.
10
 */
11
class View implements ArrayAccess
12
{
13
14
    /**
15
     * @var int|DateTimeInterface
16
     */
17
    protected $cacheTtl = 0;
18
19
    /**
20
     * Extracted to variables upon render.
21
     *
22
     * @var array
23
     */
24
    protected $data = [];
25
26
    /**
27
     * @var string
28
     */
29
    protected $template;
30
31
    public function __construct(string $template, array $data = [])
32
    {
33
        $this->template = $template;
34
        $this->data = $data;
35
    }
36
37
    /**
38
     * @return string
39
     */
40
    public function __toString()
41
    {
42
        return $this->template;
43
    }
44
45
    /**
46
     * The minimum TTL, considering the instance itself and any nested views.
47
     *
48
     * @return int|DateTimeInterface
49
     */
50
    public function getCacheTtl()
51
    {
52
        return $this->cacheTtl;
53
    }
54
55
    /**
56
     * Returns what would be rendered.
57
     *
58
     * @return string
59
     */
60
    public function getContent(): string
61
    {
62
        ob_start();
63
        $this->render();
64
        return ob_end_clean();
0 ignored issues
show
Bug Best Practice introduced by
The expression return ob_end_clean() returns the type boolean which is incompatible with the type-hinted return string.
Loading history...
65
    }
66
67
    /**
68
     * @param mixed $offset
69
     * @return bool
70
     */
71
    public function offsetExists($offset): bool
72
    {
73
        return isset($this->data[$offset]);
74
    }
75
76
    /**
77
     * @param mixed $key
78
     * @return mixed
79
     */
80
    public function offsetGet($key)
81
    {
82
        return $this->data[$key] ?? null;
83
    }
84
85
    /**
86
     * @param mixed $key
87
     * @param mixed $value
88
     */
89
    public function offsetSet($key, $value): void
90
    {
91
        $this->data[$key] = $value;
92
    }
93
94
    /**
95
     * @param mixed $key
96
     */
97
    public function offsetUnset($key): void
98
    {
99
        unset($this->data[$key]);
100
    }
101
102
    /**
103
     * Directly outputs content.
104
     *
105
     * Extracts `$data` to variables, and includes the template.
106
     * `$this` within the template references the view instance.
107
     */
108
    public function render(): void
109
    {
110
        extract($this->data);
111
        include "{$this->template}";
112
    }
113
114
    /**
115
     * @param int|DateTimeInterface $cacheTtl
116
     * @return $this
117
     */
118
    public function setCacheTtl($cacheTtl)
119
    {
120
        $this->cacheTtl = $cacheTtl;
121
        return $this;
122
    }
123
}
124