-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcss_generator.php
executable file
·257 lines (224 loc) · 4.55 KB
/
css_generator.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
253
254
255
256
257
<?php
function generate_styles($styleIndex = null) {
$options = get_option();
$Stylesheet = new CssStyleSheet();
foreach ($styleIndex as $style) {
if (array_key_exists($style['option_id'], $options)) {
$CssAttribute = new CssAttribute($style['selector']);
$CssAttribute->addProperty($style['property'], $options[$style['option_id']]);
$Stylesheet->addAttribute($CssAttribute);
}
}
echo $Stylesheet->toString(true); // pass true to compress output
}
/**
* CssStyleSheet
*
* Generates a style sheet
*
* @package default
* @author Josh Mahony (Republique Design)
**/
class CssStyleSheet {
/**
* attributes
*
* Holds an array of CssAttribute objects
* with the selector as the key
*
* @var array
**/
private $attributes;
function __construct() {
$this->attributes = array();
}
/**
* toString
*
* Iterates through the attributes array calling the
* toString method of each attribute object
*
* @return String
**/
public function toString($compression = false) {
$header = '<style type="text/css">';
$css = '';
$footer = '</style>';
foreach ($this->attributes as $attribute) {
$css .= $attribute->toString();
}
if ($compression) {
$css = $this->compress($css);
}
return $header . $css . $footer;
}
/**
* addAttribute
*
* Adds an attribute to the stylesheet.
* Will merge with attribute if selector already
* exists in the attributes array.
*
* @return void
* @param CssAttribute object
**/
public function addAttribute(CssAttribute $CssAttribute) {
$selector = $CssAttribute->getSelector();
if ($this->hasAttribute($selector)) {
$this->attributes[$selector]->merge($CssAttribute);
} else {
$this->attributes[$selector] = $CssAttribute;
}
}
/**
* hasAttribute
*
* Checks if selector already exists in the attributes array
*
* @return Boolean
* @param selector
**/
private function hasAttribute($selector = '') {
return array_key_exists($selector, $this->attributes);
}
/**
* compress
*
* @param String css
* @return String
* @author
**/
private function compress($css = '') {
$css = preg_replace('/\\n+|\\t+/', '', $css);
return $css;
}
} // END class
/**
* CssAttribute
*
* Stores the selector and properties of a CSS attribute
*
* @package default
* @author Josh Mahony (Republique Design)
* @
**/
class CssAttribute {
/**
* selector
*
* Holds the selector part of the CSS attribute,
* e.g. the 'heading' part of .heading { }
*
* @var String
**/
private $selector;
/**
* properties
*
*
* Holds the properties part of the CSS attribute,
* e.g. anything between { } part of .heading { }
* @example
* array(
* 'color' => 'red'
* );
*
* @var Array
**/
private $properties;
/**
* Constructor
*
* Assign the selector type and selector for the attribute
*
* @return void
* @param selector
**/
function __construct($selector = '') {
$this->selector = $selector;
$this->properties = array();
}
/**
* addProperty
*
* Add a property to the properties array, e.g.
* addProperty('color','red')
*
* @return void
* @param type
* @param value
**/
public function addProperty($type = '', $value = ''){
$this->properties[$type] = $value;
}
/**
* toString
*
* Converts the properties to a printable string
*
* @return String
**/
public function toString() {
$attribute = '';
$attribute .= $this->selector . '{';
foreach ($this->properties as $property => $value) {
if ($value != null) {
$attribute .= $property . ':';
if ($this->isURL($value)) {
$attribute .= 'url("' . $value . '");';
} else {
$attribute .= $value . ';';
}
}
}
$attribute .= '}';
return $attribute;
}
/**
* merges
*
* Merges supplied CssAttribute with itself
*
* @return String
* @param CssAttribute object
**/
public function merge(CssAttribute $CssAttribute) {
$properties = $CssAttribute->getProperties();
$this->properties = array_merge($this->properties, $properties);
}
/**
* isURL
*
* Retruns whether or not a string is a URL
*
* @param url
* @return Boolean
**/
public function isURL($url = '') {
if (strpos($url, 'http') > -1) {
return true;
}
return false;
}
/**
* getProperties
*
* Returns the properties array
*
* @return Array
**/
private function getProperties() {
return $this->properties;
}
/**
* getSelector
*
* Returns the selector string
*
* @return String
**/
public function getSelector() {
return $this->selector;
}
} // END class
?>