Completed
Push — master ( 0b8712...2f8bc7 )
by Mihail
04:21
created

Variables::setGlobal()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 1 Features 0
Metric Value
c 3
b 1
f 0
dl 0
loc 4
rs 10
cc 2
eloc 2
nc 2
nop 3
1
<?php
2
3
namespace Ffcms\Core\Template;
4
5
use Ffcms\Core\App;
6
use Ffcms\Core\Helper\Type\Obj;
7
use Ffcms\Core\Traits\Singleton;
8
9
/**
10
 * Class Variables. Support singleton-based class to store data from any place as a variables for view's.
11
 * @package Ffcms\Core\Template
12
 */
13
class Variables
14
{
15
    use Singleton;
16
17
    protected $globalVars = [];
18
19
    /**
20
     * Set global variable for Views
21
     * @param string $var
22
     * @param string|array $value
23
     * @param bool $html
24
     */
25
    public function setGlobal($var, $value, $html = false)
26
    {
27
        $this->globalVars[$var] = $html ? App::$Security->secureHtml($value) : App::$Security->strip_tags($value);
28
    }
29
30
    /**
31
     * Set global variable from key=>value array (key = varname)
32
     * @param array $array
33
     */
34
    public function setGlobalArray(array $array)
35
    {
36
        if (!Obj::isArray($array)) {
37
            return;
38
        }
39
        foreach ($array as $var => $value) {
40
            $this->globalVars[$var] = Obj::isString($value) ? App::$Security->strip_tags($value) : $value;
41
        }
42
    }
43
44
    /**
45
     * Get all global variables as array
46
     * @return array|null
47
     */
48
    public function getGlobalsArray()
49
    {
50
        return $this->globalVars;
51
    }
52
53
    /**
54
     * Get all global variables as stdObject
55
     * @return object
56
     */
57
    public function getGlobalsObject()
58
    {
59
        return (object)$this->globalVars;
60
    }
61
62
    /**
63
     * Check if global variable isset
64
     * @param string $var
65
     * @return bool
66
     */
67
    public function issetGlobal($var)
68
    {
69
        return array_key_exists($var, $this->globalVars);
70
    }
71
}