-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathpudlInterfaces.php
More file actions
104 lines (76 loc) · 3.13 KB
/
pudlInterfaces.php
File metadata and controls
104 lines (76 loc) · 3.13 KB
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
<?php
////////////////////////////////////////////////////////////////////////////////
// CHECK IF THE GIVEN ITEM IS EITHER AN ARRAY
// OR AN OBJECT IMPLEMENTING ARRAYACCESS
////////////////////////////////////////////////////////////////////////////////
function pudl_array($item) {
return is_array($item) || ($item instanceof ArrayAccess);
}
////////////////////////////////////////////////////////////////////////////////
// VERIFY THE REQUIRED PHP EXTENSION IS INSTALLED ON THE LOCAL SERVER
////////////////////////////////////////////////////////////////////////////////
function pudl_require_extension($extension) {
if (extension_loaded($extension)) return;
throw new pudlExtensionException(
NULL,
'Required PHP extension is missing: ' . $extension
);
}
////////////////////////////////////////////////////////////////////////////////
// HELPER INTERFACE TO SHOW PUDL THE GIVEN CLASS BELONGS TO THIS LIBRARY
// THIS SHOULD *NOT* BE USED OUTSIDE OF THE PUDL LIBRARY
////////////////////////////////////////////////////////////////////////////////
interface pudlHelper {}
////////////////////////////////////////////////////////////////////////////////
// ALLOW A CLASS TO RETURN AN ID RECOGNIZED BY PUDL
////////////////////////////////////////////////////////////////////////////////
interface pudlId {
public function pudlId();
}
////////////////////////////////////////////////////////////////////////////////
// ALLOW A CLASS TO RETURN A VALUE RECOGNIZED BY PUDL
////////////////////////////////////////////////////////////////////////////////
interface pudlValue {
public function pudlValue(pudl $pudl, $quote=true);
}
////////////////////////////////////////////////////////////////////////////////
// FIX FOR PUDL OBJECT RECURSIVE ARRAYS
// SOURCE: http://php.net/manual/en/function.array-diff-assoc.php#111675
////////////////////////////////////////////////////////////////////////////////
if (!function_exists('array_diff_assoc_recursive')) {
function array_diff_assoc_recursive($array1, $array2) {
$array1 = (array) $array1;
$array2 = (array) $array2;
$difference = [];
foreach($array1 as $key => $value) {
if (pudl_array($value)) {
if(!array_key_exists($key, $array2) || !pudl_array($array2[$key])) {
$difference[$key] = $value;
} else {
$new_diff = array_diff_assoc_recursive(
(array) $value,
(array) $array2[$key]
);
if(!empty($new_diff)) $difference[$key] = $new_diff;
}
} else if(!array_key_exists($key, $array2) || $array2[$key] !== $value) {
$difference[$key] = $value;
}
}
return $difference;
}
}
////////////////////////////////////////////////////////////////////////////////
// FIX FOR PHP NOT HAVING A STR_PUTCSV
// SOURCE: https://www.php.net/manual/en/function.fputcsv.php
////////////////////////////////////////////////////////////////////////////////
if (!function_exists('str_putcsv')) {
function str_putcsv($fields, $delimiter=',', $enclosure='"', $escape='\\') {
$fp = fopen('php://temp', 'w');
fputcsv($fp, $fields, $delimiter, $enclosure, $escape);
fseek($fp, 0, SEEK_SET);
$csv = stream_get_contents($fp);
fclose($fp);
return $csv;
}
}