-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathView.php
More file actions
168 lines (150 loc) · 4.05 KB
/
View.php
File metadata and controls
168 lines (150 loc) · 4.05 KB
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
<?php
/*
* Opulence
*
* @link https://www.opulencephp.com
* @copyright Copyright (C) 2021 David Young
* @license https://github.com/opulencephp/Opulence/blob/1.2/LICENSE.md
*/
namespace Opulence\Views;
/**
* Defines a basic view
*/
class View implements IView
{
/** The default open tag for unsanitized delimiter */
const DEFAULT_OPEN_UNSANITIZED_TAG_DELIMITER = '{{!';
/** The default close tag for unsanitized delimiter */
const DEFAULT_CLOSE_UNSANITIZED_TAG_DELIMITER = '!}}';
/** The default open tag for sanitized delimiter */
const DEFAULT_OPEN_SANITIZED_TAG_DELIMITER = '{{';
/** The default close tag for sanitized delimiter */
const DEFAULT_CLOSE_SANITIZED_TAG_DELIMITER = '}}';
/** The default open tag for directive delimiter */
const DEFAULT_OPEN_DIRECTIVE_DELIMITER = '<%';
/** The default close tag for directive delimiter */
const DEFAULT_CLOSE_DIRECTIVE_DELIMITER = '%>';
/** The default open tag for comment delimiter */
const DEFAULT_OPEN_COMMENT_DELIMITER = '{#';
/** The default close tag for comment delimiter */
const DEFAULT_CLOSE_COMMENT_DELIMITER = '#}';
/** @var string The uncompiled contents of the view */
protected $contents = '';
/** @var string The path to the raw view */
protected $path = '';
/** @var array The mapping of PHP variable names to their values */
protected $vars = [];
/** @var array The stack of parent delimiter types to values */
private $delimiters = [
self::DELIMITER_TYPE_UNSANITIZED_TAG => [
self::DEFAULT_OPEN_UNSANITIZED_TAG_DELIMITER,
self::DEFAULT_CLOSE_UNSANITIZED_TAG_DELIMITER
],
self::DELIMITER_TYPE_SANITIZED_TAG => [
self::DEFAULT_OPEN_SANITIZED_TAG_DELIMITER,
self::DEFAULT_CLOSE_SANITIZED_TAG_DELIMITER
],
self::DELIMITER_TYPE_DIRECTIVE => [
self::DEFAULT_OPEN_DIRECTIVE_DELIMITER,
self::DEFAULT_CLOSE_DIRECTIVE_DELIMITER
],
self::DELIMITER_TYPE_COMMENT => [
self::DEFAULT_OPEN_COMMENT_DELIMITER,
self::DEFAULT_CLOSE_COMMENT_DELIMITER
]
];
/**
* @param string $path The path to the raw view
* @param string $contents The contents of the view
*/
public function __construct(string $path = '', string $contents = '')
{
$this->setPath($path);
$this->setContents($contents);
}
/**
* @inheritdoc
*/
public function getContents() : string
{
return $this->contents;
}
/**
* @inheritdoc
*/
public function getDelimiters($type) : array
{
if (!isset($this->delimiters[$type])) {
return [null, null];
}
return $this->delimiters[$type];
}
/**
* @inheritdoc
*/
public function getPath() : string
{
return $this->path;
}
/**
* @inheritdoc
*/
public function getVar(string $name)
{
if (isset($this->vars[$name])) {
return $this->vars[$name];
}
return null;
}
/**
* @inheritdoc
*/
public function getVars() : array
{
return $this->vars;
}
/**
* @inheritdoc
*/
public function hasVar(string $name) : bool
{
return array_key_exists($name, $this->vars);
}
/**
* @inheritdoc
*/
public function setContents(string $contents)
{
$this->contents = $contents;
}
/**
* @inheritdoc
*/
public function setDelimiters($type, array $values)
{
$this->delimiters[$type] = $values;
}
/**
* @inheritdoc
*/
public function setPath(string $path)
{
$this->path = $path;
}
/**
* @inheritdoc
*/
public function setVar(string $name, $value)
{
$this->vars[$name] = $value;
}
/**
* @inheritdoc
*/
public function setVars(array $namesToValues)
{
foreach ($namesToValues as $name => $value) {
$this->setVar($name, $value);
}
}
}