-
Notifications
You must be signed in to change notification settings - Fork 0
/
aBasicFormatDescriptor.php
375 lines (357 loc) · 9.64 KB
/
aBasicFormatDescriptor.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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
<?php
namespace phpWTL;
use phpWTL\aSingleton;
use phpWTL\FormatDescriptorHelper;
use phpWTL\DescriptorField;
require_once 'aSingleton.php';
require_once 'FormatDescriptorHelper.php';
require_once 'DescriptorField.php';
/**
* Abstract logging format descriptor class.
*
* @author Michael Beyer <mgbeyer@gmx.de>
* @version v0.3.1
* @api
*/
abstract class aBasicFormatDescriptor extends aSingleton {
protected static $formatFields= null;
protected static $formatPrefix= null;
protected static $formatVersion= null;
protected static $formatFieldDelimiter= null;
/**
* @param $default lists all or only fields (not) flagged as default
* @return array Get array containing all log format field IDs in their proper sequence.
*
* @author Michael Beyer <mgbeyer@gmx.de>
* @version v0.2.2
* @api
*/
public function getFieldNames($default= FormatDescriptorHelper::DEFAULT_ONLY) {
$ret= array();
foreach (static::$formatFields as $k=>$f) {
switch ($default) {
case FormatDescriptorHelper::DEFAULT_ONLY:
$add= $f->default;
break;
case FormatDescriptorHelper::DEFAULT_NONE:
$add= !$f->default;
break;
case FormatDescriptorHelper::DEFAULT_ANY:
default:
$add= true;
break;
}
if ($add) array_push($ret, $f->name);
}
return $ret;
}
/**
* @param $default lists all or only fields (not) flagged as default
* @return array Get array containing all log format meta field IDs in their proper sequence.
*
* @author Michael Beyer <mgbeyer@gmx.de>
* @version v0.2.1
* @api
*/
public function getMetaFieldNames($default= FormatDescriptorHelper::DEFAULT_ONLY) {
$ret= array();
foreach (static::$formatFields as $k=>$f) {
switch ($default) {
case FormatDescriptorHelper::DEFAULT_ONLY:
$add= $f->default;
break;
case FormatDescriptorHelper::DEFAULT_NONE:
$add= !$f->default;
break;
case FormatDescriptorHelper::DEFAULT_ANY:
default:
$add= true;
break;
}
if ($add && $f->meta) array_push($ret, $f->name);
}
return $ret;
}
/**
* @param $default lists all or only fields (not) flagged as default
* @return array Get array containing all regular (non-meta) log format field IDs in their proper sequence.
*
* @author Michael Beyer <mgbeyer@gmx.de>
* @version v0.2.2
* @api
*/
public function getRegularFieldNames($default= FormatDescriptorHelper::DEFAULT_ONLY) {
$ret= array();
foreach (static::$formatFields as $k=>$f) {
switch ($default) {
case FormatDescriptorHelper::DEFAULT_ONLY:
$add= $f->default;
break;
case FormatDescriptorHelper::DEFAULT_NONE:
$add= !$f->default;
break;
case FormatDescriptorHelper::DEFAULT_ANY:
default:
$add= true;
break;
}
if ($add && !$f->meta) array_push($ret, $f->name);
}
return $ret;
}
/**
* @param string $data Attribute to retrieve
* @param array $names Array of field names (IDs)
* @return array Get associative array containing all log format field data for a given list and attribute in their proper sequence.
*
* @author Michael Beyer <mgbeyer@gmx.de>
* @version v0.1.1
* @api
*/
public function getFieldData($data, $names= null) {
$ret= array();
foreach (static::$formatFields as $k=>$f) {
if ($names && is_array($names)) {
$pass= in_array($f->name, $names);
} else {
$pass= true;
}
if ($pass && $data) {
$ret[$f->name]= static::getFieldAttribute($f->name, $data);
}
}
return $ret;
}
/**
* @param string $field_name ID of log format field.
* @return object DescriptorField.
*
* @author Michael Beyer <mgbeyer@gmx.de>
* @version v0.1.0
* @api
*/
public function getFieldDescription($field_name) {
foreach (static::$formatFields as $k=>$f) {
if ($f->name==$field_name) return $f;
}
}
/**
* @return array Get array of all DescriptorField attribute names: name, prefix, suffix, formatter, validator, datatype.
*
* @author Michael Beyer <mgbeyer@gmx.de>
* @version v0.1.0
* @api
*/
public function getFieldAttributeNames() {
return array_keys(get_object_vars(static::$formatFields[0]));
}
/**
* @param string $field_name ID of log format field.
* @param string $attribute_name Name of the DescriptorField attribute.
* @return string Value of the DescriptorField attribute for the desired field ID.
*
* @author Michael Beyer <mgbeyer@gmx.de>
* @version v0.1.0
* @api
*/
public function getFieldAttribute($field_name, $attribute_name) {
foreach (static::$formatFields as $k=>$f) {
if ($f->name==$field_name && property_exists($f, $attribute_name)) return $f->{$attribute_name};
}
}
/**
* @param string $field_name ID of log format field.
* @param string $attribute_name Name of the DescriptorField attribute ("name", because it serves as an ID, is immutable).
* @param string $value Value to assign the DescriptorField attribute for the desired field ID.
*
* @author Michael Beyer <mgbeyer@gmx.de>
* @version v0.1.0
* @api
*/
public function setFieldAttribute($field_name, $attribute_name, $value) {
if ($attribute_name!="name" && in_array($attribute_name, static::getFieldAttributeNames())) {
foreach (static::$formatFields as $k=>$f) {
if ($f->name==$field_name) {
$f->{$attribute_name}= $value;
}
}
}
}
/**
* @return string Prefix (ID) for the log format this descriptor class represents, suffixed by an underscore.
*
* @author Michael Beyer <mgbeyer@gmx.de>
* @version v0.1.0
* @api
*/
public function getFormatPrefix() {
return static::$formatPrefix;
}
/**
* @return string Line delimiter for a "toString-like" representation of all format fields content.
*
* @author Michael Beyer <mgbeyer@gmx.de>
* @version v0.1.0
* @api
*/
public function getFormatFieldDelimiter() {
return static::$formatFieldDelimiter;
}
/**
* @return string Version for the log format this descriptor class represents.
*
* @author Michael Beyer <mgbeyer@gmx.de>
* @version v0.1.0
* @api
*/
public function getFormatVersion() {
return static::$formatVersion;
}
/**
* @author Michael Beyer <mgbeyer@gmx.de>
* @version v0.1.0
* @api
*/
public function getCaption($field_name) {
return static::getFieldAttribute($field_name, "caption");
}
/**
* @author Michael Beyer <mgbeyer@gmx.de>
* @version v0.1.0
* @api
*/
public function getPrefix($field_name) {
return static::getFieldAttribute($field_name, "prefix");
}
/**
* @author Michael Beyer <mgbeyer@gmx.de>
* @version v0.1.0
* @api
*/
public function getSuffix($field_name) {
return static::getFieldAttribute($field_name, "suffix");
}
/**
* @author Michael Beyer <mgbeyer@gmx.de>
* @version v0.1.0
* @api
*/
public function getFormatter($field_name) {
return static::getFieldAttribute($field_name, "formatter");
}
/**
* @author Michael Beyer <mgbeyer@gmx.de>
* @version v0.1.0
* @api
*/
public function getValidator($field_name) {
return static::getFieldAttribute($field_name, "validator");
}
/**
* @author Michael Beyer <mgbeyer@gmx.de>
* @version v0.1.0
* @api
*/
public function getDatatypeRaw($field_name) {
return static::getFieldAttribute($field_name, "datatype_raw");
}
/**
* @author Michael Beyer <mgbeyer@gmx.de>
* @version v0.1.0
* @api
*/
public function getDatatypeFormatted($field_name) {
return static::getFieldAttribute($field_name, "datatype_formatted");
}
/**
* @author Michael Beyer <mgbeyer@gmx.de>
* @version v0.1.0
* @api
*/
public function isDefault($field_name) {
return static::getFieldAttribute($field_name, "default");
}
/**
* @author Michael Beyer <mgbeyer@gmx.de>
* @version v0.1.0
* @api
*/
public function isMeta($field_name) {
return static::getFieldAttribute($field_name, "meta");
}
/**
* @author Michael Beyer <mgbeyer@gmx.de>
* @version v0.1.0
* @api
*/
public function setCaption($field_name, $value) {
static::setFieldAttribute($field_name, "caption", $value);
}
/**
* @author Michael Beyer <mgbeyer@gmx.de>
* @version v0.1.0
* @api
*/
public function setPrefix($field_name, $value) {
static::setFieldAttribute($field_name, "prefix", $value);
}
/**
* @author Michael Beyer <mgbeyer@gmx.de>
* @version v0.1.0
* @api
*/
public function setSuffix($field_name, $value) {
static::setFieldAttribute($field_name, "suffix", $value);
}
/**
* @author Michael Beyer <mgbeyer@gmx.de>
* @version v0.1.0
* @api
*/
public function setFormatter($field_name, $value) {
static::setFieldAttribute($field_name, "formatter", $value);
}
/**
* @author Michael Beyer <mgbeyer@gmx.de>
* @version v0.1.0
* @api
*/
public function setValidator($field_name, $value) {
static::setFieldAttribute($field_name, "validator", $value);
}
/**
* @author Michael Beyer <mgbeyer@gmx.de>
* @version v0.1.0
* @api
*/
public function setDatatypeRaw($field_name, $value) {
static::setFieldAttribute($field_name, "datatype_raw", $value);
}
/**
* @author Michael Beyer <mgbeyer@gmx.de>
* @version v0.1.0
* @api
*/
public function setDatatypeFormatted($field_name, $value) {
static::setFieldAttribute($field_name, "datatype_formatted", $value);
}
/**
* @author Michael Beyer <mgbeyer@gmx.de>
* @version v0.1.0
* @api
*/
public function setDefault($field_name, $on= true) {
$value= ($on==true);
static::setFieldAttribute($field_name, "default", $value);
}
/**
* @author Michael Beyer <mgbeyer@gmx.de>
* @version v0.1.0
* @api
*/
public function setMeta($field_name, $on= true) {
$value= ($on==true);
static::setFieldAttribute($field_name, "meta", $value);
}
}
?>