forked from Furgas/php-api-library
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkyHelpers.php
393 lines (340 loc) · 10.5 KB
/
kyHelpers.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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
<?php
/**
* Helper functions and classes.
*
* @author Tomasz Sawicki (https://github.com/Furgas)
* @package Common
*/
/**
* Base exception class used by library.
*
* @package Common
*/
class kyException extends Exception {
}
if (!function_exists('ky_xml_to_array')) {
/**
* Transforms XML data to array.
*
* @param string $xml XML data.
* @param string[] $namespaces List of namespaces to include in parsing or empty to include all namespaces.
* @return array
*/
function ky_xml_to_array($xml, $namespaces = null) {
$iter = 0;
$arr = array();
if (is_string($xml))
$xml = new SimpleXMLElement($xml);
if (!($xml instanceof SimpleXMLElement))
return $arr;
if ($namespaces === null)
$namespaces = $xml->getDocNamespaces(true);
foreach ($xml->attributes() as $attributeName => $attributeValue) {
$arr["_attributes"][$attributeName] = trim($attributeValue);
}
foreach ($namespaces as $namespace_prefix => $namespace_name) {
foreach ($xml->attributes($namespace_prefix, true) as $attributeName => $attributeValue) {
$arr["_attributes"][$namespace_prefix.':'.$attributeName] = trim($attributeValue);
}
}
$has_children = false;
foreach ($xml->children() as $element) {
/** @var $element SimpleXMLElement */
$has_children = true;
$elementName = $element->getName();
if ($element->children()) {
$arr[$elementName][] = ky_xml_to_array($element, $namespaces);
} else {
$shouldCreateArray = array_key_exists($elementName, $arr) && !is_array($arr[$elementName]);
if ($shouldCreateArray) {
$arr[$elementName] = array($arr[$elementName]);
}
$shouldAddValueToArray = array_key_exists($elementName, $arr) && is_array($arr[$elementName]);
if ($shouldAddValueToArray) {
$arr[$elementName][] = trim($element[0]);
} else {
$arr[$elementName] = trim($element[0]);
}
}
$iter++;
}
if (!$has_children) {
$arr['_contents'] = trim($xml[0]);
}
return $arr;
}
}
if (!function_exists('ky_seconds_format')) {
/**
* Outputs seconds in hh:mm:ss format.
*
* @param int $seconds Seconds.
* @return string
*/
function ky_seconds_format($seconds) {
if (!is_numeric($seconds))
return $seconds;
$minus = $seconds < 0 ? "-" : "";
$seconds = abs($seconds);
$formatted_seconds = str_pad(($seconds % 60), 2, "0", STR_PAD_LEFT);
$minutes = floor($seconds / 60);
$formatted_minutes = str_pad(($minutes % 60), 2, "0", STR_PAD_LEFT);
$formatted_hours = str_pad(floor($minutes / 60), 2, "0", STR_PAD_LEFT);
return sprintf("%s%s:%s:%s", $minus, $formatted_hours, $formatted_minutes, $formatted_seconds);
}
}
if (!function_exists('ky_bytes_format')) {
/**
* Outputs formatted bytes.
*
* @param int $bytes Bytes.
* @return string
*/
function ky_bytes_format($bytes) {
$unim = array("B","KB","MB","GB","TB","PB");
$c = 0;
while ($bytes>=1024) {
$c++;
$bytes = $bytes/1024;
}
return number_format($bytes,($c ? 2 : 0),",",".")." ".$unim[$c];
}
}
if (!function_exists('ky_usort_comparison')) {
/**
* Helper class for sorting array with parametrized callback.
*
* @author Tomasz Sawicki (https://github.com/Furgas)
* @package Common
*/
class kyUsort {
/**
* Sorting callback.
* @var callback
*/
private $callback;
/**
* List of arguments to sorting callback.
* @var array
*/
private $arguments;
/**
* Constructs the helper object for sorting array with parametrized callback.
*
* @param callback $callback Sorting callback.
* @param array $arguments List of arguments to sorting callback.
*/
function __construct($callback, $arguments) {
$this->callback = $callback;
$this->arguments = $arguments;
}
/**
* Proxy to the sorter callback responsible for passing additional parameters.
*
* @param mixed $a First value to compare.
* @param mixed $b Second value to compare.
* @return int
*/
public function sort($a, $b) {
$arguments = $this->arguments;
array_unshift($arguments, $a, $b);
return call_user_func_array($this->callback, $arguments);
}
}
/**
* Helper function for sorting array with parametrized callback.
*
* @param callback $callback Sorting callback.
* @param array $arguments List of arguments to sorting callback.
* @return callback
*/
function ky_usort_comparison($callback, $arguments) {
$usorter = new kyUsort($callback, $arguments);
return array($usorter, "sort");
}
}
if (!function_exists('ky_get_post_value')) {
/**
* Returns field value from POST data.
*
* @param kyCustomFieldDefinition $custom_field_definition Custom field definition.
* @throws kyException
* @return mixed Field value.
*/
function ky_get_post_value($custom_field_definition) {
$field_name = $custom_field_definition->getName();
$required = $custom_field_definition->getIsRequired();
$regexp = $custom_field_definition->getRegexpValidate();
$as_array = $custom_field_definition->getType() === kyCustomFieldDefinition::TYPE_CHECKBOX || $custom_field_definition->getType() === kyCustomFieldDefinition::TYPE_MULTI_SELECT;
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
return null;
}
if (!array_key_exists($field_name, $_POST)) {
if ($required)
throw new kyException("Field '%s' is required.", $custom_field_definition->getTitle());
return null;
}
if ($as_array) {
$value = $_POST[$field_name];
if (!is_array($value)) {
if (strlen(trim($value)) > 0) {
$value = array($value);
} else {
$value = array();
}
}
if ($required && count($value) === 0)
throw new kyException("Field '%s' is required.", $custom_field_definition->getTitle());
} else {
$value = trim($_POST[$field_name]);
if ($required && strlen($value) === 0)
throw new kyException("Field '%s' is required.", $custom_field_definition->getTitle());
if (strlen($regexp) > 0 && !preg_match($regexp, $value))
throw new kyException("Error validating field '%s'.", $custom_field_definition->getTitle());
}
return $value;
}
}
if (!function_exists('ky_get_tag_parameters')) {
/**
* Returns custom PHPDoc tag parameter list.
*
* Custom PHPDoc used by this library tags have following format:
* @tagName parameter1=value parameter2=value ...
*
* @param string $comment PHPDoc block.
* @param string $tag_name Custom tag name.
* @return bool|array List of parameters (may be empty if tag is parameter-less) or false when tag was not found.
*/
function ky_get_tag_parameters($comment, $tag_name) {
$tag_pos = stripos($comment, '@'.$tag_name);
if ($tag_pos === false)
return false;
$tag_end_pos = stripos($comment, "\n", $tag_pos);
$tag = trim(substr($comment, $tag_pos, $tag_end_pos - $tag_pos));
if (strlen($tag) === 0)
return false;
$parameter_pairs = explode(' ', $tag);
$parameters = array();
foreach ($parameter_pairs as $parameter_pair) {
if (stripos($parameter_pair, '=') === false)
continue;
list($name, $value) = explode('=', $parameter_pair);
if (array_key_exists($name, $parameters)) {
if (!is_array($parameters[$name])) {
$parameters[$name] = array($parameters[$name]);
}
$parameters[$name][] = $value;
$parameters[$name] = array_unique($parameters[$name]);
if (count($parameters[$name]) === 1) {
$parameters[$name] = reset($parameters[$name]);
}
} else {
$parameters[$name] = $value;
}
}
return $parameters;
}
}
if (!function_exists('ky_assure_string')) {
/**
* Returns specified value as string.
*
* @param mixed $value Value.
* @param string|null $value_on_null What to return if value is null.
* @return string|null
*/
function ky_assure_string($value, $value_on_null = null) {
return $value !== null ? strval($value) : $value_on_null;
}
}
if (!function_exists('ky_assure_int')) {
/**
* Returns specified value as int.
*
* @param mixed $value Value.
* @param int|null $value_on_null What to return if value is null.
* @return int|null
*/
function ky_assure_int($value, $value_on_null = null) {
return $value !== null ? intval($value) : $value_on_null;
}
}
if (!function_exists('ky_assure_positive_int')) {
/**
* Returns specified value as positive int.
*
* @param mixed $value Value.
* @param int|null $value_on_non_positive What to return if value non-positive (including null).
* @return int|null
*/
function ky_assure_positive_int($value, $value_on_non_positive = null) {
return intval($value) > 0 ? intval($value) : $value_on_non_positive;
}
}
if (!function_exists('ky_assure_bool')) {
/**
* Returns specified value as bool.
*
* @param mixed $value Value.
* @return bool
*/
function ky_assure_bool($value) {
return $value ? true : false;
}
}
if (!function_exists('ky_assure_array')) {
/**
* Returns specified value as array.
*
* @param mixed $value Value.
* @param mixed $value_on_null What to return if value is null.
* @return mixed
*/
function ky_assure_array($value, $value_on_null = array()) {
if (is_array($value))
return $value;
return $value !== null ? array($value) : $value_on_null;
}
}
if (!function_exists('ky_assure_object')) {
/**
* Returns specified object only if it is an instance of specified class.
*
* @param object $object Object.
* @param string $class_name Class name to check for.
* @param mixed $value_on_invalid_object What to return if object is not an instance os specified class.
* @return mixed
*/
function ky_assure_object($object, $class_name, $value_on_invalid_object = null) {
return $object instanceof $class_name ? $object : $value_on_invalid_object;
}
}
if (!function_exists('ky_assure_constant')) {
/**
* Assures that specified value is proper constant value.
*
* @param mixed $value Value.
* @param string|object $object_or_class_name Object or class name with constants.
* @param string $constant_prefix Constants prefix (without last '_').
* @param mixed $value_on_invalid_constant What to return if value is not a valid constant.
* @return mixed
*/
function ky_assure_constant($value, $object_or_class_name, $constant_prefix, $value_on_invalid_constant = null) {
if (is_string($object_or_class_name)) {
$class_name = $object_or_class_name;
} elseif (is_object($object_or_class_name)) {
$class_name = get_class($object_or_class_name);
} else {
return $value_on_invalid_constant;
}
$class = new ReflectionClass($class_name);
foreach ($class->getConstants() as $constant_name => $constant_value) {
if (stripos($constant_name, $constant_prefix.'_') !== 0)
continue;
if ($value == $constant_value)
return $constant_value;
}
return $value_on_invalid_constant;
}
}