aboutsummaryrefslogtreecommitdiff
path: root/lib/classes/StudipControllerPropertiesTrait.php
blob: 4e906fae0ef5bd841b9f7bb5f56a7d6a050ff0fd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
<?php
/**
 * This trait manages all variable assignments to the controller and templates.
 *
 * @author Jan-Hendrik Willms <tleilax+studip@gmail.com>
 * @license GPL2 or any later version
 * @since Stud.IP 5.2
 */
trait StudipControllerPropertiesTrait
{
    /**
     * Stores the assigned variables.
     * @var array
     */
    protected $_template_variables = [];

    /**
     * Returns whether a variable is set.
     *
     * @param string $offset
     * @return bool
     */
    public function __isset(string $offset): bool
    {
        return isset($this->_template_variables[$offset]);
    }

    /**
     * Stores a variable.
     *
     * @param string $offset
     * @param mixed $value
     */
    public function __set(string $offset, $value): void
    {
        $this->_template_variables[$offset] = $value;
    }

    /**
     * Returns a previously set variable.
     *
     * @param string $offset
     * @return mixed
     */
    public function &__get(string $offset)
    {
        if (!isset($this->_template_variables[$offset])) {
            $this->_template_variables[$offset] = null;
        }
        return $this->_template_variables[$offset];
    }

    /**
     * Unsets a previously set variable
     *
     * @param string $offset
     */
    public function __unset(string $offset): void
    {
        unset($this->_template_variables[$offset]);
    }

    public function get_assigned_variables(): array
    {
        $variables = $this->_template_variables;
        $variables['controller'] = $this;
        return $variables;
    }
}