Completed
Push — master ( 27bd14...d9eb91 )
by Vitaly
02:03
created

View::__get()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 8
ccs 4
cts 4
cp 1
rs 9.4285
cc 2
eloc 5
nc 2
nop 1
crap 2
1
<?php
2
/**
3
 * Created by Vitaly Iegorov <[email protected]>.
4
 * on 18.02.16 at 14:15
5
 */
6
namespace samsonframework\view;
7
8
use samsonframework\core\RenderInterface;
9
use samsonframework\core\ViewInterface;
10
use samsonframework\view\exception\VariableKeyNotFound;
11
use samsonframework\view\exception\ViewFileNotFound;
12
13
/**
14
 * View class for rendering.
15
 * @package samsonframework\view
16
 */
17
class View implements ViewInterface
18
{
19
    /** Default view file extension */
20
    const DEFAULT_EXT = 'vphp';
21
22
    /** @deprecated Class name for old PHP versions */
23
    const CLASSNAME = __CLASS__;
24
25
    /** @var array Collection of $key => $value view data */
26
    protected $data = array();
27
28
    /** @var string Full path to view file */
29
    protected $file;
30
31
    /** @var string Rendered view contents */
32
    protected $output;
33
34
    /**
35
     * Set current view for rendering.
36
     * Method searches for the shortest matching view path by $pathPattern,
37
     * from loaded views.
38
     *
39
     * Module saves all view data that has been set to a specific view in appropriate
40
     * view data collection entry. By default module creates vied data entry - VD_POINTER_DEF,
41
     * and until any call of iModule::view() or iModule::output(), all data that is iModule::set(),
42
     * is stored to that location.
43
     *
44
     * On the first call of iModule::view() or iModule::output(), this method changes the view data
45
     * pointer to actual relative view path, and copies(actually just sets view data pointer) all view
46
     * data set before to new view data pointer. This guarantees backward compatibility and gives
47
     * opportunity not to set the view path before setting view data to it.
48
     *
49
     * @param string $pathPattern Path pattern for view searching
50
     *
51
     * @return $this Chaining
52
     * @throws \Exception
53
     */
54 2
    public function view($pathPattern)
55
    {
56 2
        if (file_exists($pathPattern)) {
57 2
            $this->file = $pathPattern;
58 2
        } else {
59 1
            throw new ViewFileNotFound($pathPattern);
60
        }
61
62 2
        return $this;
63
    }
64
65
    /**
66
     * Render current view.
67
     * Method uses current view context and outputs rendering
68
     * result.
69
     *
70
     * @return string Rendered view
71
     */
72 1
    public function output()
73
    {
74
        // Start buffering
75 1
        ob_start();
76
77
        // Make variables accessible directly in view
78 1
        extract($this->data);
79
80
        // Render view file
81 1
        include($this->file);
82
83
        // Store buffer output
84 1
        $this->output = ob_get_contents();
85
86
        // Clear buffer
87 1
        ob_end_clean();
88
89
        // Returned rendered view
90 1
        return $this->output;
91
    }
92
93
    /**
94
     * Magic method for getting view variables.
95
     *
96
     * @param string $name Variable key
97
     *
98
     * @return mixed Value
99
     * @throws VariableKeyNotFound
100
     */
101 5
    public function __get($name)
102
    {
103 5
        if (array_key_exists($name, $this->data)) {
104 5
            return $this->data[$name];
105
        } else {
106 1
            throw new VariableKeyNotFound($name);
107
        }
108
    }
109
110
    /**
111
     * Magic method for setting view variables.
112
     *
113
     * @param string $name      Variable key
114
     * @param array  $arguments Variable value
115
     *
116
     * @return $this Chaining
117
     * @throws VariableKeyNotFound
118
     */
119 1
    public function __call($name, $arguments)
120
    {
121 1
        if (count($arguments)) {
122 1
            $this->set($arguments[0], $name);
123 1
        }
124
125 1
        return $this;
126
    }
127
128
    /**
129
     * Set view variable.
130
     *
131
     * Passing an array as $value will add array key => values into current
132
     * view data collection. If $key is passed then an array variable with this
133
     * key will be added to view data collection beside adding array key => values.
134
     *
135
     * @param mixed       $value Variable value
136
     * @param string|null $key   Variable key\prefix for objects and arrays
137
     *
138
     * @return $this Chaining
139
     */
140 5
    public function set($value, $key = null)
141
    {
142
        // RenderInterface implementation
143 5
        if (is_object($value) && is_a($value, RenderInterface::class)) {
144
            /** @var RenderInterface $value */
145
            // Generate objects view array data and merge it with view data
146 3
            $this->data = array_merge(
147 3
                $this->data,
148 3
                $value->toView(null !== $key ? $key : get_class($value))
149 3
            );
150 5
        } elseif (is_array($value)) { // Merge array into view data
151 2
            $this->data = array_merge($this->data, $value);
152 2
        }
153
154
        // Store key value
155 5
        $this->data[$key] = $value;
156
157 5
        return $this;
158
    }
159
}
160