Completed
Push — b0.27.0 ( c529b3...34cc26 )
by Sebastian
05:07
created

View::render()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
c 1
b 0
f 0
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 10
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
/**
4
 * Linna Framework.
5
 *
6
 * @author Sebastian Rapetti <[email protected]>
7
 * @copyright (c) 2018, Sebastian Rapetti
8
 * @license http://opensource.org/licenses/MIT MIT License
9
 */
10
declare(strict_types=1);
11
12
namespace Linna\Mvc;
13
14
use Linna\Mvc\TemplateInterface;
15
16
use SplObserver;
17
use SplSubject;
18
19
/**
20
 * Parent class for view classes.
21
 *
22
 * This class was implemented like part of Observer pattern
23
 * https://en.wikipedia.org/wiki/Observer_pattern
24
 * http://php.net/manual/en/class.splobserver.php
25
 */
26
class View implements SplObserver
27
{
28
    /**
29
     * @var array<mixed> Data for the dynamic view
30
     */
31
    protected array $data = [];
32
33
    /**
34
     * @var TemplateInterface Template utilized for render data
35
     */
36
    protected TemplateInterface $template;
37
38
    /**
39
     * @var Model Model for access data
40
     */
41
    protected Model $model;
42
43
    /**
44
     * Constructor.
45
     *
46
     * @param Model $model
47
     */
48 16
    public function __construct(Model $model, TemplateInterface $template)
49
    {
50 16
        $this->model = $model;
51 16
        $this->template = $template;
52 16
    }
53
54
    /**
55
     * Render a template.
56
     *
57
     * @return string
58
     */
59 19
    public function render(): string
60
    {
61 19
        $this->template->setData($this->data);
62
63 19
        return $this->template->getOutput();
64
    }
65
66
    /**
67
     * Update Observer data.
68
     *
69
     * @param SplSubject $subject
70
     *
71
     * @return void
72
     */
73 19
    public function update(SplSubject $subject)
74
    {
75 19
        if ($subject instanceof Model) {
76 19
            $this->data = \array_merge($this->data, $subject->get());
77
        }
78 19
    }
79
}
80