-
Notifications
You must be signed in to change notification settings - Fork 0
/
Obj.php
executable file
·105 lines (97 loc) · 3.74 KB
/
Obj.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
<?php
/**
* Bioversity AGROVOC Indexing
*
* PHP Version 7.2.11
*
* @copyright
* @license http://www.gnu.org/licenses/gpl-3.0.html GNU General Public License, version 3
* @link https://github.com/gubi/bioversity_agrovoc-indexing
*/
/**
* A script for manage XML file and prepare data for Dataverse
*
* @package Bioversity AGROVOC Indexing
* @author Alessandro Gubitosi <[email protected]>
* @license http://www.gnu.org/licenses/gpl-3.0.html GNU General Public License, version 3
* @link https://github.com/gubi/bioversity_agrovoc-indexing
*/
class Obj {
/**
* Sort and list all array items in human readable format
* @example [1, 3, 2] ==> 1, 2 and 3
*
* @param int|array $array The array to display
* @return int|string An integer or a string describing the array elements or
*/
public static function list($array) {
if(is_array($array)) {
sort($array);
$res = preg_replace('/\,\s+(\w+)$/', " and $1", implode(", ", $array));
return (is_numeric($res)) ? (int)$res : $res;
}
}
/**
* Convert an array to an object
*
* @param array $array The array to convert
* @return object The converted object
*/
public static function array_to_object($array) {
return json_decode(json_encode($array));
}
/**
* Convert an object to an array
*
* @param object $object The object to convert
* @return object The converted array
*/
public static function object_to_array($object) {
return json_decode(json_encode($array), 1);
}
/**
* Move an array item to the top of order
*
* @param array $array The array to sort
* @param string $key The item to move to the top
* @return array The sorted array
*/
public static function move_to_top($array, $key) {
return array_splice($array, array_search($key, array_keys($array)), 1) + $array;
}
/**
* Move an array item to the bottom of order
*
* @param array $array The array to sort
* @param string $key The item to move to the bottom
* @return array The sorted array
*/
public static function move_to_bottom($array, $key) {
if(is_object($array)) {
$array = object_to_array($array);
}
return $array + array_splice($array, array_search($key, array_keys($array)), 1);
}
/**
* Display data on screen
*
* @param string|object|array $data What to display
* @param boolean $json Display as JSON?
*/
function output($data, $json = false) {
if($json) {
// Display the output as json
header("Content-type: application/json");
// print_r(json_encode($changes, JSON_PRETTY_PRINT));
if(is_array($data)) {
print_r(json_encode($data, JSON_PRETTY_PRINT));
} else {
print_r($data);
}
} else {
header("Content-type: text/plain");
print_r($data);
}
}
}
?>