-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathfunctions.php
More file actions
99 lines (85 loc) · 2.26 KB
/
functions.php
File metadata and controls
99 lines (85 loc) · 2.26 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
<?php
namespace ipl\Stdlib;
use InvalidArgumentException;
use Traversable;
use stdClass;
/**
* Detect and return the PHP type of the given subject
*
* If subject is an object, the name of the object's class is returned, otherwise the subject's type.
*
* @param $subject
*
* @return string
*/
function get_php_type($subject)
{
if (is_object($subject)) {
return get_class($subject);
} else {
return gettype($subject);
}
}
/**
* Get the array value of the given subject
*
* @param array|object|Traversable $subject
*
* @return array
*
* @throws InvalidArgumentException If subject type is invalid
*/
function arrayval($subject)
{
if (is_array($subject)) {
return $subject;
}
if ($subject instanceof stdClass) {
return (array) $subject;
}
if ($subject instanceof Traversable) {
// Works for generators too
return iterator_to_array($subject);
}
throw new InvalidArgumentException(sprintf(
'arrayval expects arrays, objects or instances of Traversable. Got %s instead.',
get_php_type($subject)
));
}
/**
* Format the given input array as CSV string and return it
*
* The input array is always expected to be an array of rows.
* The keys of the first row will be automatically used as the header row.
*
* @param iterable $data
* @param string $delimiter Field delimiter
* @param string $enclosure Field enclosure
* @param string $escape Escape character
*
* @return string
*
* @throws \InvalidArgumentException
*/
function str_putcsv($data, $delimiter = ',', $enclosure = '"', $escape = '\\')
{
$fp = fopen('php://temp', 'r+b');
if (! is_iterable($data)) {
throw new \InvalidArgumentException(sprintf(
'str_putcsv expects arrays or instances of Traversable. Got %s instead.',
get_php_type($data)
));
}
foreach ($data as $row) {
fputcsv($fp, array_keys($row), $delimiter, $enclosure, $escape);
break;
}
foreach ($data as $row) {
fputcsv($fp, $row, $delimiter, $enclosure, $escape);
}
rewind($fp);
$csv = stream_get_contents($fp);
fclose($fp);
$csv = rtrim($csv, "\n"); // fputcsv adds a newline
return $csv;
}