View::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
namespace FMUP;
3
4
use FMUP\Exception\UnexpectedValue;
5
6
/**
7
 * Class View
8
 * /!\ Beware this version is not compliant with FMU View since layout are hardcoded.
9
 * With FMUP\View you'll be able to inject Views to views
10
 *
11
 * @package FMUP
12
 */
13
class View
14
{
15
    private $viewPath;
16
    private $params = array();
17
18
    /**
19
     * @param array $params
20
     */
21 4
    public function __construct(array $params = array())
22
    {
23 4
        $this->addParams((array)$params);
24 4
    }
25
26
    /**
27
     * Define multiple value for key (associative array)
28
     * @param array $params
29
     * @return $this
30
     */
31 6
    public function addParams(array $params = array())
32
    {
33 6
        $this->params = array_merge($this->params, $params);
34 6
        return $this;
35
    }
36
37
    /**
38
     * Define a value for a specific key
39
     * @param string $name
40
     * @param mixed $value
41
     * @throws UnexpectedValue
42
     * @return $this
43
     */
44 2 View Code Duplication
    public function setParam($name, $value)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
45
    {
46 2
        if (!is_string($name)) {
47 1
            throw new UnexpectedValue(UnexpectedValue::MESSAGE_TYPE_NOT_STRING, UnexpectedValue::CODE_TYPE_NOT_STRING);
48
        }
49 2
        if (empty($name)) {
50 1
            throw new UnexpectedValue(UnexpectedValue::MESSAGE_VALUE_EMPTY, UnexpectedValue::CODE_VALUE_EMPTY);
51
        }
52 2
        $this->params[$name] = $value;
53 2
        return $this;
54
    }
55
56
    /**
57
     * Get defined value for a specific key
58
     * @param string $name
59
     * @throws UnexpectedValue
60
     * @return mixed
61
     */
62 4 View Code Duplication
    public function getParam($name)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
63
    {
64 4
        if (!is_string($name)) {
65 1
            throw new UnexpectedValue(UnexpectedValue::MESSAGE_TYPE_NOT_STRING, UnexpectedValue::CODE_TYPE_NOT_STRING);
66
        }
67 4
        if (empty($name)) {
68 1
            throw new UnexpectedValue(UnexpectedValue::MESSAGE_VALUE_EMPTY, UnexpectedValue::CODE_VALUE_EMPTY);
69
        }
70 4
        return isset($this->params[$name]) ? $this->params[$name] : null;
71
    }
72
73
    /**
74
     * Return defined params
75
     * @return array
76
     */
77 4
    public function getParams()
78
    {
79 4
        return $this->params;
80
    }
81
82
    /**
83
     * Return string of interpreted template
84
     * @return string
85
     * @throws UnexpectedValue
86
     */
87 2
    public function render()
88
    {
89 2
        if (is_null($this->getViewPath())) {
90 1
            throw new UnexpectedValue(
91 1
                'View must be defined : ' . $this->getViewPath(),
92 1
                UnexpectedValue::CODE_VALUE_NULL
93
            );
94
        }
95 2
        if (!file_exists($this->getViewPath())) {
96 1
            throw new UnexpectedValue(
97 1
                'File does not exist : ' . $this->getViewPath(),
98 1
                UnexpectedValue::CODE_VALUE_INVALID_FILE_PATH
99
            );
100
        }
101 2
        ob_start();
102 2
        $vars = $this->getParams();
103 2
        extract($vars); //for compliance only - @todo remove this line
104 2
        require($this->getViewPath());
105 2
        return ob_get_clean();
106
    }
107
108
    /**
109
     * Define view to use
110
     * @param string $viewPath Full path to view
111
     * @throws UnexpectedValue
112
     * @return $this
113
     */
114 3 View Code Duplication
    public function setViewPath($viewPath)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
115
    {
116 3
        if (!is_string($viewPath)) {
117 1
            throw new UnexpectedValue(UnexpectedValue::MESSAGE_TYPE_NOT_STRING, UnexpectedValue::CODE_TYPE_NOT_STRING);
118
        }
119 3
        if (empty($viewPath)) {
120 1
            throw new UnexpectedValue(UnexpectedValue::MESSAGE_VALUE_EMPTY, UnexpectedValue::CODE_VALUE_EMPTY);
121
        }
122
123 3
        $this->viewPath = $viewPath;
124 3
        return $this;
125
    }
126
127
    /**
128
     * Return defined view path
129
     * @return mixed
130
     */
131 3
    public function getViewPath()
132
    {
133 3
        return $this->viewPath;
134
    }
135
136
    /**
137
     * Implements object use
138
     * @param string $param
139
     * @return mixed
140
     */
141 1
    public function __get($param)
142
    {
143 1
        return $this->getParam($param);
144
    }
145
146
    /**
147
     * Implements object use
148
     * @param string $param
149
     * @param mixed $value
150
     * @throws UnexpectedValue
151
     * @return View
152
     */
153 1
    public function __set($param, $value)
154
    {
155 1
        return $this->setParam($param, $value);
156
    }
157
}
158