-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTinyView.php
86 lines (74 loc) · 1.82 KB
/
TinyView.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
<?php
namespace TinyLara\TinyView;
/**
* \View
*/
class TinyView {
public $view;
public $data;
public $isJson;
public function __construct($view, $isJson = false)
{
$this->view = $view;
$this->isJson = $isJson;
}
public static function make($viewName = null)
{
if ( !defined('VIEW_BASE_PATH') ) {
throw new \InvalidArgumentException("VIEW_BASE_PATH is undefined!");
}
if ( ! $viewName ) {
throw new \InvalidArgumentException("View name can not be empty!");
} else {
$viewFilePath = self::getFilePath($viewName);
if ( is_file($viewFilePath) ) {
return new TinyView($viewFilePath);
} else {
throw new \UnexpectedValueException("View file does not exist!");
}
}
}
public static function json($arr)
{
if ( !is_array($arr) ) {
throw new \UnexpectedValueException("View::json can only recieve Array!");
} else {
return new TinyView($arr, true);
}
}
public static function process($view = null)
{
if ( is_string($view) ) {
echo $view;
return;
}
if ( isset($view) && $view->isJson ) {
echo json_encode($view->view);
} else {
if ( $view instanceof TinyView ) {
if ($view->data) {
extract($view->data);
}
require $view->view;
}
}
}
public function with($key, $value = null)
{
$this->data[$key] = $value;
return $this;
}
private static function getFilePath($viewName)
{
$filePath = str_replace('.', '/', $viewName);
return VIEW_BASE_PATH.$filePath.'.php';
}
public function __call($method, $parameters)
{
if (starts_with($method, 'with'))
{
return $this->with(snake_case(substr($method, 4)), $parameters[0]);
}
throw new \BadMethodCallException("Function [$method] does not exist!");
}
}