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
|
|
|
// @todo add this |
52
|
|
|
// if (($resource = $obj) instanceof Resource) { |
53
|
|
|
// return $resource() or $resource->stream(); |
54
|
|
|
// } |
55
|
|
|
|
56
|
27 |
|
if (is_resource($obj) && get_resource_type($obj) == 'stream') { |
57
|
1 |
|
return new Stream(new Resource($obj)); |
58
|
|
|
} |
59
|
|
|
|
60
|
26 |
|
if ($obj instanceof Iterator) { |
61
|
4 |
|
return new IteratorStream($obj); |
62
|
|
|
} |
63
|
|
|
|
64
|
22 |
|
if (is_object($obj) && method_exists($obj, '__toString')) { |
65
|
2 |
|
$obj = (string) $obj; |
66
|
2 |
|
} |
67
|
22 |
|
if (is_string($obj)) { |
68
|
20 |
|
$stream = Stream::open('php://temp', 'r+'); |
69
|
20 |
|
if ($obj !== '') { |
70
|
17 |
|
$res = $stream->getResource(); |
71
|
17 |
|
fwrite($res->getHandle(), $obj); |
72
|
17 |
|
fseek($res->getHandle(), 0); |
73
|
17 |
|
} |
74
|
20 |
|
return $stream; |
75
|
|
|
} |
76
|
|
|
|
77
|
2 |
|
throw new InvalidArgumentException(sprintf( |
78
|
2 |
|
"Invalid argument type for %s: %s", |
79
|
2 |
|
__FUNCTION__, |
80
|
2 |
|
gettype($obj) |
81
|
2 |
|
)); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
function stream_resource($uri, $mode = null, $context = null, $lazy = true) |
85
|
|
|
{ |
86
|
2 |
|
$res = (new Resource($uri, $mode)) |
87
|
2 |
|
->setContextResource($context); |
88
|
3 |
|
if (!$lazy) $res->connect(); |
89
|
2 |
|
return $res; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
function stream($uri, $mode = null, $context = null, $lazy = true) |
93
|
|
|
{ |
94
|
|
|
$res = stream_resource($uri, $mode, $context, $lazy); |
95
|
|
|
return new Stream($res); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* "Taste" a stream object. |
100
|
|
|
* |
101
|
|
|
* Pass any class that implements the "Streamable" interface to this function |
102
|
|
|
* to auto-detect "flavor" (formatting attributes). |
103
|
|
|
* |
104
|
|
|
* @param \CSVelte\Contract\Streamable Any streamable class to analyze |
105
|
|
|
* @return \CSVelte\Flavor A flavor representing stream's formatting attributes |
106
|
|
|
* @since v0.2.1 |
107
|
|
|
*/ |
108
|
|
|
function taste(Streamable $str) |
109
|
|
|
{ |
110
|
13 |
|
$taster = new Taster($str); |
111
|
13 |
|
return $taster(); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Does dataset being streamed by $str have a header row? |
116
|
|
|
* |
117
|
|
|
* @param \CSVelte\Contract\Streamable $str Stream object |
118
|
|
|
* @return boolean Whether stream dataset has header |
119
|
|
|
* @since v0.2.1 |
120
|
|
|
*/ |
121
|
|
|
function taste_has_header(Streamable $str) |
122
|
|
|
{ |
123
|
5 |
|
$taster = new Taster($str); |
124
|
5 |
|
$flv = $taster(); |
125
|
5 |
|
return $taster->lickHeader( |
126
|
5 |
|
$flv->delimiter, |
127
|
5 |
|
$flv->lineTerminator |
128
|
5 |
|
); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Collection factory. |
133
|
|
|
* |
134
|
|
|
* Simply an alias to (new Collection($in)). Allows for a little more concise and |
135
|
|
|
* simpler instantiation of a collection. Also I plan to eventually support |
136
|
|
|
* additional input types that will make this function more flexible and forgiving |
137
|
|
|
* than simply instantiating a Collection object, but for now the two are identical. |
138
|
|
|
* |
139
|
|
|
* @param array|Iterator $in Either an array or an iterator of data |
140
|
|
|
* @return \CSVelte\Collection A collection object containing data from $in |
141
|
|
|
* @see CSVelte\Collection::__construct() (alias) |
142
|
|
|
* ) |
143
|
|
|
*/ |
144
|
|
|
function collect($in = null) |
145
|
|
|
{ |
146
|
70 |
|
return new Collection($in); |
|
|
|
|
147
|
|
|
} |
148
|
|
|
|
This check looks at variables that have been passed in as parameters and are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.