1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* CSVelte: Slender, elegant CSV for PHP |
4
|
|
|
* |
5
|
|
|
* Inspired by Python's CSV module and Frictionless Data and the W3C's CSV |
6
|
|
|
* standardization efforts, CSVelte was written in an effort to take all the |
7
|
|
|
* suck out of working with CSV. |
8
|
|
|
* |
9
|
|
|
* @version v0.2.1 |
10
|
|
|
* @copyright Copyright (c) 2016 Luke Visinoni <[email protected]> |
11
|
|
|
* @author Luke Visinoni <[email protected]> |
12
|
|
|
* @license https://github.com/deni-zen/csvelte/blob/master/LICENSE The MIT License (MIT) |
13
|
|
|
*/ |
14
|
|
|
namespace CSVelte; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Library Functions |
18
|
|
|
* |
19
|
|
|
* @package CSVelte |
20
|
|
|
* @subpackage functions |
21
|
|
|
* @since v0.2.1 |
22
|
|
|
*/ |
23
|
|
|
|
24
|
|
|
use \Iterator; |
25
|
|
|
use CSVelte\Taster; |
26
|
|
|
use CSVelte\Flavor; |
27
|
|
|
use CSVelte\IO\Stream; |
28
|
|
|
use CSVelte\IO\Resource; |
29
|
|
|
use CSVelte\IO\IteratorStream; |
30
|
|
|
use CSVelte\Contract\Streamable; |
31
|
|
|
|
32
|
|
|
use \InvalidArgumentException; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Stream - streams various types of values and objects. |
36
|
|
|
* |
37
|
|
|
* You can pass a string, or an iterator, or an object with a __toString() |
38
|
|
|
* method to this function and it will find the best possible way to stream the |
39
|
|
|
* data from that object. |
40
|
|
|
* |
41
|
|
|
* @param mixed The item you want to stream |
42
|
|
|
* @return \CSVelte\IO\Stream A stream object |
43
|
|
|
* @since v0.2.1 |
44
|
|
|
*/ |
45
|
|
|
function streamize($obj = '') |
46
|
|
|
{ |
47
|
27 |
|
if ($obj instanceof Streamable) { |
48
|
1 |
|
return $obj; |
49
|
|
|
} |
50
|
|
|
|
51
|
27 |
|
if ($obj instanceof Resource) { |
52
|
1 |
|
return $obj(); |
53
|
|
|
} |
54
|
|
|
|
55
|
26 |
|
if (is_resource($obj) && get_resource_type($obj) == 'stream') { |
56
|
1 |
|
return new Stream(new Resource($obj)); |
57
|
|
|
} |
58
|
|
|
|
59
|
25 |
|
if ($obj instanceof Iterator) { |
60
|
4 |
|
return new IteratorStream($obj); |
61
|
|
|
} |
62
|
|
|
|
63
|
21 |
|
if (is_object($obj) && method_exists($obj, '__toString')) { |
64
|
2 |
|
$obj = (string) $obj; |
65
|
2 |
|
} |
66
|
21 |
|
if (is_string($obj)) { |
67
|
19 |
|
$stream = Stream::open('php://temp', 'r+'); |
68
|
19 |
|
if ($obj !== '') { |
69
|
16 |
|
$res = $stream->getResource(); |
70
|
16 |
|
fwrite($res->getHandle(), $obj); |
71
|
16 |
|
fseek($res->getHandle(), 0); |
72
|
16 |
|
} |
73
|
19 |
|
return $stream; |
74
|
|
|
} |
75
|
|
|
|
76
|
2 |
|
throw new InvalidArgumentException(sprintf( |
77
|
2 |
|
"Invalid argument type for %s: %s", |
78
|
2 |
|
__FUNCTION__, |
79
|
2 |
|
gettype($obj) |
80
|
2 |
|
)); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
function stream_resource($uri, $mode = null, $context = null, $lazy = true) |
84
|
|
|
{ |
85
|
2 |
|
$res = (new Resource($uri, $mode)) |
86
|
2 |
|
->setContextResource($context); |
87
|
2 |
|
if (!$lazy) $res->connect(); |
88
|
2 |
|
return $res; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
function stream($uri, $mode = null, $context = null, $lazy = true) |
92
|
|
|
{ |
93
|
|
|
$res = stream_resource($uri, $mode, $context, $lazy); |
94
|
|
|
return new Stream($res); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* "Taste" a stream object. |
99
|
|
|
* |
100
|
|
|
* Pass any class that implements the "Streamable" interface to this function |
101
|
|
|
* to auto-detect "flavor" (formatting attributes). |
102
|
|
|
* |
103
|
|
|
* @param \CSVelte\Contract\Streamable Any streamable class to analyze |
104
|
|
|
* @return \CSVelte\Flavor A flavor representing str eam's formatting attributes |
105
|
|
|
* @since v0.2.1 |
106
|
|
|
*/ |
107
|
|
|
function taste(Streamable $str) |
108
|
|
|
{ |
109
|
13 |
|
$taster = new Taster($str); |
110
|
13 |
|
return $taster(); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Does dataset being streamed by $str have a header row? |
115
|
|
|
* |
116
|
|
|
* @param \CSVelte\Contract\Streamable $str Stream object |
117
|
|
|
* @return boolean Whether stream dataset has header |
118
|
|
|
* @since v0.2.1 |
119
|
|
|
*/ |
120
|
|
|
function taste_has_header(Streamable $str) |
121
|
|
|
{ |
122
|
5 |
|
$taster = new Taster($str); |
123
|
5 |
|
$flv = $taster(); |
124
|
5 |
|
return $taster->lickHeader( |
125
|
5 |
|
$flv->delimiter, |
126
|
5 |
|
$flv->lineTerminator |
127
|
5 |
|
); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Collection factory. |
132
|
|
|
* |
133
|
|
|
* Simply an alias to (new Collection($in)). Allows for a little more concise and |
134
|
|
|
* simpler instantiation of a collection. Also I plan to eventually support |
135
|
|
|
* additional input types that will make this function more flexible and forgiving |
136
|
|
|
* than simply instantiating a Collection object, but for now the two are identical. |
137
|
|
|
* |
138
|
|
|
* @param array|Iterator $in Either an array or an iterator of data |
139
|
|
|
* @return \CSVelte\Collection A collection object containing data from $in |
140
|
|
|
* @see CSVelte\Collection::__construct() (alias) |
141
|
|
|
* ) |
142
|
|
|
*/ |
143
|
|
|
function collect($in = null) |
144
|
|
|
{ |
145
|
65 |
|
return new Collection($in); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* @param callable $callback |
150
|
|
|
* @param array ...$args |
151
|
|
|
* @return mixed |
152
|
|
|
*/ |
153
|
|
|
function getvalue(Callable $callback, ...$args) |
154
|
|
|
{ |
155
|
|
|
return $callback(...$args); |
156
|
|
|
} |