forked from istitutoculturaitaliana/sf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbrowser.php
252 lines (182 loc) · 4.87 KB
/
browser.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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
<?php
/**
* This script contains some basic functions
* to assist the creation of stylesheets.
* For instance, it can provide an abstraction
* layer regarding the various browser's versions, since
* the passed attributes can be interpolated
* by the PHP functions
*
*
* @package browser.php
* @todo implement the various "abstraction layers"
* to specific (browser-related) css properties
*
*/
$GLOBALS['css'] = array();
/**
* This function is used to scale
* the measures expressed in pixels
* in a similar way the zoom function
* of the browser does: everything is
* scaled, both text and images.
*
* @param int $px Value expressed in pixels
* @return string Interpolated pixel value with suffix 'px'
*
* @todo TO BE IMPLEMENTED!
*/
function __px__($px) {
return $px . 'px';
}
/**
* This function records the css attributes
* of selector or list of selectors (tags, classes
* and so on) for a further elaboration and
* use
*
* @param array $array A multidimensional
* array with selectors (or list of
* selectors) as keys, and an array of
* attributes / values as values
*/
function __css__($array) {
foreach($array as $key => $value) {
/**
* @var string $key The selector name
*/
if(!array_key_exists($key,$GLOBALS['css']))
$GLOBALS['css'][$key] = array();
/**
* @var string $key_ The attribute name
* @var string $value_ The attribute value
*
* @todo The attribute value could be further
* "interpolated", providing a kind of abstraction
* layer
*/
foreach($value as $key_ => $value_) {
$GLOBALS['css'][$key][$key_] = $value_;
}
}
}
/**
* A compacted form of the Str
* class which can be found in sf.php
* We only use it to return a string
* when an array is converted into it
*/
class Str_ {
/**
* @var array $str_array
* An array of strings. This is the "class
* argument" assigned by the constructor
*/
private $str_array = array();
/**
* @var bool $is_array Records if the parameter passed to the
* constructor is an array, to provide the expected
* output when the get() method is called
*/
private $is_array = false;
/**
* The class constructor. We allow both
* single strings and array of strings
* to which perform any method of the class
*
* @param string|array $mixed A string or an array of strings
* @return $this Returns the class instance
*/
public function __construct($mixed) {
$this->is_array = is_array($mixed);
if(!$this->is_array)
$this->str_array = array($mixed);
else
$this->str_array = $mixed;
return $this;
}
/**
* Gets the transformed class argument
*
* @return mixed A string of an array of strings
* depending on the value of $this->is_array
*/
public function get() {
if($this->is_array)
return $this->str_array;
else
return current($this->str_array);
}
}
/**
* A simple class to handle arrays
* in the method chaining (fluent
* interface style) and to group
* array-related functions
*/
class Obj_ {
/**
* @var array $obj The class argument
* assigned by the constructor
*/
private $obj = array();
/**
* Assigns the class argument ($this->obj)
*
* @param array $obj An associative array to
* be used as class argument
*
* @return $this Returns the class instance
*/
public function __construct($obj) {
$this->obj = $obj;
return $this;
}
/**
* A simple function to "implode"
* a single-dimension associative array
*
* @param string $tok_a The token to be used to join the key and the value
* @param string $tok_b The token to be used to join key / value pairs
* @return callable Returns an instance of the class Str (documented above)
*/
public function implode_map($tok_a,$tok_b) {
$output = array();
foreach($this->obj as $key => $value)
$output []= $key . $tok_a . $value;
$output = implode($tok_b,$output);
return str($output);
}
}
/**
* A function by which calling the class Str
* without the use of the 'new' operator
*
* @param string $str The class argument
* of the new instance of Str_
*/
function str($str) {
return new Str_($str);
}
/**
* A function by which calling the class Obj
* without the use of the 'new' operator
*
*
* @param array $obj The class argument
* of the new instance of Obj_
*/
function obj($obj) {
return new Obj_($obj);
}
/**
* A function to render the
* css declarations recorded using
* the function __css__
*/
function __css__render() {
$output = array();
foreach($GLOBALS['css'] as $key => $value)
$output []= $key . '{' . obj($value)->implode_map(':',';')->get() . ';}';
return implode("\n",$output);
}