Completed
Push — master ( 89f95d...43ee50 )
by Wanderson
02:15
created

View::getTitle()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
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
	public function getTitle() {
75
		return $this->getData('title');
76
	}
77
78
	/**
79
	 * Define o arquivo da view
80
	 * @param string $file
81
	 */
82
	private function setFile($file) {
83
		$filePath = BASE_PATH . static::$dir . $file;
84
85
		if (!is_null(Template::instance()->getTheme())):
86
			$filePath = Template::instance()->getFilePath(BASE_PATH . static::$dir, $file);
87
		endif;
88
89
		if (file_exists($filePath . '.phtml')):
90
			$this->file = $filePath . '.phtml';
91
		endif;
92
	}
93
94
	/**
95
	 * Retorna true se a view existe
96
	 * @return boolean
97
	 */
98
	public function exists() {
99
		return (file_exists($this->file));
100
	}
101
102
	/**
103
	 * Se arquivo nao existe, define como 404
104
	 */
105
	public function validate() {
106
		if (!$this->exists() && $this->app->getPage() !== '404'):
107
			$this->app->pageNotFound();
108
		endif;
109
	}
110
111
	/**
112
	 * Retorna o html da view
113
	 * @return string
114
	 */
115
	public function __toString() {
116
		return $this->toString();
117
	}
118
119
	/**
120
	 * Retorna o html da view
121
	 * @return string
122
	 */
123
	public function toString() {
124
		ob_start();
125
		$this->toHtml();
126
		return ob_get_clean();
127
	}
128
129
	/**
130
	 * Carrega o arquivo da view, imprimindo o resultado html
131
	 * @return string
132
	 */
133
	public function load() {
134
		$this->toHtml();
135
	}
136
137
	/**
138
	 * Carrega o arquivo da view, imprimindo o resultado html
139
	 */
140
	public function toHtml() {
141
		if (!is_null($this->file)):
142
			extract($this->data);
143
			include $this->file;
144
		endif;
145
	}
146
147
}
148