forked from xzilla/phppgadmin
/
decorator.inc.php
215 lines (179 loc) · 4.3 KB
/
decorator.inc.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
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
<?php
// $Id: decorator.inc.php,v 1.8 2007/04/05 11:09:38 mr-russ Exp $
// This group of functions and classes provides support for
// resolving values in a lazy manner (ie, as and when required)
// using the Decorator pattern.
###TODO: Better documentation!!!
// Construction functions:
function field($fieldName, $default = null) {
return new FieldDecorator($fieldName, $default);
}
function merge(/* ... */) {
return new ArrayMergeDecorator(func_get_args());
}
function concat(/* ... */) {
return new ConcatDecorator(func_get_args());
}
function callback($callback, $params = null) {
return new CallbackDecorator($callback, $params);
}
function ifempty($value, $empty, $full = null) {
return new IfEmptyDecorator($value, $empty, $full);
}
function url($base, $vars = null /* ... */) {
// If more than one array of vars is given,
// use an ArrayMergeDecorator to have them merged
// at value evaluation time.
if (func_num_args() > 2) {
$v = func_get_args();
array_shift($v);
return new UrlDecorator($base, new ArrayMergeDecorator($v));
}
return new UrlDecorator($base, $vars);
}
function replace($str, $params) {
return new replaceDecorator($str, $params);
}
// Resolving functions:
function value(&$var, &$fields, $esc = null) {
if (is_a($var, 'Decorator')) {
$val = $var->value($fields);
} else {
$val =& $var;
}
if (is_string($val)) {
switch($esc) {
case 'xml':
return strtr($val, array(
'&' => '&',
"'" => ''', '"' => '"',
'<' => '<', '>' => '>'
));
case 'html':
return htmlentities($val, ENT_COMPAT, 'UTF-8');
case 'url':
return urlencode($val);
}
}
return $val;
}
function value_xml(&$var, &$fields) {
return value($var, $fields, 'xml');
}
function value_xml_attr($attr, &$var, &$fields) {
$val = value($var, $fields, 'xml');
if (!empty($val))
return " {$attr}=\"{$val}\"";
else
return '';
}
function value_url(&$var, &$fields) {
return value($var, $fields, 'url');
}
// Underlying classes:
class Decorator
{
function Decorator($value) {
$this->v = $value;
}
function value($fields) {
return $this->v;
}
}
class FieldDecorator extends Decorator
{
function FieldDecorator($fieldName, $default = null) {
$this->f = $fieldName;
if ($default !== null) $this->d = $default;
}
function value($fields) {
return isset($fields[$this->f]) ? $fields[$this->f] : (isset($this->d) ? $this->d : null);
}
}
class ArrayMergeDecorator extends Decorator
{
function ArrayMergeDecorator($arrays) {
$this->m = $arrays;
}
function value($fields) {
$accum = array();
foreach($this->m as $var) {
$accum = array_merge($accum, value($var, $fields));
}
return $accum;
}
}
class ConcatDecorator extends Decorator
{
function ConcatDecorator($values) {
$this->c = $values;
}
function value($fields) {
$accum = '';
foreach($this->c as $var) {
$accum .= value($var, $fields);
}
return trim($accum);
}
}
class CallbackDecorator extends Decorator
{
function CallbackDecorator($callback, $param = null) {
$this->fn = $callback;
$this->p = $param;
}
function value($fields) {
return call_user_func($this->fn, $fields, $this->p);
}
}
class IfEmptyDecorator extends Decorator
{
function IfEmptyDecorator($value, $empty, $full = null) {
$this->v = $value;
$this->e = $empty;
if ($full !== null) $this->f = $full;
}
function value($fields) {
$val = value($this->v, $fields);
if (empty($val))
return value($this->e, $fields);
else
return isset($this->f) ? value($this->f, $fields) : $val;
}
}
class UrlDecorator extends Decorator
{
function UrlDecorator($base, $queryVars = null) {
$this->b = $base;
if ($queryVars !== null)
$this->q = $queryVars;
}
function value($fields) {
$url = value($this->b, $fields);
if ($url === false) return '';
if (!empty($this->q)) {
$queryVars = value($this->q, $fields);
$sep = '?';
foreach ($queryVars as $var => $value) {
$url .= $sep . value_url($var, $fields) . '=' . value_url($value, $fields);
$sep = '&';
}
}
return $url;
}
}
class replaceDecorator extends Decorator
{
function replaceDecorator($str, $params) {
$this->s = $str;
$this->p = $params;
}
function value($fields) {
$str = $this->s;
foreach ($this->p as $k => $v) {
$str = str_replace($k, value($v, $fields), $str);
}
return $str;
}
}
?>