Completed
Push — 9.0-dev ( bf07f2...007112 )
by Radu
01:34
created

AbstractOutputLoader::getRenderedHtml()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 1
1
<?php
2
namespace WebServCo\Framework;
3
4
class AbstractOutputLoader
5
{
6
    protected $projectPath;
7
    protected $htmlOutput;
8
    protected $jsonOutput;
9
    
10
    public function __construct($projectPath, $htmlOutput = null, $jsonOutput = null)
11
    {
12
        $this->projectPath = $projectPath;
13
        $this->htmlOutput = $htmlOutput;
14
        $this->jsonOutput = $jsonOutput;
15
    }
16
    
17
    protected function htmlOutput()
18
    {
19
        return $this->htmlOutput;
20
    }
21
    
22
    protected function jsonOutput()
23
    {
24
        $this->jsonOutput = $jsonOutput;
0 ignored issues
show
Bug introduced by
The variable $jsonOutput does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
25
    }
26
    
27
    private function getRenderedHtml($template)
28
    {
29
        /**
30
         * Set template path.
31
         */
32
        $this->htmlOutput()->setPath("{$this->projectPath}resources/views/");
33
        /**
34
         * Set page template
35
         */
36
        $this->htmlOutput()->setTemplate($template);
37
        return $this->htmlOutput()->render();
38
    }
39
    
40
    public function html($data, $pageTemplate, $mainTemplate = null)
41
    {
42
        /**
43
         * Set page data
44
         */
45
        if (is_array($data)) {
46
            foreach ($data as $key => $value) {
47
                $this->htmlOutput()->setData($key, $value);
48
            }
49
        }
50
        /**
51
         * Partials
52
         */
53
        foreach ($this->htmlOutput()->setting('partials', []) as $partialTemplate) {
54
            $this->htmlOutput()->setData(
55
                "tpl_{$partialTemplate}",
56
                $this->getRenderedHtml($partialTemplate)
57
            );
58
        }
59
        /**
60
         * Page content
61
         */
62
        $this->htmlOutput()->setData(
63
            'tpl_content',
64
            $this->getRenderedHtml($pageTemplate)
65
        );
66
        /**
67
         * Main template
68
         */
69
        $mainTemplate = $mainTemplate ? $mainTemplate :
70
            $this->htmlOutput()->setting('main_template', 'layout');
71
        return $this->getRenderedHtml($mainTemplate);
72
    }
73
    
74
    public function json($data)
75
    {
76
        if (is_array($data)) {
77
            foreach ($data as $key => $value) {
78
                $this->jsonOutput()->setData($key, $value);
0 ignored issues
show
Bug introduced by
The method setData cannot be called on $this->jsonOutput() (of type null).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
79
            }
80
        }
81
        return $this->jsonOutput()->render();
0 ignored issues
show
Bug introduced by
The method render cannot be called on $this->jsonOutput() (of type null).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
82
    }
83
}
84