Completed
Push — master ( c0f250...321e24 )
by Filipe
15:10
created

ControllerMethods::set()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 2
1
<?php
2
3
/**
4
 * This file is part of slick/web_stack package
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
namespace Slick\WebStack\Controller;
11
12
use Slick\WebStack\ControllerInterface;
13
14
/**
15
 * Trait Controller Methods
16
 *
17
 * @package Slick\WebStack\Controller
18
 */
19
trait ControllerMethods
20
{
21
22
    /**
23
     * @var ControllerContextInterface
24
     */
25
    protected $context;
26
27
    /**
28
     * @var array
29
     */
30
    protected $data = [];
31
32
    /**
33
     * Sets te context for this controller execution
34
     *
35
     * @param ControllerContextInterface $context
36
     *
37
     * @return self|ControllerInterface
38
     */
39
    public function runWithContext(ControllerContextInterface $context)
40
    {
41
        $this->context = $context;
42
        return $this;
43
    }
44
45
    /**
46
     * Sets a variable to the view data model
47
     *
48
     * If you provide an associative array in the $name argument it will be
49
     * set all the elements using the key as the variable name on view
50
     * data model.
51
     *
52
     * @param string|array $name
53
     * @param mixed $value
54
     *
55
     * @return self|ControllerInterface
56
     */
57
    public function set($name, $value = null)
58
    {
59
        if (is_array($name)) {
60
            return $this->setValues($name);
61
        }
62
        $this->data[$name] = $value;
63
        return $this;
64
    }
65
66
    /**
67
     * A view data model used by renderer
68
     *
69
     * @return array
70
     */
71
    public function data()
72
    {
73
        return $this->data;
74
    }
75
76
    /**
77
     * Adds a multiple values from an associative array
78
     *
79
     * @param array $data
80
     *
81
     * @return self|ControllerInterface
82
     */
83
    private function setValues(array $data)
84
    {
85
        foreach ($data as $key => $datum) {
86
            $this->set($key, $datum);
87
        }
88
        return $this;
89
    }
90
}
91