Completed
Push — master ( e32bea...07ff99 )
by Wanderson
02:49
created

View::mergeData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 3
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace Win\Mvc;
4
5
use Win\Helper\Template;
6
7
/**
8
 * Views
9
 *
10
 * São responsáveis por criar o visual da página
11
 */
12
class View {
13
14
	public static $dir = '/app/view/';
15
16
	/**
17
	 * Ponteiro para Aplicação Principal
18
	 * @var Application
19
	 */
20
	public $app;
21
22
	/**
23
	 * Endereço completo do arquivo .phtml que contem o código html
24
	 * @var string
25
	 */
26
	private $file = null;
27
28
	/**
29
	 * Variáveis para serem usadas no arquivo da view
30
	 * @var mixed[]
31
	 */
32
	private $data;
33
34
	/**
35
	 * Cria uma View com base no arquivo escolhido
36
	 * @param string $file Nome do arquivo da view
37
	 * @param mixed[] $data Array de variaveis
38
	 */
39
	public function __construct($file, $data = []) {
40
		$this->app = Application::app();
41
		$this->setFile($file);
42
		$this->data = $data;
43
	}
44
45
	/**
46
	 * Adiciona uma variavel para usar na view
47
	 * @param string $name
48
	 * @param mixed $value
49
	 */
50
	public function addData($name, $value) {
51
		$this->data[$name] = $value;
52
	}
53
54
	/**
55
	 * Adiciona um array de variaveis para usar na view
56
	 * @param mixed[] $data
57
	 */
58
	public function mergeData(array $data) {
59
		$this->data = array_merge($this->data, $data);
60
	}
61
62
	/**
63
	 * Retorna uma variavel da view
64
	 * @param string $name
65
	 * @return mixed|null
66
	 */
67
	public function getData($name) {
68
		if (key_exists($name, $this->data)) {
69
			return $this->data[$name];
70
		}
71
		return null;
72
	}
73
74
	/**
75
	 * Define o arquivo da view
76
	 * @param string $file
77
	 */
78
	private function setFile($file) {
79
		$filePath = BASE_PATH . static::$dir . $file;
80
81
		if (!is_null(Template::instance()->getTheme())):
82
			$filePath = Template::instance()->getFilePath(BASE_PATH . static::$dir, $file);
83
		endif;
84
85
		if (file_exists($filePath . '.phtml')):
86
			$this->file = $filePath . '.phtml';
87
		endif;
88
	}
89
90
	/**
91
	 * Retorna true se a view existe
92
	 * @return boolean
93
	 */
94
	public function exists() {
95
		return (file_exists($this->file));
96
	}
97
98
	/**
99
	 * Se arquivo nao existe, define como 404
100
	 */
101
	public function validate() {
102
		if (!$this->exists() && $this->app->getPage() !== '404'):
103
			$this->app->pageNotFound();
104
		endif;
105
	}
106
107
	/**
108
	 * Retorna o html da view
109
	 * @return string
110
	 */
111
	public function __toString() {
112
		return $this->toString();
113
	}
114
115
	/**
116
	 * Retorna o html da view
117
	 * @return string
118
	 */
119
	public function toString() {
120
		ob_start();
121
		$this->toHtml();
122
		return ob_get_clean();
123
	}
124
125
	/**
126
	 * Carrega o arquivo da view, imprimindo o resultado html
127
	 * @return string
128
	 */
129
	public function load() {
130
		$this->toHtml();
131
	}
132
133
	/**
134
	 * Carrega o arquivo da view, imprimindo o resultado html
135
	 */
136
	public function toHtml() {
137
		if (!is_null($this->file)):
138
			extract($this->data);
139
			include $this->file;
140
		endif;
141
	}
142
143
}
144