Base::assemble()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 23
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 10
nc 2
nop 2
dl 0
loc 23
rs 9.0856
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Provides view functionality
5
 *
6
 * PHP Version 5
7
 *
8
 * @category  Core
9
 * @package   View
10
 * @author    Hans-Joachim Piepereit <[email protected]>
11
 * @copyright 2013 cSphere Team
12
 * @license   http://opensource.org/licenses/bsd-license Simplified BSD License
13
 * @link      http://www.csphere.eu
14
 **/
15
16
namespace csphere\core\view;
17
18
/**
19
 * Provides view functionality
20
 *
21
 * @category  Core
22
 * @package   View
23
 * @author    Hans-Joachim Piepereit <[email protected]>
24
 * @copyright 2013 cSphere Team
25
 * @license   http://opensource.org/licenses/bsd-license Simplified BSD License
26
 * @link      http://www.csphere.eu
27
 **/
28
29
abstract class Base extends \csphere\core\service\Drivers
30
{
31
    /**
32
     * Stores the content parts
33
     **/
34
    protected $content = [];
35
36
    /**
37
     * Content type header
38
     **/
39
    protected $type = '';
40
41
    /**
42
     * Allows for adding content
43
     *
44
     * @param string $content The content that should be attached
45
     *
46
     * @return boolean
47
     **/
0 ignored issues
show
Coding Style introduced by
There must be no blank lines after the function comment
Loading history...
48
49
    public function add($content)
50
    {
51
        // Add string to content
52
        $this->content[] = $content;
53
54
        return true;
55
    }
56
57
    /**
58
     * Put everything together
59
     *
60
     * @param boolean $xhr Defaults to false which turns xhr mode off
61
     * @param boolean $box Defaults to false which turns box mode off
62
     *
63
     * @return string
64
     **/
0 ignored issues
show
Coding Style introduced by
There must be no blank lines after the function comment
Loading history...
65
66
    public final function assemble($xhr = false, $box = false)
67
    {
68
        // Check for json object requests
69
        if ($xhr == 1) {
70
71
            $type = 'application/json; charset=UTF-8';
72
73
            $array = $this->format($box);
74
75
            $output = json_encode($array, JSON_FORCE_OBJECT);
76
77
        } else {
78
79
            $type = $this->type;
80
81
            $output = $this->combine($box);
82
        }
83
84
        // Set content type header
85
        \csphere\core\http\Response::header('Content-Type', $type);
86
87
        return $output;
88
    }
89
90
    /**
91
     * Combine content parts on usual requests
92
     *
93
     * @param boolean $box Special case for box mode
94
     *
95
     * @return string
96
     **/
0 ignored issues
show
Coding Style introduced by
There must be no blank lines after the function comment
Loading history...
97
98
    abstract protected function combine($box);
99
100
    /**
101
     * Format content parts on json requests
102
     *
103
     * @param boolean $box Special case for box mode
104
     *
105
     * @return array
106
     **/
0 ignored issues
show
Coding Style introduced by
There must be no blank lines after the function comment
Loading history...
107
108
    protected function format($box)
109
    {
110
        // Just use combine method as content by default
111
        $result            = [];
112
        $result['content'] = $this->combine($box);
113
114
        return $result;
115
    }
116
117
    /**
118
     * Check for view settings like e.g. debug
119
     *
120
     * @param string $key Get a specific array key
121
     *
122
     * @return mixed
123
     **/
0 ignored issues
show
Coding Style introduced by
There must be no blank lines after the function comment
Loading history...
124
125
    public function getOption($key)
126
    {
127
        // Check if the key exists
128
        if (isset($this->config[$key])) {
129
130
            return $this->config[$key];
131
        } else {
132
133
            return null;
134
        }
135
    }
136
}
137